From fa22c55f45367e0212ea8ea24de53f33de396a66 Mon Sep 17 00:00:00 2001 From: Anthony Clark Date: Fri, 17 Nov 2023 09:53:40 -0800 Subject: [PATCH 1/2] Require request validation formatter to extend controller --- src/Formatters/RequestValidation.php | 20 +++++++++++++++++ .../Formatters/RequestValidationTest.php | 22 +++++++++++++++++++ 2 files changed, 42 insertions(+) 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); + } } From 568b46730fe0b14f522c64a5f1f2f36b483078ba Mon Sep 17 00:00:00 2001 From: Anthony Clark Date: Fri, 17 Nov 2023 09:58:09 -0800 Subject: [PATCH 2/2] Dusting --- src/Commands/FormatCommand.php | 2 +- src/Commands/LintCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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; } }