Browse Source

Merge branch '2.2' into 2.7

# Conflicts:
#	src/Tokenizer/TokensAnalyzer.php
#	src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php
#	tests/Fixer/Basic/BracesFixerTest.php
#	tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php
#	tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
#	tests/Fixer/Operator/NewWithBracesFixerTest.php
#	tests/Tokenizer/Transformer/BraceClassInstantiationTransformerTest.php
Dariusz Ruminski 7 years ago
parent
commit
4514e5bea9

+ 1 - 1
README.rst

@@ -13,7 +13,7 @@ If you are already using a linter to identify coding standards problems in your
 code, you know that fixing them by hand is tedious, especially on large
 projects. This tool does not only detect them, but also fixes them for you.
 
-The PHP CS Fixer is maintained on github at https://github.com/FriendsOfPHP/PHP-CS-Fixer
+The PHP CS Fixer is maintained on GitHub at https://github.com/FriendsOfPHP/PHP-CS-Fixer
 bug reports and ideas about new features are welcome there.
 
 You can talk to us at https://gitter.im/PHP-CS-Fixer/Lobby about the project,

+ 2 - 2
doc/COOKBOOK-FIXERS.md

@@ -28,7 +28,7 @@ classes.
 ## Assumptions
 
 * You are familiar with Test Driven Development.
-* Forked FriendsOfPHP/PHP-CS-Fixer into your own Github Account.
+* Forked FriendsOfPHP/PHP-CS-Fixer into your own GitHub Account.
 * Cloned your forked repository locally.
 * Installed the dependencies of PHP CS Fixer using [Composer](https://getcomposer.org/).
 * You have read [`CONTRIBUTING.md`](/CONTRIBUTING.md).
@@ -428,7 +428,7 @@ This will fix all the coding style mistakes.
 
 After the final CS fix, you are ready to commit. Do it.
 
-Now, go to Github and open a Pull Request.
+Now, go to GitHub and open a Pull Request.
 
 
 ### Step 5 - Peer review: it is all about code and community building.

+ 7 - 6
src/AbstractLinesBeforeNamespaceFixer.php

@@ -27,13 +27,14 @@ use PhpCsFixer\Tokenizer\Tokens;
 abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
 {
     /**
-     * Make sure the expected number of new lines prefix a namespace.
+     * Make sure # of line breaks prefixing namespace is within given range.
      *
      * @param Tokens $tokens
      * @param int    $index
-     * @param int    $expected
+     * @param int    $expectedMin min. # of line breaks
+     * @param int    $expectedMax max. # of line breaks
      */
-    protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expected)
+    protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expectedMin, $expectedMax)
     {
         // Let's determine the total numbers of new lines before the namespace
         // and the opening token
@@ -60,14 +61,14 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implement
             }
         }
 
-        if ($expected === $precedingNewlines) {
+        if ($precedingNewlines >= $expectedMin && $precedingNewlines <= $expectedMax) {
             return;
         }
 
         $previousIndex = $index - 1;
         $previous = $tokens[$previousIndex];
 
-        if (0 === $expected) {
+        if (0 === $expectedMax) {
             // Remove all the previous new lines
             if ($previous->isWhitespace()) {
                 $tokens->clearAt($previousIndex);
@@ -81,7 +82,7 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implement
         }
 
         $lineEnding = $this->whitespacesConfig->getLineEnding();
-        $newlinesForWhitespaceToken = $expected;
+        $newlinesForWhitespaceToken = $expectedMax;
         if (null !== $openingToken) {
             // Use the configured line ending for the PHP opening tag
             $content = rtrim($openingToken->getContent());

+ 1 - 1
src/Console/Command/ReadmeCommand.php

@@ -58,7 +58,7 @@ If you are already using a linter to identify coding standards problems in your
 code, you know that fixing them by hand is tedious, especially on large
 projects. This tool does not only detect them, but also fixes them for you.
 
-The PHP CS Fixer is maintained on github at https://github.com/FriendsOfPHP/PHP-CS-Fixer
+The PHP CS Fixer is maintained on GitHub at https://github.com/FriendsOfPHP/PHP-CS-Fixer
 bug reports and ideas about new features are welcome there.
 
 You can talk to us at https://gitter.im/PHP-CS-Fixer/Lobby about the project,

+ 1 - 1
src/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixer.php

@@ -57,7 +57,7 @@ final class NoBlankLinesBeforeNamespaceFixer extends AbstractLinesBeforeNamespac
                 continue;
             }
 
-            $this->fixLinesBeforeNamespace($tokens, $index, 1);
+            $this->fixLinesBeforeNamespace($tokens, $index, 0, 1);
         }
     }
 }

+ 1 - 1
src/Fixer/NamespaceNotation/SingleBlankLineBeforeNamespaceFixer.php

@@ -53,7 +53,7 @@ final class SingleBlankLineBeforeNamespaceFixer extends AbstractLinesBeforeNames
             $token = $tokens[$index];
 
             if ($token->isGivenKind(T_NAMESPACE)) {
-                $this->fixLinesBeforeNamespace($tokens, $index, 2);
+                $this->fixLinesBeforeNamespace($tokens, $index, 2, 2);
             }
         }
     }

+ 1 - 0
src/Tokenizer/TokensAnalyzer.php

@@ -458,6 +458,7 @@ final class TokensAnalyzer
                 T_SR => true,                   // >>
                 T_SR_EQUAL => true,             // >>=
                 T_XOR_EQUAL => true,            // ^=
+                CT::T_TYPE_ALTERNATION => true, // |
             ];
 
             if (defined('T_SPACESHIP')) {

+ 12 - 4
src/Tokenizer/Transformer/BraceClassInstantiationTransformer.php

@@ -60,14 +60,22 @@ final class BraceClassInstantiationTransformer extends AbstractTransformer
         if (!$tokens[$index]->equals('(') || !$tokens[$tokens->getNextMeaningfulToken($index)]->equals([T_NEW])) {
             return;
         }
+
         if ($tokens[$tokens->getPrevMeaningfulToken($index)]->equalsAny([
-            [T_STRING],
             ']',
-            [CT::T_ARRAY_SQUARE_BRACE_CLOSE],
             [CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE],
-            [T_VARIABLE],
-            [T_CLASS],
+            [CT::T_ARRAY_SQUARE_BRACE_CLOSE],
             [T_ARRAY],
+            [T_CLASS],
+            [T_ELSEIF],
+            [T_FOR],
+            [T_FOREACH],
+            [T_IF],
+            [T_STATIC],
+            [T_STRING],
+            [T_SWITCH],
+            [T_VARIABLE],
+            [T_WHILE],
         ])) {
             return;
         }

+ 21 - 0
tests/Fixer/Basic/BracesFixerTest.php

@@ -785,6 +785,27 @@ function test()
 //        return true;
 //    };
     $a = 3;
+}',
+            ],
+            [
+                '<?php
+class Foo
+{
+    public function bar()
+    {
+        foreach (new Bar() as $file) {
+            foo();
+        }
+    }
+}',
+                '<?php
+class Foo {
+    public function bar() {
+        foreach (new Bar() as $file)
+        {
+            foo();
+        }
+    }
 }',
             ],
         ];

+ 1 - 0
tests/Fixer/NamespaceNotation/NoBlankLinesBeforeNamespaceFixerTest.php

@@ -46,6 +46,7 @@ final class NoBlankLinesBeforeNamespaceFixerTest extends AbstractFixerTestCase
     public function provideFixCases()
     {
         return [
+            ['<?php namespace Some\Name\Space;'],
             ["<?php\nnamespace X;"],
             ["<?php\nnamespace X;", "<?php\n\n\n\nnamespace X;"],
             ["<?php\r\nnamespace X;"],

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