Browse Source

minor #5193 DX: add AbstractPhpUnitFixer (kubawerlos)

This PR was merged into the 2.15 branch.

Discussion
----------

DX: add AbstractPhpUnitFixer

Follow up to: https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/5190

//cc @SpacePossum

Commits
-------

c28552602 DX: add AbstractPhpUnitFixer
SpacePossum 4 years ago
parent
commit
31423d4bd2

+ 70 - 0
src/Fixer/AbstractPhpUnitFixer.php

@@ -0,0 +1,70 @@
+<?php
+
+/*
+ * This file is part of PHP CS Fixer.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace PhpCsFixer\Fixer;
+
+use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
+use PhpCsFixer\Tokenizer\Tokens;
+
+/**
+ * @internal
+ */
+abstract class AbstractPhpUnitFixer extends AbstractFixer
+{
+    /**
+     * {@inheritdoc}
+     */
+    final public function isCandidate(Tokens $tokens)
+    {
+        return $tokens->isAllTokenKindsFound([T_CLASS, T_STRING]);
+    }
+
+    final protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    {
+        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
+
+        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indices) {
+            $this->applyPhpUnitClassFix($tokens, $indices[0], $indices[1]);
+        }
+    }
+
+    /**
+     * @param int $startIndex
+     * @param int $endIndex
+     */
+    abstract protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex);
+
+    /**
+     * @param int $index
+     *
+     * @return int
+     */
+    final protected function getDocBlockIndex(Tokens $tokens, $index)
+    {
+        do {
+            $index = $tokens->getPrevNonWhitespace($index);
+        } while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT]));
+
+        return $index;
+    }
+
+    /**
+     * @param int $index
+     *
+     * @return bool
+     */
+    final protected function isPHPDoc(Tokens $tokens, $index)
+    {
+        return $tokens[$index]->isGivenKind(T_DOC_COMMENT);
+    }
+}

+ 9 - 22
src/Fixer/PhpUnit/PhpUnitConstructFixer.php

@@ -12,14 +12,13 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -27,7 +26,7 @@ use PhpCsFixer\Tokenizer\Tokens;
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-final class PhpUnitConstructFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class PhpUnitConstructFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
 {
     private static $assertionFixers = [
         'assertSame' => 'fixAssertPositive',
@@ -36,14 +35,6 @@ final class PhpUnitConstructFixer extends AbstractFixer implements Configuration
         'assertNotSame' => 'fixAssertNegative',
     ];
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -104,25 +95,21 @@ final class FooTest extends \PHPUnit_Framework_TestCase {
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         // no assertions to be fixed - fast return
         if (empty($this->configuration['assertions'])) {
             return;
         }
 
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            foreach ($this->configuration['assertions'] as $assertionMethod) {
-                $assertionFixer = self::$assertionFixers[$assertionMethod];
+        foreach ($this->configuration['assertions'] as $assertionMethod) {
+            $assertionFixer = self::$assertionFixers[$assertionMethod];
 
-                for ($index = $indexes[0]; $index < $indexes[1]; ++$index) {
-                    $index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);
+            for ($index = $startIndex; $index < $endIndex; ++$index) {
+                $index = $this->{$assertionFixer}($tokens, $index, $assertionMethod);
 
-                    if (null === $index) {
-                        break;
-                    }
+                if (null === $index) {
+                    break;
                 }
             }
         }

+ 10 - 16
src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
@@ -27,7 +27,7 @@ use PhpCsFixer\Tokenizer\Tokens;
  * @author SpacePossum
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-final class PhpUnitDedicateAssertFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
 {
     private static $fixMap = [
         'array_key_exists' => ['assertArrayNotHasKey', 'assertArrayHasKey'],
@@ -120,14 +120,6 @@ final class PhpUnitDedicateAssertFixer extends AbstractFixer implements Configur
         }
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -190,11 +182,9 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
-        //findPhpUnitClasses
-
-        foreach ($this->getPreviousAssertCall($tokens) as $assertCall) {
+        foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) {
             // test and fix for assertTrue/False to dedicated asserts
             if ('asserttrue' === $assertCall['loweredName'] || 'assertfalse' === $assertCall['loweredName']) {
                 $this->fixAssertTrueFalse($tokens, $assertCall);
@@ -397,11 +387,15 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         ]);
     }
 
-    private function getPreviousAssertCall(Tokens $tokens)
+    /**
+     * @param int $startIndex
+     * @param int $endIndex
+     */
+    private function getPreviousAssertCall(Tokens $tokens, $startIndex, $endIndex)
     {
         $functionsAnalyzer = new FunctionsAnalyzer();
 
-        for ($index = $tokens->count(); $index > 0; --$index) {
+        for ($index = $endIndex; $index > $startIndex; --$index) {
             $index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]);
             if (null === $index) {
                 return;

+ 4 - 25
src/Fixer/PhpUnit/PhpUnitDedicateAssertInternalTypeFixer.php

@@ -12,13 +12,12 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\Tokenizer\TokensAnalyzer;
@@ -26,7 +25,7 @@ use PhpCsFixer\Tokenizer\TokensAnalyzer;
 /**
  * @author Filippo Tessarotto <zoeslam@gmail.com>
  */
-final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class PhpUnitDedicateAssertInternalTypeFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
 {
     /**
      * @var array
@@ -76,14 +75,6 @@ final class MyTest extends \PHPUnit\Framework\TestCase
         );
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_STRING]);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -102,17 +93,6 @@ final class MyTest extends \PHPUnit\Framework\TestCase
         return -16;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            $this->updateAssertInternalTypeMethods($tokens, $indexes[0], $indexes[1]);
-        }
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -128,10 +108,9 @@ final class MyTest extends \PHPUnit\Framework\TestCase
     }
 
     /**
-     * @param int $startIndex
-     * @param int $endIndex
+     * {@inheritdoc}
      */
-    private function updateAssertInternalTypeMethods(Tokens $tokens, $startIndex, $endIndex)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         $anonymousClassIndexes = [];
         $tokenAnalyzer = new TokensAnalyzer($tokens);

+ 6 - 23
src/Fixer/PhpUnit/PhpUnitExpectationFixer.php

@@ -12,14 +12,13 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -27,7 +26,7 @@ use PhpCsFixer\Tokenizer\Tokens;
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-final class PhpUnitExpectationFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
+final class PhpUnitExpectationFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface, WhitespacesAwareFixerInterface
 {
     /**
      * @var array<string, string>
@@ -130,14 +129,6 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         return 0;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_CLASS);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -146,17 +137,6 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         return true;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            $this->fixExpectation($tokens, $indexes[0], $indexes[1]);
-        }
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -171,7 +151,10 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         ]);
     }
 
-    private function fixExpectation(Tokens $tokens, $startIndex, $endIndex)
+    /**
+     * {@inheritdoc}
+     */
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         $argumentsAnalyzer = new ArgumentsAnalyzer();
 

+ 7 - 20
src/Fixer/PhpUnit/PhpUnitFqcnAnnotationFixer.php

@@ -12,10 +12,9 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Preg;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -23,7 +22,7 @@ use PhpCsFixer\Tokenizer\Tokens;
 /**
  * @author Roland Franssen <franssen.roland@gmail.com>
  */
-final class PhpUnitFqcnAnnotationFixer extends AbstractFixer
+final class PhpUnitFqcnAnnotationFixer extends AbstractPhpUnitFixer
 {
     /**
      * {@inheritdoc}
@@ -64,25 +63,13 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     /**
      * {@inheritdoc}
      */
-    public function isCandidate(Tokens $tokens)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_DOC_COMMENT]);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            $startIndex = $indexes[0];
-            $prevDocCommentIndex = $tokens->getPrevTokenOfKind($startIndex, [[T_DOC_COMMENT]]);
-            if (null !== $prevDocCommentIndex) {
-                $startIndex = $prevDocCommentIndex;
-            }
-            $this->fixPhpUnitClass($tokens, $startIndex, $indexes[1]);
+        $prevDocCommentIndex = $tokens->getPrevTokenOfKind($startIndex, [[T_DOC_COMMENT]]);
+        if (null !== $prevDocCommentIndex) {
+            $startIndex = $prevDocCommentIndex;
         }
+        $this->fixPhpUnitClass($tokens, $startIndex, $endIndex);
     }
 
     /**

+ 7 - 53
src/Fixer/PhpUnit/PhpUnitInternalClassFixer.php

@@ -12,9 +12,9 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\DocBlock\DocBlock;
 use PhpCsFixer\DocBlock\Line;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
 use PhpCsFixer\FixerConfiguration\AllowedValueSubset;
@@ -22,14 +22,13 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
 /**
  * @author Gert de Pagter <BackEndTea@gmail.com>
  */
-final class PhpUnitInternalClassFixer extends AbstractFixer implements WhitespacesAwareFixerInterface, ConfigurationDefinitionFixerInterface
+final class PhpUnitInternalClassFixer extends AbstractPhpUnitFixer implements WhitespacesAwareFixerInterface, ConfigurationDefinitionFixerInterface
 {
     /**
      * {@inheritdoc}
@@ -58,14 +57,6 @@ final class PhpUnitInternalClassFixer extends AbstractFixer implements Whitespac
         return 68;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_CLASS);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -82,19 +73,10 @@ final class PhpUnitInternalClassFixer extends AbstractFixer implements Whitespac
         ]);
     }
 
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens, true) as $indexes) {
-            $this->markClassInternal($tokens, $indexes[0]);
-        }
-    }
-
     /**
-     * @param int $startIndex
+     * {@inheritdoc}
      */
-    private function markClassInternal(Tokens $tokens, $startIndex)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         $classIndex = $tokens->getPrevTokenOfKind($startIndex, [[T_CLASS]]);
 
@@ -104,13 +86,11 @@ final class PhpUnitInternalClassFixer extends AbstractFixer implements Whitespac
 
         $docBlockIndex = $this->getDocBlockIndex($tokens, $classIndex);
 
-        if ($this->hasDocBlock($tokens, $classIndex)) {
+        if ($this->isPHPDoc($tokens, $docBlockIndex)) {
             $this->updateDocBlockIfNeeded($tokens, $docBlockIndex);
-
-            return;
+        } else {
+            $this->createDocBlock($tokens, $docBlockIndex);
         }
-
-        $this->createDocBlock($tokens, $docBlockIndex);
     }
 
     /**
@@ -157,32 +137,6 @@ final class PhpUnitInternalClassFixer extends AbstractFixer implements Whitespac
         $tokens[$docBlockIndex] = new Token([T_DOC_COMMENT, $lines]);
     }
 
-    /**
-     * @param int $index
-     *
-     * @return bool
-     */
-    private function hasDocBlock(Tokens $tokens, $index)
-    {
-        $docBlockIndex = $this->getDocBlockIndex($tokens, $index);
-
-        return $tokens[$docBlockIndex]->isGivenKind(T_DOC_COMMENT);
-    }
-
-    /**
-     * @param int $index
-     *
-     * @return int
-     */
-    private function getDocBlockIndex(Tokens $tokens, $index)
-    {
-        do {
-            $index = $tokens->getPrevNonWhitespace($index);
-        } while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT]));
-
-        return $index;
-    }
-
     /**
      * @param int $index
      *

+ 10 - 61
src/Fixer/PhpUnit/PhpUnitMethodCasingFixer.php

@@ -12,15 +12,14 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\DocBlock\DocBlock;
 use PhpCsFixer\DocBlock\Line;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Preg;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -30,7 +29,7 @@ use PhpCsFixer\Utils;
 /**
  * @author Filippo Tessarotto <zoeslam@gmail.com>
  */
-final class PhpUnitMethodCasingFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class PhpUnitMethodCasingFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
 {
     /**
      * @internal
@@ -81,25 +80,6 @@ class MyTest extends \\PhpUnit\\FrameWork\\TestCase
         return 0;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION]);
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            $this->applyCasing($tokens, $indexes[0], $indexes[1]);
-        }
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -114,10 +94,9 @@ class MyTest extends \\PhpUnit\\FrameWork\\TestCase
     }
 
     /**
-     * @param int $startIndex
-     * @param int $endIndex
+     * {@inheritdoc}
      */
-    private function applyCasing(Tokens $tokens, $startIndex, $endIndex)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         for ($index = $endIndex - 1; $index > $startIndex; --$index) {
             if (!$this->isTestMethod($tokens, $index)) {
@@ -133,7 +112,8 @@ class MyTest extends \\PhpUnit\\FrameWork\\TestCase
             }
 
             $docBlockIndex = $this->getDocBlockIndex($tokens, $index);
-            if ($this->hasDocBlock($tokens, $index)) {
+
+            if ($this->isPHPDoc($tokens, $docBlockIndex)) {
                 $this->updateDocBlock($tokens, $docBlockIndex);
             }
         }
@@ -177,18 +157,13 @@ class MyTest extends \\PhpUnit\\FrameWork\\TestCase
         if ($this->startsWith('test', $functionName)) {
             return true;
         }
-        // If the function doesn't have test in its name, and no doc block, it's not a test
-        if (!$this->hasDocBlock($tokens, $index)) {
-            return false;
-        }
 
         $docBlockIndex = $this->getDocBlockIndex($tokens, $index);
-        $doc = $tokens[$docBlockIndex]->getContent();
-        if (false === strpos($doc, '@test')) {
-            return false;
-        }
 
-        return true;
+        return
+            $this->isPHPDoc($tokens, $docBlockIndex) // If the function doesn't have test in its name, and no doc block, it's not a test
+            && false !== strpos($tokens[$docBlockIndex]->getContent(), '@test')
+        ;
     }
 
     /**
@@ -214,32 +189,6 @@ class MyTest extends \\PhpUnit\\FrameWork\\TestCase
         return substr($haystack, 0, \strlen($needle)) === $needle;
     }
 
-    /**
-     * @param int $index
-     *
-     * @return bool
-     */
-    private function hasDocBlock(Tokens $tokens, $index)
-    {
-        $docBlockIndex = $this->getDocBlockIndex($tokens, $index);
-
-        return $tokens[$docBlockIndex]->isGivenKind(T_DOC_COMMENT);
-    }
-
-    /**
-     * @param int $index
-     *
-     * @return int
-     */
-    private function getDocBlockIndex(Tokens $tokens, $index)
-    {
-        do {
-            $index = $tokens->getPrevNonWhitespace($index);
-        } while ($tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_COMMENT]));
-
-        return $index;
-    }
-
     /**
      * @param int $docBlockIndex
      */

+ 18 - 30
src/Fixer/PhpUnit/PhpUnitMockFixer.php

@@ -12,13 +12,12 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\Fixer\ConfigurationDefinitionFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -26,7 +25,7 @@ use PhpCsFixer\Tokenizer\Tokens;
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  */
-final class PhpUnitMockFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
+final class PhpUnitMockFixer extends AbstractPhpUnitFixer implements ConfigurationDefinitionFixerInterface
 {
     /**
      * @var bool
@@ -74,14 +73,6 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isTokenKindFound(T_CLASS);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -103,32 +94,29 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
         $argumentsAnalyzer = new ArgumentsAnalyzer();
 
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            for ($index = $indexes[0]; $index < $indexes[1]; ++$index) {
-                if (!$tokens[$index]->isGivenKind(T_OBJECT_OPERATOR)) {
-                    continue;
-                }
+        for ($index = $startIndex; $index < $endIndex; ++$index) {
+            if (!$tokens[$index]->isGivenKind(T_OBJECT_OPERATOR)) {
+                continue;
+            }
 
-                $index = $tokens->getNextMeaningfulToken($index);
+            $index = $tokens->getNextMeaningfulToken($index);
 
-                if ($tokens[$index]->equals([T_STRING, 'getMockWithoutInvokingTheOriginalConstructor'], false)) {
-                    $tokens[$index] = new Token([T_STRING, 'createMock']);
-                } elseif ($tokens[$index]->equals([T_STRING, 'getMock'], false)) {
-                    $openingParenthesis = $tokens->getNextMeaningfulToken($index);
-                    $closingParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingParenthesis);
+            if ($tokens[$index]->equals([T_STRING, 'getMockWithoutInvokingTheOriginalConstructor'], false)) {
+                $tokens[$index] = new Token([T_STRING, 'createMock']);
+            } elseif ($tokens[$index]->equals([T_STRING, 'getMock'], false)) {
+                $openingParenthesis = $tokens->getNextMeaningfulToken($index);
+                $closingParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingParenthesis);
 
-                    $argumentsCount = $argumentsAnalyzer->countArguments($tokens, $openingParenthesis, $closingParenthesis);
+                $argumentsCount = $argumentsAnalyzer->countArguments($tokens, $openingParenthesis, $closingParenthesis);
 
-                    if (1 === $argumentsCount) {
-                        $tokens[$index] = new Token([T_STRING, 'createMock']);
-                    } elseif (2 === $argumentsCount && true === $this->fixCreatePartialMock) {
-                        $tokens[$index] = new Token([T_STRING, 'createPartialMock']);
-                    }
+                if (1 === $argumentsCount) {
+                    $tokens[$index] = new Token([T_STRING, 'createMock']);
+                } elseif (2 === $argumentsCount && true === $this->fixCreatePartialMock) {
+                    $tokens[$index] = new Token([T_STRING, 'createPartialMock']);
                 }
             }
         }

+ 3 - 24
src/Fixer/PhpUnit/PhpUnitMockShortWillReturnFixer.php

@@ -12,10 +12,9 @@
 
 namespace PhpCsFixer\Fixer\PhpUnit;
 
-use PhpCsFixer\AbstractFixer;
+use PhpCsFixer\Fixer\AbstractPhpUnitFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
-use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -23,7 +22,7 @@ use PhpCsFixer\Tokenizer\Tokens;
  * @author Michał Adamski <michal.adamski@gmail.com>
  * @author Kuba Werłos <werlos@gmail.com>
  */
-final class PhpUnitMockShortWillReturnFixer extends AbstractFixer
+final class PhpUnitMockShortWillReturnFixer extends AbstractPhpUnitFixer
 {
     /**
      * @internal
@@ -64,14 +63,6 @@ final class MyTest extends \PHPUnit_Framework_TestCase
         );
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function isCandidate(Tokens $tokens)
-    {
-        return $tokens->isAllTokenKindsFound([T_CLASS, T_OBJECT_OPERATOR, T_STRING]);
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -83,19 +74,7 @@ final class MyTest extends \PHPUnit_Framework_TestCase
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
-    {
-        $phpUnitTestCaseIndicator = new PhpUnitTestCaseIndicator();
-        foreach ($phpUnitTestCaseIndicator->findPhpUnitClasses($tokens) as $indexes) {
-            $this->fixWillReturn($tokens, $indexes[0], $indexes[1]);
-        }
-    }
-
-    /**
-     * @param int $startIndex
-     * @param int $endIndex
-     */
-    private function fixWillReturn(Tokens $tokens, $startIndex, $endIndex)
+    protected function applyPhpUnitClassFix(Tokens $tokens, $startIndex, $endIndex)
     {
         for ($index = $startIndex; $index < $endIndex; ++$index) {
             if (!$tokens[$index]->isGivenKind(T_OBJECT_OPERATOR)) {

Some files were not shown because too many files changed in this diff