Repository: tighten/tlint (php)

Issue:
Exempt protected setUp/tearDown for ClassThingsOrder
If you have a setup method, it has to be declared protected, but then we get a lint warning. Can we make an exception for the setUp method and possibly also teardown? (As they are protected, but we still likely want them at the top, #edgeCase)


Pull request:
Exempt protected setUp/tearDown for ClassThingsOrder.
Closes #204 

Patch:
diff --git a/src/Linters/ClassThingsOrder.php b/src/Linters/ClassThingsOrder.php
index 1e16f08..3954e94 100644
--- a/src/Linters/ClassThingsOrder.php
+++ b/src/Linters/ClassThingsOrder.php
@@ -84,6 +84,15 @@ class ClassThingsOrder extends BaseLinter
 
                     throw new Exception('Unknown statement');
                 }, array_filter($node->stmts, function (Node\Stmt $stmt) {
+                    // Ignore PhpUnit protected setUp/tearDown method
+                    if ($stmt instanceof Node\Stmt\ClassMethod
+                        && $stmt->isProtected()
+                        && in_array($stmt->name->toString(), ['setUp', 'tearDown'])
+                        && $stmt->returnType->toString() === 'void'
+                    ) {
+                        return false;
+                    }
+
                     return ! $stmt instanceof Node\Stmt\Nop;
                 }));
 


Test patch:
diff --git a/tests/Linting/Linters/ClassThingsOrderTest.php b/tests/Linting/Linters/ClassThingsOrderTest.php
index 9271c9f..4fdb9be 100644
--- a/tests/Linting/Linters/ClassThingsOrderTest.php
+++ b/tests/Linting/Linters/ClassThingsOrderTest.php
@@ -135,4 +135,74 @@ file;
 
         $this->assertEquals(3, $lints[0]->getNode()->getLine());
     }
+
+    /** @test */
+    function does_not_trigger_on_setup_setup() {
+        $file = <<<file
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Tests\TestCase;
+
+class ResourceSearchTest extends TestCase
+{
+    use RefreshDatabase;
+
+    protected function setUp(): void
+    {
+        // Do stuff
+        parent::setUp();
+    }
+
+    /** @test */
+    function true_is_true()
+    {
+        \$this->assertTrue(true);
+    }
+}
+file;
+
+        $lints = (new TLint)->lint(
+            new ClassThingsOrder($file)
+        );
+
+        $this->assertEmpty($lints);
+    }
+
+    /** @test */
+    function does_not_trigger_on_phpunit_teardown() {
+        $file = <<<file
+<?php
+
+namespace Tests\Feature;
+
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Tests\TestCase;
+
+class ResourceSearchTest extends TestCase
+{
+    use RefreshDatabase;
+
+    protected function tearDown(): void
+    {
+        // Do stuff
+        parent::tearDown();
+    }
+
+    /** @test */
+    function true_is_true()
+    {
+        \$this->assertTrue(true);
+    }
+}
+file;
+
+        $lints = (new TLint)->lint(
+            new ClassThingsOrder($file)
+        );
+
+        $this->assertEmpty($lints);
+    }
 }