Repository: tighten/tlint (php)

Issue:
Update`RequestValidation` formatter to only format classes extending base controller
Only controllers extending the base controller will have `$this->validate`


Pull request description:
Require request validation formatter to extend controller
This PR requires the `RequestValidation` formatter to extend the controller class. 

Closes #324

Patch:
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 @@ class FormatCommand extends BaseCommand
         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 @@ class LintCommand extends BaseCommand
         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 @@ class RequestValidation extends BaseFormatter
     {
         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;
                 }


Test patch:
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 @@ file;
 
         $this->assertSame($file, $formatted);
     }
+
+    /** @test */
+    public function it_ignores_classes_that_do_not_extend_controller()
+    {
+        $file = <<<'file'
+<?php
+
+namespace App;
+
+class ControllerA extends NotController
+{
+    public function store()
+    {
+        $this->validate(['name' => 'required'], ['name.required' => 'Name is required']);
+    }
+}
+file;
+
+        $formatted = (new TFormat)->format(new RequestValidation($file));
+
+        $this->assertSame($file, $formatted);
+    }
 }