Skip to content

Commit 1b22637

Browse files
Poc of path aware rules
1 parent ecd3c1c commit 1b22637

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

src/Rules/Node/PathAwareNodeRule.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace TwigCsFixer\Rules\Node;
4+
5+
use Twig\Environment;
6+
use Twig\Node\Node;
7+
use TwigCsFixer\Report\Report;
8+
9+
final class PathAwareNodeRule implements PathAwareNodeRuleInterface
10+
{
11+
public function __construct(
12+
private NodeRuleInterface $rule,
13+
private string $filePattern,
14+
) {
15+
}
16+
17+
public function enterNode(Node $node, Environment $env): Node
18+
{
19+
return $this->rule->enterNode($node, $env);
20+
}
21+
22+
public function leaveNode(Node $node, Environment $env): ?Node
23+
{
24+
return $this->rule->leaveNode($node, $env);
25+
}
26+
27+
public function getPriority(): int
28+
{
29+
return $this->rule->getPriority();
30+
}
31+
32+
public function setReport(Report $report, array $ignoredViolations = []): void
33+
{
34+
$this->rule->setReport($report, $ignoredViolations);
35+
}
36+
37+
public function support(string $path): bool
38+
{
39+
return fnmatch($this->filePattern, $path);
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCsFixer\Rules\Node;
6+
7+
use Twig\NodeVisitor\NodeVisitorInterface;
8+
use TwigCsFixer\Report\Report;
9+
use TwigCsFixer\Report\ViolationId;
10+
11+
interface PathAwareNodeRuleInterface extends NodeRuleInterface
12+
{
13+
public function support(string $path): bool;
14+
}

src/Rules/PathAwareRule.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace TwigCsFixer\Rules;
4+
5+
use TwigCsFixer\Report\Report;
6+
use TwigCsFixer\Token\Tokens;
7+
8+
final class PathAwareRule implements PathAwareRuleInterface
9+
{
10+
public function __construct(
11+
private RuleInterface $rule,
12+
private string $filePattern,
13+
) {
14+
}
15+
16+
public function lintFile(Tokens $tokens, Report $report): void
17+
{
18+
$this->rule->lintFile($tokens, $report);
19+
}
20+
21+
public function support(string $path): bool
22+
{
23+
return fnmatch($this->filePattern, $path);
24+
}
25+
}

src/Rules/PathAwareRuleInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TwigCsFixer\Rules;
6+
7+
use TwigCsFixer\Report\Report;
8+
use TwigCsFixer\Token\Tokens;
9+
10+
interface PathAwareRuleInterface extends RuleInterface
11+
{
12+
public function support(string $path): bool;
13+
}

src/Runner/Linter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use TwigCsFixer\Report\Violation;
1818
use TwigCsFixer\Report\ViolationId;
1919
use TwigCsFixer\Rules\Node\NodeRuleInterface;
20+
use TwigCsFixer\Rules\Node\PathAwareNodeRuleInterface;
21+
use TwigCsFixer\Rules\PathAwareRuleInterface;
2022
use TwigCsFixer\Rules\RuleInterface;
2123
use TwigCsFixer\Ruleset\Ruleset;
2224
use TwigCsFixer\Token\TokenizerInterface;
@@ -46,8 +48,6 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =
4648
$rules = array_filter($ruleset->getRules(), static fn ($rule) => $rule instanceof RuleInterface);
4749
$nodeVisitorRules = array_filter($ruleset->getRules(), static fn ($rule) => $rule instanceof NodeRuleInterface);
4850

49-
$traverser = new NodeTraverser($this->env, $nodeVisitorRules);
50-
5151
// Process
5252
foreach ($files as $file) {
5353
$filePath = $file->getPathname();
@@ -117,6 +117,10 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =
117117
}
118118

119119
foreach ($rules as $rule) {
120+
if ($rule instanceof PathAwareRuleInterface && !$rule->support($filePath)) {
121+
continue;
122+
}
123+
120124
$rule->lintFile($stream, $report);
121125
}
122126

@@ -126,10 +130,17 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =
126130
continue;
127131
}
128132

133+
$nodeVisitors = [];
129134
foreach ($nodeVisitorRules as $nodeVisitor) {
135+
if ($rule instanceof PathAwareNodeRuleInterface && !$rule->support($filePath)) {
136+
continue;
137+
}
138+
130139
$nodeVisitor->setReport($report, $stream->getIgnoredViolations());
140+
$nodeVisitors[] = $nodeVisitor;
131141
}
132142

143+
$traverser = new NodeTraverser($this->env, $nodeVisitors);
133144
$traverser->traverse($node);
134145
}
135146

0 commit comments

Comments
 (0)