Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Rules/Node/PathAwareNodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace TwigCsFixer\Rules\Node;

use Twig\Environment;
use Twig\Node\Node;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Rules\ConfigurableRuleInterface;

final class PathAwareNodeRule implements PathAwareNodeRuleInterface, ConfigurableRuleInterface
{
public function __construct(
private NodeRuleInterface $rule,
private string $filePattern,
) {
}

public function getConfiguration(): array
{
$config = $this->rule instanceof ConfigurableRuleInterface ? $this->rule->getConfiguration() : [];
$config['__class'] = $this->rule::class;
$config['__file_pattern'] = $this->filePattern;

return $config;
}

public function enterNode(Node $node, Environment $env): Node
{
return $this->rule->enterNode($node, $env);
}

public function leaveNode(Node $node, Environment $env): ?Node
{
return $this->rule->leaveNode($node, $env);
}

public function getPriority(): int
{
return $this->rule->getPriority();
}

public function setReport(Report $report, array $ignoredViolations = []): void
{
$this->rule->setReport($report, $ignoredViolations);
}

public function support(string $path): bool
{
return fnmatch($this->filePattern, $path);
}
}
14 changes: 14 additions & 0 deletions src/Rules/Node/PathAwareNodeRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules\Node;

use Twig\NodeVisitor\NodeVisitorInterface;
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\ViolationId;

interface PathAwareNodeRuleInterface extends NodeRuleInterface
{
public function support(string $path): bool;
}
34 changes: 34 additions & 0 deletions src/Rules/PathAwareRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\Report;
use TwigCsFixer\Token\Tokens;

final class PathAwareRule implements PathAwareRuleInterface, ConfigurableRuleInterface
{
public function __construct(
private RuleInterface $rule,
private string $filePattern,
) {
}

public function getConfiguration(): array
{
$config = $this->rule instanceof ConfigurableRuleInterface ? $this->rule->getConfiguration() : [];
$config['__class'] = $this->rule::class;
$config['__file_pattern'] = $this->filePattern;

return $config;
}

public function lintFile(Tokens $tokens, Report $report): void
{
$this->rule->lintFile($tokens, $report);
}

public function support(string $path): bool
{
return fnmatch($this->filePattern, $path);
}
}
13 changes: 13 additions & 0 deletions src/Rules/PathAwareRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\Report;
use TwigCsFixer\Token\Tokens;

interface PathAwareRuleInterface extends RuleInterface
{
public function support(string $path): bool;
}
15 changes: 13 additions & 2 deletions src/Runner/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Rules\Node\NodeRuleInterface;
use TwigCsFixer\Rules\Node\PathAwareNodeRuleInterface;
use TwigCsFixer\Rules\PathAwareRuleInterface;
use TwigCsFixer\Rules\RuleInterface;
use TwigCsFixer\Ruleset\Ruleset;
use TwigCsFixer\Token\TokenizerInterface;
Expand Down Expand Up @@ -46,8 +48,6 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =
$rules = array_filter($ruleset->getRules(), static fn ($rule) => $rule instanceof RuleInterface);
$nodeVisitorRules = array_filter($ruleset->getRules(), static fn ($rule) => $rule instanceof NodeRuleInterface);

$traverser = new NodeTraverser($this->env, $nodeVisitorRules);

// Process
foreach ($files as $file) {
$filePath = $file->getPathname();
Expand Down Expand Up @@ -117,6 +117,10 @@ public function run(iterable $files, Ruleset $ruleset, ?FixerInterface $fixer =
}

foreach ($rules as $rule) {
if ($rule instanceof PathAwareRuleInterface && !$rule->support($filePath)) {
continue;
}

$rule->lintFile($stream, $report);
}

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

$nodeVisitors = [];
foreach ($nodeVisitorRules as $nodeVisitor) {
if ($nodeVisitor instanceof PathAwareNodeRuleInterface && !$nodeVisitor->support($filePath)) {
continue;
}

$nodeVisitor->setReport($report, $stream->getIgnoredViolations());
$nodeVisitors[] = $nodeVisitor;
}

$traverser = new NodeTraverser($this->env, $nodeVisitors);
$traverser->traverse($node);
}

Expand Down
Loading