Browse Source

Merge branch 'master' into 3.0

* master:
  NoSuperfluousPhpdocTagsFixer,PhpdocAddMissingParamAnnotationFixer - p…
  CommentsAnalyzer - fix for declare before header comment
  FullyQualifiedStrictTypesFixer - Ignore partial class names which look like FQCNs
  LineEndingFixer - handle \"\r\r\n\"
  NativeConstantInvocationFixer - add "strict" flag
  When followed directly by a use declaration, the PhpDoc is most likely a file-level documentation block. Do not strip the newline after the documentation block in that case.

# Conflicts:
#	php-cs-fixer
#	tests/AutoReview/FixerFactoryTest.php
#	tests/Fixtures/Integration/priority/method_separation,indentation_type.test
SpacePossum 5 years ago
parent
commit
a9b077722d

+ 2 - 0
README.rst

@@ -929,6 +929,8 @@ Choose from the list of available rules:
   - ``include`` (``array``): list of additional constants to fix; defaults to ``[]``
   - ``scope`` (``'all'``, ``'namespaced'``): only fix constant invocations that are made
     within a namespace or fix all; defaults to ``'all'``
+  - ``strict`` (``bool``): whether leading ``\`` of constant invocation not meant to
+    have it should be removed; defaults to ``false``
 
 * **native_function_casing** [@Symfony, @PhpCsFixer]
 

+ 0 - 4
php-cs-fixer

@@ -11,10 +11,6 @@
  * with this source code in the file LICENSE.
  */
 
-/**
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- */
 error_reporting(-1);
 
 if (defined('HHVM_VERSION_ID')) {

+ 26 - 15
src/Fixer/ConstantNotation/NativeConstantInvocationFixer.php

@@ -225,14 +225,18 @@ namespace {
                 ->setAllowedValues(['all', 'namespaced'])
                 ->setDefault('all')
                 ->getOption(),
+            (new FixerOptionBuilder('strict', 'Whether leading `\` of constant invocation not meant to have it should be removed.'))
+                ->setAllowedTypes(['bool'])
+                ->setDefault(false) // @TODO: 3.0 change to true as default
+                ->getOption(),
         ]);
     }
 
     /**
-     * @param int $start
-     * @param int $end
+     * @param int $startIndex
+     * @param int $endIndex
      */
-    private function fixConstantInvocations(Tokens $tokens, $start, $end)
+    private function fixConstantInvocations(Tokens $tokens, $startIndex, $endIndex)
     {
         $useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens);
         $useConstantDeclarations = [];
@@ -244,8 +248,7 @@ namespace {
 
         $tokenAnalyzer = new TokensAnalyzer($tokens);
 
-        $indexes = [];
-        for ($index = $start; $index < $end; ++$index) {
+        for ($index = $endIndex; $index > $startIndex; --$index) {
             $token = $tokens[$index];
 
             // test if we are at a constant call
@@ -253,9 +256,27 @@ namespace {
                 continue;
             }
 
+            if (!$tokenAnalyzer->isConstantInvocation($index)) {
+                continue;
+            }
+
             $tokenContent = $token->getContent();
 
+            $prevIndex = $tokens->getPrevMeaningfulToken($index);
+
             if (!isset($this->constantsToEscape[$tokenContent]) && !isset($this->caseInsensitiveConstantsToEscape[strtolower($tokenContent)])) {
+                if (!$this->configuration['strict']) {
+                    continue;
+                }
+                if (!$tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
+                    continue;
+                }
+                $prevPrevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
+                if ($tokens[$prevPrevIndex]->isGivenKind(T_STRING)) {
+                    continue;
+                }
+                $tokens->clearTokenAndMergeSurroundingWhitespace($prevIndex);
+
                 continue;
             }
 
@@ -263,20 +284,10 @@ namespace {
                 continue;
             }
 
-            $prevIndex = $tokens->getPrevMeaningfulToken($index);
             if ($tokens[$prevIndex]->isGivenKind(T_NS_SEPARATOR)) {
                 continue;
             }
 
-            if (!$tokenAnalyzer->isConstantInvocation($index)) {
-                continue;
-            }
-
-            $indexes[] = $index;
-        }
-
-        $indexes = array_reverse($indexes);
-        foreach ($indexes as $index) {
             $tokens->insertAt($index, new Token([T_NS_SEPARATOR, '\\']));
         }
     }

+ 5 - 0
src/Fixer/Import/FullyQualifiedStrictTypesFixer.php

@@ -152,6 +152,11 @@ class SomeClass
         }
 
         $typeName = $type->getName();
+
+        if (0 !== strpos($typeName, '\\')) {
+            return;
+        }
+
         $shortType = (new TypeShortNameResolver())->resolve($tokens, $typeName);
         if ($shortType === $typeName) {
             return;

+ 11 - 11
src/Fixer/PhpTag/EchoTagSyntaxFixer.php

@@ -93,10 +93,10 @@ EOT
     public function isCandidate(Tokens $tokens)
     {
         if (self::FORMAT_SHORT === $this->configuration[self::OPTION_FORMAT]) {
-            return $tokens->isAnyTokenKindsFound([\T_ECHO, \T_PRINT]);
+            return $tokens->isAnyTokenKindsFound([T_ECHO, T_PRINT]);
         }
 
-        return $tokens->isTokenKindFound(\T_OPEN_TAG_WITH_ECHO);
+        return $tokens->isTokenKindFound(T_OPEN_TAG_WITH_ECHO);
     }
 
     /**
@@ -137,14 +137,14 @@ EOT
         $skipWhenComplexCode = $this->configuration[self::OPTION_SHORTEN_SIMPLE_STATEMENTS_ONLY];
         $count = $tokens->count();
         for ($index = 0; $index < $count; ++$index) {
-            if (!$tokens[$index]->isGivenKind(\T_OPEN_TAG)) {
+            if (!$tokens[$index]->isGivenKind(T_OPEN_TAG)) {
                 continue;
             }
             $nextMeaningful = $tokens->getNextMeaningfulToken($index);
             if (null === $nextMeaningful) {
                 return;
             }
-            if (!$tokens[$nextMeaningful]->isGivenKind([\T_ECHO, \T_PRINT])) {
+            if (!$tokens[$nextMeaningful]->isGivenKind([T_ECHO, T_PRINT])) {
                 $index = $nextMeaningful;
 
                 continue;
@@ -163,19 +163,19 @@ EOT
     private function shortToLong(Tokens $tokens)
     {
         if (self::LONG_FUNCTION_PRINT === $this->configuration[self::OPTION_LONG_FUNCTION]) {
-            $echoToken = [\T_PRINT, 'print'];
+            $echoToken = [T_PRINT, 'print'];
         } else {
-            $echoToken = [\T_ECHO, 'echo'];
+            $echoToken = [T_ECHO, 'echo'];
         }
         $index = -1;
         for (;;) {
-            $index = $tokens->getNextTokenOfKind($index, [[\T_OPEN_TAG_WITH_ECHO]]);
+            $index = $tokens->getNextTokenOfKind($index, [[T_OPEN_TAG_WITH_ECHO]]);
             if (null === $index) {
                 return;
             }
-            $replace = [new Token([\T_OPEN_TAG, '<?php ']), new Token($echoToken)];
+            $replace = [new Token([T_OPEN_TAG, '<?php ']), new Token($echoToken)];
             if (!$tokens[$index + 1]->isWhitespace()) {
-                $replace[] = new Token([\T_WHITESPACE, ' ']);
+                $replace[] = new Token([T_WHITESPACE, ' ']);
             }
             $tokens->overrideRange($index, $index, $replace);
             ++$index;
@@ -202,7 +202,7 @@ EOT
         $semicolonFound = false;
         for ($count = $tokens->count(); $index < $count; ++$index) {
             $token = $tokens[$index];
-            if ($token->isGivenKind(\T_CLOSE_TAG)) {
+            if ($token->isGivenKind(T_CLOSE_TAG)) {
                 return false;
             }
             if (';' === $token->getContent()) {
@@ -225,7 +225,7 @@ EOT
      */
     private function buildLongToShortTokens(Tokens $tokens, $openTagIndex, $echoTagIndex)
     {
-        $result = [new Token([\T_OPEN_TAG_WITH_ECHO, '<?='])];
+        $result = [new Token([T_OPEN_TAG_WITH_ECHO, '<?='])];
 
         $start = $tokens->getNextNonWhitespace($openTagIndex);
 

+ 1 - 0
src/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixer.php

@@ -79,6 +79,7 @@ class Bar {}
             T_CONTINUE,
             T_BREAK,
             T_DECLARE,
+            T_USE,
         ];
 
         foreach ($tokens as $index => $token) {

+ 2 - 1
src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php

@@ -94,7 +94,8 @@ class Foo {
      */
     public function getPriority()
     {
-        // should run before NoEmptyPhpdocFixer and after PhpdocToParamTypeFixer
+        // must be run before NoEmptyPhpdocFixer
+        // must be run after PhpdocAddMissingParamAnnotationFixer
         return 6;
     }
 

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

@@ -81,7 +81,7 @@ function f9(string $foo, $bar, $baz) {}
     public function getPriority()
     {
         // must be run after PhpdocNoAliasTagFixer
-        // must be run before PhpdocAlignFixer and PhpdocNoEmptyReturnFixer
+        // must be run before PhpdocAlignFixer, NoSuperfluousPhpdocTagsFixer and PhpdocNoEmptyReturnFixer
         return 10;
     }
 

+ 1 - 1
src/Fixer/StringNotation/ExplicitStringVariableFixer.php

@@ -112,7 +112,7 @@ EOT
 
                 ++$nextIndex;
             }
-            krsort($variableTokens, \SORT_NUMERIC);
+            krsort($variableTokens, SORT_NUMERIC);
 
             foreach ($variableTokens as $distinctVariableSet) {
                 if (1 === \count($distinctVariableSet['tokens'])) {

+ 2 - 2
src/Fixer/Whitespace/LineEndingFixer.php

@@ -67,7 +67,7 @@ final class LineEndingFixer extends AbstractFixer implements WhitespacesAwareFix
                     $tokens[$index] = new Token([
                         $token->getId(),
                         Preg::replace(
-                            "#\r\n|\n#",
+                            '#\R#',
                             $ending,
                             $token->getContent()
                         ),
@@ -81,7 +81,7 @@ final class LineEndingFixer extends AbstractFixer implements WhitespacesAwareFix
                 $tokens[$index] = new Token([
                     $token->getId(),
                     Preg::replace(
-                        "#\r\n|\n#",
+                        '#\R#',
                         $ending,
                         $token->getContent()
                     ),

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