Просмотр исходного кода

feature #1613 ScalarCastFixer - LowerCaseCastFixer - Add (SpacePossum)

This PR was squashed before being merged into the 1.12 branch (closes #1613).

Discussion
----------

ScalarCastFixer - LowerCaseCastFixer - Add

Commits
-------

0993fd0 ScalarCastFixer - LowerCaseCastFixer - Add
Dariusz Ruminski 9 лет назад
Родитель
Сommit
5d9470c7c0

+ 11 - 0
README.rst

@@ -368,6 +368,10 @@ Choose from the list of available fixers:
                         Remove trailing commas in list
                         function calls.
 
+* **lowercase_cast** [symfony]
+                        Cast should be written in
+                        lower case.
+
 * **method_argument_default_value** [symfony]
                         In method arguments there must
                         not be arguments with default
@@ -513,6 +517,13 @@ Choose from the list of available fixers:
                         exclamation mark should not be
                         used.
 
+* **short_scalar_cast** [symfony]
+                        Cast "(boolean)" and
+                        "(integer)" should be written
+                        as "(bool)" and "(int)".
+                        "(double)" and "(real)" as
+                        "(float)".
+
 * **single_array_no_trailing_comma** [symfony]
                         PHP single-line arrays should
                         not have trailing comma.

+ 47 - 0
Symfony/CS/Fixer/Symfony/LowercaseCastFixer.php

@@ -0,0 +1,47 @@
+<?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\Symfony;
+
+use Symfony\CS\AbstractFixer;
+use Symfony\CS\Tokenizer\Tokens;
+
+/**
+ * @author SpacePossum
+ */
+final class LowercaseCastFixer extends AbstractFixer
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function fix(\SplFileInfo $file, $content)
+    {
+        $tokens = Tokens::fromCode($content);
+
+        for ($index = 0, $count = $tokens->count(); $index  < $count; ++$index) {
+            if (!$tokens[$index]->isCast()) {
+                continue;
+            }
+
+            $tokens[$index]->setContent(strtolower($tokens[$index]->getContent()));
+        }
+
+        return $tokens->generateCode();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDescription()
+    {
+        return 'Cast should be written in lower case.';
+    }
+}

+ 60 - 0
Symfony/CS/Fixer/Symfony/ShortScalarCastFixer.php

@@ -0,0 +1,60 @@
+<?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\Symfony;
+
+use Symfony\CS\AbstractFixer;
+use Symfony\CS\Tokenizer\Tokens;
+
+/**
+ * @author SpacePossum
+ */
+final class ShortScalarCastFixer extends AbstractFixer
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function fix(\SplFileInfo $file, $content)
+    {
+        static $castMap = array(
+            'boolean' => 'bool',
+            'integer' => 'int',
+            'double' => 'float',
+            'real' => 'float',
+        );
+
+        $tokens = Tokens::fromCode($content);
+
+        for ($index = 0, $count = $tokens->count(); $index  < $count; ++$index) {
+            if (!$tokens[$index]->isCast()) {
+                continue;
+            }
+
+            $castFrom = trim(substr($tokens[$index]->getContent(), 1, -1));
+            $castFromLowered = strtolower($castFrom);
+            if (!array_key_exists($castFromLowered, $castMap)) {
+                continue;
+            }
+
+            $tokens[$index]->setContent(str_replace($castFrom, $castMap[$castFromLowered], $tokens[$index]->getContent()));
+        }
+
+        return $tokens->generateCode();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getDescription()
+    {
+        return 'Cast "(boolean)" and "(integer)" should be written as "(bool)" and "(int)". "(double)" and "(real)" as "(float)".';
+    }
+}

+ 1 - 1
Symfony/CS/Tests/Fixer/PSR2/LowercaseKeywordsFixerTest.php

@@ -30,7 +30,7 @@ class LowercaseKeywordsFixerTest extends AbstractFixerTestBase
     {
         return array(
             array('<?php $x = (1 and 2);', '<?php $x = (1 AND 2);'),
-            array('<?php foreach(array(1, 2, 3) as $val) {}', '<?php foreach(array(1, 2, 3) AS $val) {}'),
+            array('<?php foreach(array(1, 2, 3) as $val) {}', '<?php FOREACH(array(1, 2, 3) AS $val) {}'),
             array('<?php echo "GOOD AS NEW";'),
         );
     }

+ 59 - 0
Symfony/CS/Tests/Fixer/Symfony/LowercaseCastFixerTest.php

@@ -0,0 +1,59 @@
+<?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\Symfony;
+
+use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
+
+/**
+ * @author SpacePossum
+ *
+ * @internal
+ */
+final class LowercaseCastFixerTest extends AbstractFixerTestBase
+{
+    /**
+     * @dataProvider provideCases
+     */
+    public function testFix($expected, $input = null)
+    {
+        $this->makeTest($expected, $input);
+    }
+
+    public function provideCases()
+    {
+        $cases = array();
+        foreach (array('boolean', 'bool', 'integer', 'int', 'double', 'float', 'real', 'float', 'string', 'array', 'object', 'unset', 'binary') as $from) {
+            $cases[] =
+                array(
+                    sprintf('<?php $b= (%s)$d;', $from),
+                    sprintf('<?php $b= (%s)$d;', strtoupper($from)),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=( %s) $d;', $from),
+                    sprintf('<?php $b=( %s) $d;', ucfirst($from)),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=(%s ) $d;', $from),
+                    sprintf('<?php $b=(%s ) $d;', strtoupper($from)),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=(  %s  ) $d;', $from),
+                    sprintf('<?php $b=(  %s  ) $d;', ucfirst($from)),
+                );
+        }
+
+        return $cases;
+    }
+}

+ 85 - 0
Symfony/CS/Tests/Fixer/Symfony/ShortScalarCastFixerTest.php

@@ -0,0 +1,85 @@
+<?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\Symfony;
+
+use Symfony\CS\Tests\Fixer\AbstractFixerTestBase;
+
+/**
+ * @author SpacePossum
+ *
+ * @internal
+ */
+final class ShortScalarCastFixerTest extends AbstractFixerTestBase
+{
+    /**
+     * @dataProvider provideFixCases
+     */
+    public function testFix($expected, $input = null)
+    {
+        $this->makeTest($expected, $input);
+    }
+
+    public function provideFixCases()
+    {
+        $cases = array();
+        foreach (array('boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float') as $from => $to) {
+            $cases[] =
+                array(
+                    sprintf('<?php echo ( %s  )$a;', $to),
+                    sprintf('<?php echo ( %s  )$a;', $from),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=(%s) $d;', $to),
+                    sprintf('<?php $b=(%s) $d;', $from),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b= (%s)$d;', $to),
+                    sprintf('<?php $b= (%s)$d;', strtoupper($from)),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=( %s) $d;', $to),
+                    sprintf('<?php $b=( %s) $d;', ucfirst($from)),
+                );
+            $cases[] =
+                array(
+                    sprintf('<?php $b=(%s ) $d;', $to),
+                    sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
+                );
+        }
+
+        return $cases;
+    }
+
+    /**
+     * @dataProvider provideNoFixCases
+     */
+    public function testNoFix($expected)
+    {
+        $this->makeTest($expected);
+    }
+
+    public function provideNoFixCases()
+    {
+        $cases = array();
+        foreach (array('string', 'array', 'object', 'unset', 'binary') as $cast) {
+            $cases[] = array(sprintf('<?php $b=(%s) $d;', $cast));
+            $cases[] = array(sprintf('<?php $b=( %s ) $d;', $cast));
+            $cases[] = array(sprintf('<?php $b=(%s ) $d;', ucfirst($cast)));
+            $cases[] = array(sprintf('<?php $b=(%s ) $d;', strtoupper($cast)));
+        }
+
+        return $cases;
+    }
+}