Browse Source

PHP8.1 - Enum support

SpacePossum 3 years ago
parent
commit
4cb265cd5d

+ 1 - 1
src/Cache/Cache.php

@@ -27,7 +27,7 @@ final class Cache implements CacheInterface
     private $signature;
 
     /**
-     * @var array
+     * @var array<string, int>
      */
     private $hashes = [];
 

+ 7 - 0
src/Console/Command/DocumentationCommand.php

@@ -15,6 +15,7 @@ declare(strict_types=1);
 namespace PhpCsFixer\Console\Command;
 
 use PhpCsFixer\Documentation\DocumentationGenerator;
+use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\FixerFactory;
 use PhpCsFixer\RuleSet\RuleSets;
 use Symfony\Component\Console\Command\Command;
@@ -68,6 +69,9 @@ final class DocumentationCommand extends Command
         return 0;
     }
 
+    /**
+     * @param FixerInterface[] $fixers
+     */
     private function generateFixersDocs(array $fixers): void
     {
         $filesystem = new Filesystem();
@@ -102,6 +106,9 @@ final class DocumentationCommand extends Command
         }
     }
 
+    /**
+     * @param FixerInterface[] $fixers
+     */
     private function generateRuleSetsDocs(array $fixers): void
     {
         $filesystem = new Filesystem();

+ 2 - 3
src/Documentation/DocumentationGenerator.php

@@ -14,7 +14,6 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Documentation;
 
-use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\Console\Command\HelpCommand;
 use PhpCsFixer\Differ\FullDiffer;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
@@ -67,7 +66,7 @@ final class DocumentationGenerator
     }
 
     /**
-     * @param AbstractFixer[] $fixers
+     * @param FixerInterface[] $fixers
      */
     public function generateFixersDocumentationIndex(array $fixers): string
     {
@@ -340,7 +339,7 @@ RST;
     }
 
     /**
-     * @param AbstractFixer[] $fixers
+     * @param FixerInterface[] $fixers
      */
     public function generateRuleSetsDocumentation(RuleSetDescriptionInterface $definition, array $fixers): string
     {

+ 2 - 0
src/Fixer/Alias/BacktickToShellExecFixer.php

@@ -94,6 +94,8 @@ EOT
 
     /**
      * Override backtick code with corresponding double-quoted string.
+     *
+     * @param array<int, Token> $backtickTokens
      */
     private function fixBackticks(Tokens $tokens, array $backtickTokens): void
     {

+ 1 - 1
src/Fixer/ControlStructure/NoBreakCommentFixer.php

@@ -217,7 +217,7 @@ switch ($foo) {
 
         $text = preg_quote($this->configuration['comment_text'], '~');
 
-        return 1 === Preg::match("~^((//|#)\\s*{$text}\\s*)|(/\\*\\*?\\s*{$text}\\s*\\*/)$~i", $token->getContent());
+        return 1 === Preg::match("~^((//|#)\\s*{$text}\\s*)|(/\\*\\*?\\s*{$text}(\\s+.*)*\\*/)$~i", $token->getContent());
     }
 
     private function insertCommentAt(Tokens $tokens, int $casePosition): void

+ 10 - 41
src/Fixer/ControlStructure/SwitchCaseSemicolonToColonFixer.php

@@ -18,6 +18,8 @@ use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
+use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -71,55 +73,22 @@ final class SwitchCaseSemicolonToColonFixer extends AbstractFixer
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
-        foreach ($tokens as $index => $token) {
-            if ($token->isGivenKind(T_CASE)) {
-                $this->fixSwitchCase($tokens, $index);
-            }
-            if ($token->isGivenKind(T_DEFAULT)) {
-                $this->fixSwitchDefault($tokens, $index);
-            }
-        }
-    }
+        /** @var SwitchAnalysis $analysis */
+        foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
+            $default = $analysis->getDefaultAnalysis();
 
-    private function fixSwitchCase(Tokens $tokens, int $index): void
-    {
-        $ternariesCount = 0;
-        do {
-            if ($tokens[$index]->equalsAny(['(', '{'])) { // skip constructs
-                $type = Tokens::detectBlockType($tokens[$index]);
-                $index = $tokens->findBlockEnd($type['type'], $index);
-
-                continue;
+            if (null !== $default) {
+                $this->fixTokenIfNeeded($tokens, $default->getColonIndex());
             }
 
-            if ($tokens[$index]->equals('?')) {
-                ++$ternariesCount;
-
-                continue;
+            foreach ($analysis->getCases() as $caseAnalysis) {
+                $this->fixTokenIfNeeded($tokens, $caseAnalysis->getColonIndex());
             }
-
-            if ($tokens[$index]->equalsAny([':', ';'])) {
-                if (0 === $ternariesCount) {
-                    break;
-                }
-
-                --$ternariesCount;
-            }
-        } while (++$index);
-
-        if ($tokens[$index]->equals(';')) {
-            $tokens[$index] = new Token(':');
         }
     }
 
-    private function fixSwitchDefault(Tokens $tokens, int $index): void
+    private function fixTokenIfNeeded(Tokens $tokens, int $index): void
     {
-        do {
-            if ($tokens[$index]->equalsAny([':', ';', [T_DOUBLE_ARROW]])) {
-                break;
-            }
-        } while (++$index);
-
         if ($tokens[$index]->equals(';')) {
             $tokens[$index] = new Token(':');
         }

+ 20 - 21
src/Fixer/ControlStructure/SwitchCaseSpaceFixer.php

@@ -18,6 +18,8 @@ use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
+use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer;
 use PhpCsFixer\Tokenizer\Tokens;
 
 /**
@@ -62,34 +64,31 @@ final class SwitchCaseSpaceFixer extends AbstractFixer
      */
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
-        foreach ($tokens as $index => $token) {
-            if (!$token->isGivenKind([T_CASE, T_DEFAULT])) {
-                continue;
-            }
+        /** @var SwitchAnalysis $analysis */
+        foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
+            $default = $analysis->getDefaultAnalysis();
+
+            if (null !== $default) {
+                $index = $default->getIndex();
 
-            $ternariesCount = 0;
-            for ($colonIndex = $index + 1;; ++$colonIndex) {
-                // We have to skip ternary case for colons.
-                if ($tokens[$colonIndex]->equals('?')) {
-                    ++$ternariesCount;
+                if (!$tokens[$index + 1]->isWhitespace() || !$tokens[$index + 2]->equalsAny([':', ';'])) {
+                    continue;
                 }
 
-                if ($tokens[$colonIndex]->equalsAny([':', ';'])) {
-                    if (0 === $ternariesCount) {
-                        break;
-                    }
+                $tokens->clearAt($index + 1);
+            }
+
+            foreach ($analysis->getCases() as $caseAnalysis) {
+                $colonIndex = $caseAnalysis->getColonIndex();
+                $valueIndex = $tokens->getPrevNonWhitespace($colonIndex);
 
-                    --$ternariesCount;
+                // skip if there is no space between the colon and previous token or is space after comment
+                if ($valueIndex === $colonIndex - 1 || $tokens[$valueIndex]->isComment()) {
+                    continue;
                 }
-            }
 
-            $valueIndex = $tokens->getPrevNonWhitespace($colonIndex);
-            // skip if there is no space between the colon and previous token or is space after comment
-            if ($valueIndex === $colonIndex - 1 || $tokens[$valueIndex]->isComment()) {
-                continue;
+                $tokens->clearAt($valueIndex + 1);
             }
-
-            $tokens->clearAt($valueIndex + 1);
         }
     }
 }

+ 16 - 20
src/Fixer/Operator/OperatorLinebreakFixer.php

@@ -23,10 +23,10 @@ use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
 use PhpCsFixer\Preg;
-use PhpCsFixer\Tokenizer\Analyzer\Analysis\CaseAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer;
 use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;
 use PhpCsFixer\Tokenizer\Analyzer\ReferenceAnalyzer;
-use PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -165,27 +165,23 @@ function foo() {
      */
     private function getExcludedIndices(Tokens $tokens): array
     {
-        $indices = [];
-        for ($index = $tokens->count() - 1; $index > 0; --$index) {
-            if ($tokens[$index]->isGivenKind(T_SWITCH)) {
-                $indices = array_merge($indices, $this->getCasesColonsForSwitch($tokens, $index));
+        $colonIndexes = [];
+
+        foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
+            foreach ($analysis->getCases() as $case) {
+                $colonIndexes[] = $case->getColonIndex();
             }
-        }
 
-        return $indices;
-    }
+            if ($analysis instanceof SwitchAnalysis) {
+                $defaultAnalysis = $analysis->getDefaultAnalysis();
 
-    /**
-     * @return int[]
-     */
-    private function getCasesColonsForSwitch(Tokens $tokens, int $switchIndex): array
-    {
-        return array_map(
-            static function (CaseAnalysis $caseAnalysis): int {
-                return $caseAnalysis->getColonIndex();
-            },
-            (new SwitchAnalyzer())->getSwitchAnalysis($tokens, $switchIndex)->getCases()
-        );
+                if (null !== $defaultAnalysis) {
+                    $colonIndexes[] = $defaultAnalysis->getColonIndex();
+                }
+            }
+        }
+
+        return $colonIndexes;
     }
 
     /**

+ 21 - 16
src/Fixer/Operator/TernaryOperatorSpacesFixer.php

@@ -18,9 +18,9 @@ use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
-use PhpCsFixer\Tokenizer\Analyzer\Analysis\CaseAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\Analysis\SwitchAnalysis;
+use PhpCsFixer\Tokenizer\Analyzer\ControlCaseStructuresAnalyzer;
 use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;
-use PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 
@@ -65,15 +65,9 @@ final class TernaryOperatorSpacesFixer extends AbstractFixer
     {
         $gotoLabelAnalyzer = new GotoLabelAnalyzer();
         $ternaryOperatorIndices = [];
-        $excludedIndices = [];
+        $excludedIndices = $this->getColonIndicesForSwitch($tokens);
 
         foreach ($tokens as $index => $token) {
-            if ($token->isGivenKind(T_SWITCH)) {
-                $excludedIndices = array_merge($excludedIndices, $this->getColonIndicesForSwitch($tokens, $index));
-
-                continue;
-            }
-
             if (!$token->equalsAny(['?', ':'])) {
                 continue;
             }
@@ -151,14 +145,25 @@ final class TernaryOperatorSpacesFixer extends AbstractFixer
     /**
      * @return int[]
      */
-    private function getColonIndicesForSwitch(Tokens $tokens, int $switchIndex): array
+    private function getColonIndicesForSwitch(Tokens $tokens): array
     {
-        return array_map(
-            static function (CaseAnalysis $caseAnalysis): int {
-                return $caseAnalysis->getColonIndex();
-            },
-            (new SwitchAnalyzer())->getSwitchAnalysis($tokens, $switchIndex)->getCases()
-        );
+        $colonIndexes = [];
+
+        foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
+            foreach ($analysis->getCases() as $case) {
+                $colonIndexes[] = $case->getColonIndex();
+            }
+
+            if ($analysis instanceof SwitchAnalysis) {
+                $defaultAnalysis = $analysis->getDefaultAnalysis();
+
+                if (null !== $defaultAnalysis) {
+                    $colonIndexes[] = $defaultAnalysis->getColonIndex();
+                }
+            }
+        }
+
+        return $colonIndexes;
     }
 
     private function ensureWhitespaceExistence(Tokens $tokens, int $index, bool $after): void

+ 58 - 0
src/Tokenizer/Analyzer/Analysis/AbstractControlCaseStructuresAnalysis.php

@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+/*
+ * 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\Tokenizer\Analyzer\Analysis;
+
+/**
+ * @internal
+ */
+abstract class AbstractControlCaseStructuresAnalysis
+{
+    /**
+     * @var int
+     */
+    private $index;
+
+    /**
+     * @var int
+     */
+    private $open;
+
+    /**
+     * @var int
+     */
+    private $close;
+
+    public function __construct(int $index, int $open, int $close)
+    {
+        $this->index = $index;
+        $this->open = $open;
+        $this->close = $close;
+    }
+
+    public function getIndex(): int
+    {
+        return $this->index;
+    }
+
+    public function getOpenIndex(): int
+    {
+        return $this->open;
+    }
+
+    public function getCloseIndex(): int
+    {
+        return $this->close;
+    }
+}

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