Skip to content

Commit cfcc475

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

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

src/Rules/Node/PathAwareNodeRule.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace TwigCsFixer\Rules\Node;
4+
5+
use Twig\Environment;
6+
use Twig\Node\Node;
7+
use TwigCsFixer\Report\Report;
8+
use TwigCsFixer\Rules\ConfigurableRuleInterface;
9+
10+
final class PathAwareNodeRule implements PathAwareNodeRuleInterface, ConfigurableRuleInterface
11+
{
12+
public function __construct(
13+
private NodeRuleInterface $rule,
14+
private string $filePattern,
15+
) {
16+
}
17+
18+
public function getConfiguration(): array
19+
{
20+
$config = $this->rule instanceof ConfigurableRuleInterface ? $this->rule->getConfiguration() : [];
21+
$config['__class'] = $this->rule::class;
22+
$config['__file_pattern'] = $this->filePattern;
23+
24+
return $config;
25+
}
26+
27+
public function enterNode(Node $node, Environment $env): Node
28+
{
29+
return $this->rule->enterNode($node, $env);
30+
}
31+
32+
public function leaveNode(Node $node, Environment $env): ?Node
33+
{
34+
return $this->rule->leaveNode($node, $env);
35+
}
36+
37+
public function getPriority(): int
38+
{
39+
return $this->rule->getPriority();
40+
}
41+
42+
public function setReport(Report $report, array $ignoredViolations = []): void
43+
{
44+
$this->rule->setReport($report, $ignoredViolations);
45+
}
46+
47+
public function support(string $path): bool
48+
{
49+
return fnmatch($this->filePattern, $path);
50+
}
51+
}
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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, ConfigurableRuleInterface
9+
{
10+
public function __construct(
11+
private RuleInterface $rule,
12+
private string $filePattern,
13+
) {
14+
}
15+
16+
public function getConfiguration(): array
17+
{
18+
$config = $this->rule instanceof ConfigurableRuleInterface ? $this->rule->getConfiguration() : [];
19+
$config['__class'] = $this->rule::class;
20+
$config['__file_pattern'] = $this->filePattern;
21+
22+
return $config;
23+
}
24+
25+
public function lintFile(Tokens $tokens, Report $report): void
26+
{
27+
$this->rule->lintFile($tokens, $report);
28+
}
29+
30+
public function support(string $path): bool
31+
{
32+
return fnmatch($this->filePattern, $path);
33+
}
34+
}

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 ($nodeVisitor instanceof PathAwareNodeRuleInterface && !$nodeVisitor->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)