Browse Source

AccessibleObject - remove most of usage

Dariusz Ruminski 7 years ago
parent
commit
65ea165815

+ 0 - 62
src/AbstractFunctionReferenceFixer.php

@@ -32,20 +32,6 @@ abstract class AbstractFunctionReferenceFixer extends AbstractFixer
         return true;
     }
 
-    /**
-     * Count amount of parameters in a function/method reference.
-     *
-     * @param Tokens $tokens
-     * @param int    $openParenthesis
-     * @param int    $closeParenthesis
-     *
-     * @return int
-     */
-    protected function countArguments(Tokens $tokens, $openParenthesis, $closeParenthesis)
-    {
-        return count($this->getArguments($tokens, $openParenthesis, $closeParenthesis));
-    }
-
     /**
      * Looks up Tokens sequence for suitable candidates and delivers boundaries information,
      * which can be supplied by other methods in this abstract class.
@@ -97,52 +83,4 @@ abstract class AbstractFunctionReferenceFixer extends AbstractFixer
 
         return array($functionName, $openParenthesis, $closeParenthesis);
     }
-
-    /**
-     * Returns start and end token indexes of arguments.
-     *
-     * Return an array which each index being the first token af an
-     * argument and the value the last. Including non-function tokens
-     * such as comments and white space tokens, but without the separation
-     * tokens like '(', ',' and ')'.
-     *
-     * @param Tokens $tokens
-     * @param int    $openParenthesis
-     * @param int    $closeParenthesis
-     *
-     * @return array<int, int>
-     */
-    protected function getArguments(Tokens $tokens, $openParenthesis, $closeParenthesis)
-    {
-        $arguments = array();
-        $firstSensibleToken = $tokens->getNextMeaningfulToken($openParenthesis);
-        if ($tokens[$firstSensibleToken]->equals(')')) {
-            return $arguments;
-        }
-
-        $paramContentIndex = $openParenthesis + 1;
-        $argumentsStart = $paramContentIndex;
-        for (; $paramContentIndex < $closeParenthesis; ++$paramContentIndex) {
-            $token = $tokens[$paramContentIndex];
-
-            // skip nested (), [], {} constructs
-            $blockDefinitionProbe = Tokens::detectBlockType($token);
-
-            if (null !== $blockDefinitionProbe && true === $blockDefinitionProbe['isStart']) {
-                $paramContentIndex = $tokens->findBlockEnd($blockDefinitionProbe['type'], $paramContentIndex);
-
-                continue;
-            }
-
-            // if comma matched, increase arguments counter
-            if ($token->equals(',')) {
-                $arguments[$argumentsStart] = $paramContentIndex - 1;
-                $argumentsStart = $paramContentIndex + 1;
-            }
-        }
-
-        $arguments[$argumentsStart] = $paramContentIndex - 1;
-
-        return $arguments;
-    }
 }

+ 2 - 2
src/ConfigurationException/InvalidConfigurationException.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\ConfigurationException;
 
-use PhpCsFixer\Console\Command\FixCommand;
+use PhpCsFixer\Console\Command\FixCommandExitStatusCalculator;
 
 /**
  * Exceptions of this type are thrown on misconfiguration of the Fixer.
@@ -32,7 +32,7 @@ class InvalidConfigurationException extends \InvalidArgumentException
     {
         parent::__construct(
             $message,
-            null === $code ? FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG : $code,
+            null === $code ? FixCommandExitStatusCalculator::EXIT_STATUS_FLAG_HAS_INVALID_CONFIG : $code,
             $previous
         );
     }

+ 2 - 2
src/ConfigurationException/InvalidFixerConfigurationException.php

@@ -12,7 +12,7 @@
 
 namespace PhpCsFixer\ConfigurationException;
 
-use PhpCsFixer\Console\Command\FixCommand;
+use PhpCsFixer\Console\Command\FixCommandExitStatusCalculator;
 
 /**
  * Exception thrown by Fixers on misconfiguration.
@@ -37,7 +37,7 @@ class InvalidFixerConfigurationException extends InvalidConfigurationException
     {
         parent::__construct(
             sprintf('[%s] %s', $fixerName, $message),
-            FixCommand::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG,
+            FixCommandExitStatusCalculator::EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG,
             $previous
         );
         $this->fixerName = $fixerName;

+ 3 - 37
src/Console/Command/FixCommand.php

@@ -38,13 +38,6 @@ use Symfony\Component\Stopwatch\Stopwatch;
  */
 final class FixCommand extends Command
 {
-    // Exit status 1 is reserved for environment constraints not matched.
-    const EXIT_STATUS_FLAG_HAS_INVALID_FILES = 4;
-    const EXIT_STATUS_FLAG_HAS_CHANGED_FILES = 8;
-    const EXIT_STATUS_FLAG_HAS_INVALID_CONFIG = 16;
-    const EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG = 32;
-    const EXIT_STATUS_FLAG_EXCEPTION_IN_APP = 64;
-
     /**
      * EventDispatcher instance.
      *
@@ -247,40 +240,13 @@ final class FixCommand extends Command
             }
         }
 
-        return $this->calculateExitStatus(
+        $exitStatusCalculator = new FixCommandExitStatusCalculator();
+
+        return $exitStatusCalculator->calculate(
             $resolver->isDryRun(),
             count($changed) > 0,
             count($invalidErrors) > 0,
             count($exceptionErrors) > 0
         );
     }
-
-    /**
-     * @param bool $isDryRun
-     * @param bool $hasChangedFiles
-     * @param bool $hasInvalidErrors
-     * @param bool $hasExceptionErrors
-     *
-     * @return int
-     */
-    private function calculateExitStatus($isDryRun, $hasChangedFiles, $hasInvalidErrors, $hasExceptionErrors)
-    {
-        $exitStatus = 0;
-
-        if ($isDryRun) {
-            if ($hasChangedFiles) {
-                $exitStatus |= self::EXIT_STATUS_FLAG_HAS_CHANGED_FILES;
-            }
-
-            if ($hasInvalidErrors) {
-                $exitStatus |= self::EXIT_STATUS_FLAG_HAS_INVALID_FILES;
-            }
-        }
-
-        if ($hasExceptionErrors) {
-            $exitStatus |= self::EXIT_STATUS_FLAG_EXCEPTION_IN_APP;
-        }
-
-        return $exitStatus;
-    }
 }

+ 57 - 0
src/Console/Command/FixCommandExitStatusCalculator.php

@@ -0,0 +1,57 @@
+<?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\Console\Command;
+
+/**
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
+ *
+ * @internal
+ */
+final class FixCommandExitStatusCalculator
+{
+    // Exit status 1 is reserved for environment constraints not matched.
+    const EXIT_STATUS_FLAG_HAS_INVALID_FILES = 4;
+    const EXIT_STATUS_FLAG_HAS_CHANGED_FILES = 8;
+    const EXIT_STATUS_FLAG_HAS_INVALID_CONFIG = 16;
+    const EXIT_STATUS_FLAG_HAS_INVALID_FIXER_CONFIG = 32;
+    const EXIT_STATUS_FLAG_EXCEPTION_IN_APP = 64;
+
+    /**
+     * @param bool $isDryRun
+     * @param bool $hasChangedFiles
+     * @param bool $hasInvalidErrors
+     * @param bool $hasExceptionErrors
+     *
+     * @return int
+     */
+    public function calculate($isDryRun, $hasChangedFiles, $hasInvalidErrors, $hasExceptionErrors)
+    {
+        $exitStatus = 0;
+
+        if ($isDryRun) {
+            if ($hasChangedFiles) {
+                $exitStatus |= self::EXIT_STATUS_FLAG_HAS_CHANGED_FILES;
+            }
+
+            if ($hasInvalidErrors) {
+                $exitStatus |= self::EXIT_STATUS_FLAG_HAS_INVALID_FILES;
+            }
+        }
+
+        if ($hasExceptionErrors) {
+            $exitStatus |= self::EXIT_STATUS_FLAG_EXCEPTION_IN_APP;
+        }
+
+        return $exitStatus;
+    }
+}

+ 3 - 1
src/Fixer/Alias/MbStrFunctionsFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\Alias;
 use PhpCsFixer\AbstractFunctionReferenceFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -84,6 +85,7 @@ $a = substr_count($a, $b);
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
+        $argumentsAnalyzer = new ArgumentsAnalyzer();
         foreach (self::$functions as $functionIdentity => $functionReplacement) {
             $currIndex = 0;
             while (null !== $currIndex) {
@@ -95,7 +97,7 @@ $a = substr_count($a, $b);
                 }
 
                 list($functionName, $openParenthesis, $closeParenthesis) = $boundaries;
-                $count = $this->countArguments($tokens, $openParenthesis, $closeParenthesis);
+                $count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis);
                 if (!in_array($count, $functionReplacement['argumentCount'], true)) {
                     continue 2;
                 }

+ 3 - 1
src/Fixer/Alias/PowToExponentiationFixer.php

@@ -16,6 +16,7 @@ use PhpCsFixer\AbstractFunctionReferenceFixer;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\VersionSpecification;
 use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
+use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
@@ -67,6 +68,7 @@ final class PowToExponentiationFixer extends AbstractFunctionReferenceFixer
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
         $candidates = $this->findPowCalls($tokens);
+        $argumentsAnalyzer = new ArgumentsAnalyzer();
 
         $numberOfTokensAdded = 0;
         $previousCloseParenthesisIndex = count($tokens);
@@ -82,7 +84,7 @@ final class PowToExponentiationFixer extends AbstractFunctionReferenceFixer
                 $numberOfTokensAdded = 0;
             }
 
-            $arguments = $this->getArguments($tokens, $candidate[1], $candidate[2]);
+            $arguments = $argumentsAnalyzer->getArguments($tokens, $candidate[1], $candidate[2]);
             if (2 !== count($arguments)) {
                 continue;
             }

+ 4 - 1
src/Fixer/Alias/RandomApiMigrationFixer.php

@@ -18,6 +18,7 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverRootless;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
@@ -84,6 +85,8 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
+        $argumentsAnalyzer = new ArgumentsAnalyzer();
+
         foreach ($this->configuration['replacements'] as $functionIdentity => $functionReplacement) {
             if ($functionIdentity === $functionReplacement['alternativeName']) {
                 continue;
@@ -99,7 +102,7 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
                 }
 
                 list($functionName, $openParenthesis, $closeParenthesis) = $boundaries;
-                $count = $this->countArguments($tokens, $openParenthesis, $closeParenthesis);
+                $count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis);
                 if (!in_array($count, $functionReplacement['argumentCount'], true)) {
                     continue 2;
                 }

+ 4 - 1
src/Fixer/CastNotation/ModernizeTypesCastingFixer.php

@@ -15,6 +15,7 @@ namespace PhpCsFixer\Fixer\CastNotation;
 use PhpCsFixer\AbstractFunctionReferenceFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -68,6 +69,8 @@ final class ModernizeTypesCastingFixer extends AbstractFunctionReferenceFixer
              'boolval' => array(T_BOOL_CAST, '(bool)'),
         );
 
+        $argumentsAnalyzer = new ArgumentsAnalyzer();
+
         foreach ($replacement as $functionIdentity => $newToken) {
             $currIndex = 0;
             while (null !== $currIndex) {
@@ -84,7 +87,7 @@ final class ModernizeTypesCastingFixer extends AbstractFunctionReferenceFixer
                 $currIndex = $openParenthesis;
 
                 // indicator that the function is overriden
-                if (1 !== $this->countArguments($tokens, $openParenthesis, $closeParenthesis)) {
+                if (1 !== $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis)) {
                     continue;
                 }
 

+ 4 - 1
src/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixer.php

@@ -21,6 +21,7 @@ use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -100,6 +101,8 @@ function f9(string $foo, $bar, $baz) {}',
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens)
     {
+        $argumentsAnalyzer = new ArgumentsAnalyzer();
+
         for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
             $mainIndex = $index;
             $token = $tokens[$index];
@@ -146,7 +149,7 @@ function f9(string $foo, $bar, $baz) {}',
 
             $arguments = array();
 
-            foreach ($this->getArguments($tokens, $openIndex, $index) as $start => $end) {
+            foreach ($argumentsAnalyzer->getArguments($tokens, $openIndex, $index) as $start => $end) {
                 $argumentInfo = $this->prepareArgumentInformation($tokens, $start, $end);
 
                 if (!$this->configuration['only_untyped'] || '' === $argumentInfo['type']) {

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