diff --git a/src/Commands/FormatCommand.php b/src/Commands/FormatCommand.php index 1155e3b..27e7e64 100644 --- a/src/Commands/FormatCommand.php +++ b/src/Commands/FormatCommand.php @@ -93,7 +93,7 @@ private function formatFile(InputInterface $input, OutputInterface $output, $fil if (! empty($only = $input->getOption('only'))) { $formatters = array_filter($this->getAllFormatters($file), function ($formatter) use ($only) { foreach ($only as $filter) { - if (false !== strpos($formatter, $filter)) { + if (strpos($formatter, $filter) !== false) { return true; } } diff --git a/src/Commands/LintCommand.php b/src/Commands/LintCommand.php index 0edae6c..29faa8f 100644 --- a/src/Commands/LintCommand.php +++ b/src/Commands/LintCommand.php @@ -117,7 +117,7 @@ private function lintFile(InputInterface $input, OutputInterface $output, $file) if (! empty($only = $input->getOption('only'))) { $linters = array_filter($this->getAllLinters($file), function ($linter) use ($only) { foreach ($only as $filter) { - if (false !== strpos($linter, $filter)) { + if (strpos($linter, $filter) !== false) { return true; } } diff --git a/src/Formatters/RequestValidation.php b/src/Formatters/RequestValidation.php index b90a948..46f29bf 100644 --- a/src/Formatters/RequestValidation.php +++ b/src/Formatters/RequestValidation.php @@ -43,8 +43,28 @@ private function visitor(): NodeVisitorAbstract { return new class extends NodeVisitorAbstract { + private bool $extendsController = false; + + public function beforeTraverse(array $nodes) + { + $this->extendsController = false; + + return null; + } + public function enterNode(Node $node): Node|int|null { + if ($node instanceof Node\Stmt\Class_ + && ! empty($node->extends) + && $node->extends->toString() === 'Controller' + ) { + $this->extendsController = true; + } + + if (! $this->extendsController) { + return null; + } + if (! $node instanceof Node\Expr\MethodCall) { return null; } diff --git a/tests/Formatting/Formatters/RequestValidationTest.php b/tests/Formatting/Formatters/RequestValidationTest.php index 0de9423..62fdd68 100644 --- a/tests/Formatting/Formatters/RequestValidationTest.php +++ b/tests/Formatting/Formatters/RequestValidationTest.php @@ -114,4 +114,26 @@ public function store() $this->assertSame($file, $formatted); } + + /** @test */ + public function it_ignores_classes_that_do_not_extend_controller() + { + $file = <<<'file' +validate(['name' => 'required'], ['name.required' => 'Name is required']); + } +} +file; + + $formatted = (new TFormat)->format(new RequestValidation($file)); + + $this->assertSame($file, $formatted); + } }