Browse Source

Merge branch '2.15'

Dariusz Ruminski 5 years ago
parent
commit
6d8415807e

+ 2 - 2
src/Console/Output/ProcessOutput.php

@@ -30,8 +30,8 @@ final class ProcessOutput implements ProcessOutputInterface
      */
     private static $eventStatusMap = [
         FixerFileProcessedEvent::STATUS_UNKNOWN => ['symbol' => '?', 'format' => '%s', 'description' => 'unknown'],
-        FixerFileProcessedEvent::STATUS_INVALID => ['symbol' => 'I', 'format' => '<bg=red>%s</bg=red>', 'description' => 'invalid file syntax, file ignored'],
-        FixerFileProcessedEvent::STATUS_SKIPPED => ['symbol' => 'S', 'format' => '<fg=cyan>%s</fg=cyan>', 'description' => 'Skipped'],
+        FixerFileProcessedEvent::STATUS_INVALID => ['symbol' => 'I', 'format' => '<bg=red>%s</bg=red>', 'description' => 'invalid file syntax (file ignored)'],
+        FixerFileProcessedEvent::STATUS_SKIPPED => ['symbol' => 'S', 'format' => '<fg=cyan>%s</fg=cyan>', 'description' => 'skipped (cached or empty file)'],
         FixerFileProcessedEvent::STATUS_NO_CHANGES => ['symbol' => '.', 'format' => '%s', 'description' => 'no changes'],
         FixerFileProcessedEvent::STATUS_FIXED => ['symbol' => 'F', 'format' => '<fg=green>%s</fg=green>', 'description' => 'fixed'],
         FixerFileProcessedEvent::STATUS_EXCEPTION => ['symbol' => 'E', 'format' => '<bg=red>%s</bg=red>', 'description' => 'error'],

+ 7 - 2
src/Fixer/Basic/BracesFixer.php

@@ -917,11 +917,16 @@ class Foo
         $nextToken = $tokens[$nextTokenIndex];
         if ($nextToken->isComment()) {
             $previousToken = $tokens[$nextTokenIndex - 1];
+            $nextTokenContent = $nextToken->getContent();
 
             // do not indent inline comments used to comment out unused code
             if (
-                (0 === strpos($nextToken->getContent(), '//'.$this->whitespacesConfig->getIndent()) || '//' === $nextToken->getContent())
-                && $previousToken->isWhitespace() && 1 === Preg::match('/\R$/', $previousToken->getContent())
+                $previousToken->isWhitespace()
+                && 1 === Preg::match('/\R$/', $previousToken->getContent())
+                && (
+                    (0 === strpos($nextTokenContent, '//'.$this->whitespacesConfig->getIndent()) || '//' === $nextTokenContent)
+                    || (0 === strpos($nextTokenContent, '#'.$this->whitespacesConfig->getIndent()) || '#' === $nextTokenContent)
+                )
             ) {
                 return;
             }

+ 8 - 6
src/Fixer/Import/NoUnusedImportsFixer.php

@@ -90,7 +90,7 @@ final class NoUnusedImportsFixer extends AbstractFixer
         foreach ((new NamespacesAnalyzer())->getDeclarations($tokens) as $namespace) {
             $currentNamespaceUseDeclarations = array_filter(
                 $useDeclarations,
-                function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
+                static function (NamespaceUseAnalysis $useDeclaration) use ($namespace) {
                     return
                         $useDeclaration->getStartIndex() >= $namespace->getScopeStartIndex()
                         && $useDeclaration->getEndIndex() <= $namespace->getScopeEndIndex()
@@ -143,7 +143,7 @@ final class NoUnusedImportsFixer extends AbstractFixer
 
                 if (
                     0 === strcasecmp($shortName, $token->getContent())
-                    && !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR])
+                    && !$prevMeaningfulToken->isGivenKind([T_NS_SEPARATOR, T_CONST, T_OBJECT_OPERATOR, T_DOUBLE_COLON])
                 ) {
                     return true;
                 }
@@ -151,10 +151,12 @@ final class NoUnusedImportsFixer extends AbstractFixer
                 continue;
             }
 
-            if ($token->isComment() && Preg::match(
-                '/(?<![[:alnum:]])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
-                $token->getContent()
-            )) {
+            if ($token->isComment()
+                && Preg::match(
+                    '/(?<![[:alnum:]])(?<!\\\\)'.$shortName.'(?![[:alnum:]])/i',
+                    $token->getContent()
+                )
+            ) {
                 return true;
             }
         }

+ 195 - 177
tests/Fixer/Basic/BracesFixerTest.php

@@ -762,30 +762,6 @@ function foo()
             ],
             [
                 '<?php
-function test()
-{
-//    $closure = function ($callback) use ($query) {
-//        doSomething();
-//
-//        return true;
-//    };
-    $a = 3;
-}',
-            ],
-            [
-                '<?php
-function test()
-{
-//    $closure = function ($callback) use ($query) {
-//        doSomething();
-//        '.'
-//        return true;
-//    };
-    $a = 3;
-}',
-            ],
-            [
-                '<?php
 class Foo
 {
     public function bar()
@@ -2589,159 +2565,6 @@ function D() /**
 }',
                 self::$configurationOopPositionSameLine,
             ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-
-//    if ($bar === \'bar\') {
-//        return [];
-//    }
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-
-//    if ($bar === \'bar\') {
-    //        return [];
-//    }
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-
-//    if ($bar === \'bar\') {
-//        return [];
-//    }
-    '.'
-    $bar = \'bar\';
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-
-//    bar();
-    '.'
-    $bar = \'bar\';
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-//    bar();
-    '.'
-    $bar = \'bar\';
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-    '.'
-//    bar();
-    $bar = \'bar\';
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-if ($foo) {
-    foo();
-    '.'
-//    bar();
-} else {
-    bar();
-}
-',
-            ],
-            [
-                '<?php
-function foo()
-{
-    $a = 1;
-    // we will return sth
-    return $a;
-}
-',
-                '<?php
-function foo()
-{
-    $a = 1;
-// we will return sth
-    return $a;
-}
-',
-            ],
-            [
-                '<?php
-function foo()
-{
-    $a = 1;
-    '.'
-//    bar();
-    // we will return sth
-    return $a;
-}
-',
-                '<?php
-function foo()
-{
-    $a = 1;
-    '.'
-//    bar();
-// we will return sth
-    return $a;
-}
-',
-            ],
-            [
-                '<?php
-function foo()
-{
-    $a = 1;
-//    if ($a === \'bar\') {
-//        return [];
-//    }
-    // we will return sth
-    return $a;
-}
-',
-                '<?php
-function foo()
-{
-    $a = 1;
-//    if ($a === \'bar\') {
-//        return [];
-//    }
-// we will return sth
-    return $a;
-}
-',
-            ],
         ];
     }
 
@@ -5230,4 +5053,199 @@ EOT
             ],
         ];
     }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixCommentsCases
+     */
+    public function testFixComments($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+        $this->doTest(str_replace('//', '#', $expected), null === $input ? null : str_replace('//', '#', $input));
+    }
+
+    public function provideFixCommentsCases()
+    {
+        return [
+            [
+                '<?php
+function test()
+{
+//    $closure = function ($callback) use ($query) {
+//        doSomething();
+//
+//        return true;
+//    };
+    $a = 3;
+}',
+            ],
+            [
+                '<?php
+function test()
+{
+//    $closure = function ($callback) use ($query) {
+//        doSomething();
+//        '.'
+//        return true;
+//    };
+    $a = 3;
+}',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+
+//    if ($bar === \'bar\') {
+//        return [];
+//    }
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+
+//    if ($bar === \'bar\') {
+    //        return [];
+//    }
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+
+//    if ($bar === \'bar\') {
+//        return [];
+//    }
+    '.'
+    $bar = \'bar\';
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+
+//    bar();
+    '.'
+    $bar = \'bar\';
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+//    bar();
+    '.'
+    $bar = \'bar\';
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+    '.'
+//    bar();
+    $bar = \'bar\';
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+if ($foo) {
+    foo();
+    '.'
+//    bar();
+} else {
+    bar();
+}
+',
+            ],
+            [
+                '<?php
+function foo()
+{
+    $a = 1;
+    // we will return sth
+    return $a;
+}
+',
+                '<?php
+function foo()
+{
+    $a = 1;
+// we will return sth
+    return $a;
+}
+',
+            ],
+            [
+                '<?php
+function foo()
+{
+    $a = 1;
+    '.'
+//    bar();
+    // we will return sth
+    return $a;
+}
+',
+                '<?php
+function foo()
+{
+    $a = 1;
+    '.'
+//    bar();
+// we will return sth
+    return $a;
+}
+',
+            ],
+            [
+                '<?php
+function foo()
+{
+    $a = 1;
+//    if ($a === \'bar\') {
+//        return [];
+//    }
+    // we will return sth
+    return $a;
+}
+',
+                '<?php
+function foo()
+{
+    $a = 1;
+//    if ($a === \'bar\') {
+//        return [];
+//    }
+// we will return sth
+    return $a;
+}
+',
+            ],
+        ];
+    }
 }

+ 7 - 1
tests/Fixer/Import/NoUnusedImportsFixerTest.php

@@ -506,12 +506,15 @@ $bar = null;
 EOF
                 ,
             ],
-            'property_name' => [
+            'property name, method name, static method call, static property' => [
                 <<<'EOF'
 <?php
 
 
 $foo->bar = null;
+$foo->bar();
+$foo::bar();
+$foo::bar;
 EOF
                 ,
                 <<<'EOF'
@@ -520,6 +523,9 @@ EOF
 use Foo\Bar;
 
 $foo->bar = null;
+$foo->bar();
+$foo::bar();
+$foo::bar;
 EOF
                 ,
             ],

+ 1 - 1
tests/Smoke/CiIntegrationTest.php

@@ -153,7 +153,7 @@ If you need help while solving warnings, ask at https://gitter.im/PHP-CS-Fixer,
             preg_quote($optionalXdebugWarning, '/'),
             preg_quote('Loaded config default from ".php_cs.dist".', '/'),
             \strlen($expectedResult3Files),
-            preg_quote('Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error', '/')
+            preg_quote('Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error', '/')
         );
 
         static::assertRegExp($pattern, $result3->getError());