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

minor #2206 Add PHP70Migration ruleset (keradus)

This PR was squashed before being merged into the 2.0-dev branch (closes #2206).

Discussion
----------

Add PHP70Migration ruleset

Commits
-------

99b8813 Add PHP70Migration ruleset
Dariusz Ruminski 8 лет назад
Родитель
Сommit
5c74ef1fb5

+ 1 - 1
README.rst

@@ -515,7 +515,7 @@ Choose from the list of available fixers:
 * **psr4**
    Class names should match the file name. (Risky fixer!)
 
-* **random_api_migration**
+* **random_api_migration** [@PHP70Migration]
    Replaces rand, srand, getrandmax functions calls with their mt_*
    analogs. (Risky fixer!)
 

+ 6 - 1
src/Fixer/Alias/RandomApiMigrationFixer.php

@@ -27,9 +27,10 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer
     private $configuration;
 
     private static $defaultConfiguration = array(
+        'getrandmax' => array('alternativeName' => 'mt_getrandmax', 'argumentCount' => array(0)),
+        'mt_rand' => array('alternativeName' => 'mt_rand', 'argumentCount' => array(1, 2)),
         'rand' => array('alternativeName' => 'mt_rand', 'argumentCount' => array(0, 2)),
         'srand' => array('alternativeName' => 'mt_srand', 'argumentCount' => array(0, 1)),
-        'getrandmax' => array('alternativeName' => 'mt_getrandmax', 'argumentCount' => array(0)),
     );
 
     /**
@@ -72,6 +73,10 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer
     public function fix(\SplFileInfo $file, Tokens $tokens)
     {
         foreach ($this->configuration as $functionIdentity => $functionReplacement) {
+            if ($functionIdentity === $functionReplacement['alternativeName']) {
+                continue;
+            }
+
             $currIndex = 0;
             while (null !== $currIndex) {
                 // try getting function reference and translate boundaries for humans

+ 6 - 0
src/RuleSet.php

@@ -128,6 +128,12 @@ final class RuleSet implements RuleSetInterface
             'php_unit_dedicate_assert' => true,
             'silenced_deprecation_error' => true,
         ),
+        '@PHP70Migration' => array(
+            'random_api_migration' => array(
+                'rand' => 'random_int',
+                'mt_rand' => 'random_int',
+            ),
+        ),
     );
 
     /**

+ 23 - 23
tests/Fixer/Alias/RandomApiMigrationFixerTest.php

@@ -55,8 +55,10 @@ final class RandomApiMigrationFixerTest extends AbstractFixerTestCase
     /**
      * @dataProvider provideCases
      */
-    public function testFix($expected, $input = null)
+    public function testFix($expected, $input = null, array $config = null)
     {
+        $this->getFixer()->configure($config);
+
         $this->doTest($expected, $input);
     }
 
@@ -107,28 +109,26 @@ class srand extends SrandClass{
             array('<?php a(mt_rand());', '<?php a(rand());'),
             array('<?php a(mt_srand());', '<?php a(srand());'),
             array('<?php a(\\mt_srand());', '<?php a(\\srand());'),
-        );
-    }
-
-    /**
-     * @dataProvider provideCasesForCustomConfiguration
-     */
-    public function testFixForCustomConfiguration($expected, $input = null)
-    {
-        $this->getFixer()->configure(array('rand' => 'random_int'));
-
-        $this->doTest($expected, $input);
-    }
-
-    /**
-     * @return array[]
-     */
-    public function provideCasesForCustomConfiguration()
-    {
-        return array(
-            array('<?php rand(rand($a));'),
-            array('<?php random_int($d, random_int($a,$b));', '<?php rand($d, rand($a,$b));'),
-            array('<?php random_int($a, \Other\Scope\mt_rand($a));', '<?php rand($a, \Other\Scope\mt_rand($a));'),
+            array(
+                '<?php rand(rand($a));',
+                null,
+                array('rand' => 'random_int'),
+            ),
+            array(
+                '<?php random_int($d, random_int($a,$b));',
+                '<?php rand($d, rand($a,$b));',
+                array('rand' => 'random_int'),
+            ),
+            array(
+                '<?php random_int($a, \Other\Scope\mt_rand($a));',
+                '<?php rand($a, \Other\Scope\mt_rand($a));',
+                array('rand' => 'random_int'),
+            ),
+            array(
+                '<?php $a = random_int(1,2) + random_int(3,4);',
+                '<?php $a = rand(1,2) + mt_rand(3,4);',
+                array('rand' => 'random_int', 'mt_rand' => 'random_int'),
+            ),
         );
     }
 }