Browse Source

Add SwitchCaseSemicolonToColonFixer.

Possum 9 years ago
parent
commit
73780a8bab

+ 4 - 0
README.rst

@@ -284,6 +284,10 @@ Choose from the list of available fixers:
                         one blank line after the use
                         statements block.
 
+* **switch_case_semicolon_to_colon** [PSR-2]
+                        A case should be followed by a
+                        colon and not a semicolon.
+
 * **switch_case_space** [PSR-2]
                         Removes extra spaces between
                         colon and case value.

+ 2 - 0
Symfony/CS/Fixer/PSR2/EofEndingFixer.php

@@ -16,6 +16,8 @@ use Symfony\CS\Tokenizer\Token;
 use Symfony\CS\Tokenizer\Tokens;
 
 /**
+ * Fixer for rules defined in PSR2 ¶2.2.
+ *
  * @author Fabien Potencier <fabien@symfony.com>
  */
 class EofEndingFixer extends AbstractFixer

+ 67 - 0
Symfony/CS/Fixer/PSR2/SwitchCaseSemicolonToColonFixer.php

@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * This file is part of the PHP CS utility.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\CS\Fixer\PSR2;
+
+use Symfony\CS\AbstractFixer;
+use Symfony\CS\Tokenizer\Tokens;
+
+/**
+ * Fixer for rules defined in PSR2 ¶5.2.
+ *
+ * @author SpacePossum
+ */
+final class SwitchCaseSemicolonToColonFixer extends AbstractFixer
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function fix(\SplFileInfo $file, $content)
+    {
+        $tokens = Tokens::fromCode($content);
+
+        foreach ($tokens as $index => $token) {
+            if (!$token->isGivenKind(array(T_CASE, T_DEFAULT))) {
+                continue;
+            }
+
+            $ternariesCount = 0;
+            for ($colonIndex = $index + 1; ; ++$colonIndex) {
+                // We have to skip ternary case for colons.
+                if ($tokens[$colonIndex]->equals('?')) {
+                    ++$ternariesCount;
+                }
+
+                if ($tokens[$colonIndex]->equalsAny(array(':', ';'))) {
+                    if (0 === $ternariesCount) {
+                        break;
+                    }
+
+                    --$ternariesCount;
+                }
+            }
+
+            if ($tokens[$colonIndex]->equals(';')) {
+                $tokens[$colonIndex]->setContent(':');
+            }
+        }
+
+        return $tokens->generateCode();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDescription()
+    {
+        return 'A case should be followed by a colon and not a semicolon.';
+    }
+}

+ 2 - 0
Symfony/CS/Fixer/PSR2/SwitchCaseSpaceFixer.php

@@ -15,6 +15,8 @@ use Symfony\CS\AbstractFixer;
 use Symfony\CS\Tokenizer\Tokens;
 
 /**
+ * Fixer for rules defined in PSR2 ¶5.2.
+ *
  * @author Sullivan Senechal <soullivaneuh@gmail.com>
  */
 final class SwitchCaseSpaceFixer extends AbstractFixer

+ 1 - 1
Symfony/CS/Fixer/Symfony/DuplicateSemicolonFixer.php

@@ -67,7 +67,7 @@ class DuplicateSemicolonFixer extends AbstractFixer
      */
     public function getPriority()
     {
-        // should be run before the BracesFixer, SpacesBeforeSemicolonFixer and MultilineSpacesBeforeSemicolonFixer
+        // should be run before the BracesFixer, SpacesBeforeSemicolonFixer, MultilineSpacesBeforeSemicolonFixer and SwitchCaseSemicolonToColonFixer.
         return 10;
     }
 }

+ 194 - 0
Symfony/CS/Tests/Fixer/PSR2/SwitchCaseSemicolonToColonFixerTest.php

@@ -0,0 +1,194 @@
+<?php
+
+/*
+ * This file is part of the PHP CS utility.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\CS\Tests\Fixer\PSR2;
+
+use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
+
+/**
+ * @author SpacePossum
+ *
+ * @internal
+ */
+final class SwitchCaseSemicolonToColonFixerTest extends AbstractFixerTestBase
+{
+    /**
+     * @dataProvider provideFixCases
+     */
+    public function testFix($expected, $input = null)
+    {
+        $this->makeTest($expected, $input);
+    }
+
+    public function provideFixCases()
+    {
+        return array(
+            array(
+                '<?php
+                switch ($a) {
+                    case 42:
+                        break;
+                }
+                ',
+                '<?php
+                switch ($a) {
+                    case 42;
+                        break;
+                }
+                ',
+            ),
+            array(
+                '<?php
+                    switch ($a) {
+                        case 42:
+                            break;
+                        case 1:
+                            switch ($a) {
+                                case 42:
+                                    break;
+                                default :
+                                    echo 1;
+                            }
+                    }',
+                '<?php
+                    switch ($a) {
+                        case 42;
+                            break;
+                        case 1:
+                            switch ($a) {
+                                case 42;
+                                    break;
+                                default ;
+                                    echo 1;
+                            }
+                    }',
+            ),
+            array(
+                '<?php
+                switch ($a) {
+                    case 42:;;// DuplicateSemicolonFixer should clean this up (partly)
+                        break;
+                }
+                ',
+                '<?php
+                switch ($a) {
+                    case 42;;;// DuplicateSemicolonFixer should clean this up (partly)
+                        break;
+                }
+                ',
+            ),
+            array(
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "d" :
+                        break;
+                }
+                ',
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "d" ;
+                        break;
+                }
+                ',
+            ),
+            array(
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "d": break;
+                }
+                ',
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "d"; break;
+                }
+                ',
+            ),
+            array(
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "this" ? "is" : "ugly":
+                        break;
+                }
+                ',
+                '<?php
+                switch ($a) {
+                    case $b ? "c" : "this" ? "is" : "ugly";
+                        break;
+                }
+                ',
+            ),
+            array(
+                '<?php
+                switch($a) {
+                    case (int) $a < 1: {
+                        echo "leave ; alone";
+                        break;
+                    }
+                    case ($a < 2)/* test */ : {
+                        echo "fix 1";
+                        break;
+                    }
+                    case (3):{
+                        echo "fix 2";
+                        break;
+                    }
+                    case /**/(/**/ // test
+                        4
+                        /**/)//
+                        /**/: {
+                        echo "fix 3";
+                        break;
+                    }
+                    case (((int)$b) + 4.1) : {
+                        echo "fix 4";
+                        break;
+                    }
+                    case ($b + 1) * 2 : {;;
+                        echo "leave alone";
+                        break;
+                    }
+                }
+                ',
+                '<?php
+                switch($a) {
+                    case (int) $a < 1; {
+                        echo "leave ; alone";
+                        break;
+                    }
+                    case ($a < 2)/* test */ ; {
+                        echo "fix 1";
+                        break;
+                    }
+                    case (3);{
+                        echo "fix 2";
+                        break;
+                    }
+                    case /**/(/**/ // test
+                        4
+                        /**/)//
+                        /**/; {
+                        echo "fix 3";
+                        break;
+                    }
+                    case (((int)$b) + 4.1) ; {
+                        echo "fix 4";
+                        break;
+                    }
+                    case ($b + 1) * 2 ; {;;
+                        echo "leave alone";
+                        break;
+                    }
+                }
+                ',
+            ),
+        );
+    }
+}

+ 28 - 8
Symfony/CS/Tests/Fixer/PSR2/SwitchCaseSpaceFixerTest.php

@@ -31,14 +31,6 @@ final class SwitchCaseSpaceFixerTest extends AbstractFixerTestBase
     public function provideFixCases()
     {
         return array(
-            array(
-                '<?php
-                switch ($a) {
-                    case "prod":
-                        break;
-                }
-                ',
-            ),
             array(
                 '<?php
                 switch ($a) {
@@ -271,6 +263,34 @@ final class SwitchCaseSpaceFixerTest extends AbstractFixerTestBase
                     }
                 }
                 ',
+                array(
+                    '<?php
+                    switch ($a) {
+                        case 42:
+                            break;
+                        case 1:
+                            switch ($a) {
+                                case 42:
+                                    break;
+                                default:
+                                    echo 1   ;
+                            }
+                    }
+                    ',
+                    '<?php
+                    switch ($a) {
+                        case 42   :
+                            break;
+                        case 1    :
+                            switch ($a) {
+                                case 42   :
+                                    break;
+                                default :
+                                    echo 1   ;
+                            }
+                    }
+                    ',
+                ),
             ),
         );
     }

+ 1 - 0
Symfony/CS/Tests/FixerTest.php

@@ -179,6 +179,7 @@ class FixerTest extends \PHPUnit_Framework_TestCase
             array($fixers['duplicate_semicolon'], $fixers['braces']),
             array($fixers['duplicate_semicolon'], $fixers['spaces_before_semicolon']),
             array($fixers['duplicate_semicolon'], $fixers['multiline_spaces_before_semicolon']),
+            array($fixers['duplicate_semicolon'], $fixers['switch_case_semicolon_to_colon']),  // tested also in: duplicate_semicolon,switch_case_semicolon_to_colon.test
             array($fixers['double_arrow_multiline_whitespaces'], $fixers['multiline_array_trailing_comma']),
             array($fixers['double_arrow_multiline_whitespaces'], $fixers['align_double_arrow']), // tested also in: double_arrow_multiline_whitespaces,align_double_arrow.test
             array($fixers['operators_spaces'], $fixers['align_double_arrow']), // tested also in: align_double_arrow,operators_spaces.test

+ 18 - 0
Symfony/CS/Tests/Fixtures/Integration/priority/duplicate_semicolon,switch_case_semicolon_to_colon.test

@@ -0,0 +1,18 @@
+--TEST--
+Integration of fixers: duplicate_semicolon,switch_case_semicolon_to_colon.
+--CONFIG--
+level=none
+fixers=duplicate_semicolon,switch_case_semicolon_to_colon
+--EXPECT--
+<?php
+switch($a) {
+    case 1:
+        echo 2;
+}
+
+--INPUT--
+<?php
+switch($a) {
+    case 1;;
+        echo 2;
+}