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

Fix PHP 7.4 deprecation notices

Julien Falque 5 лет назад
Родитель
Сommit
c5694c1492

+ 22 - 3
src/Fixer/CastNotation/LowercaseCastFixer.php

@@ -13,8 +13,9 @@
 namespace PhpCsFixer\Fixer\CastNotation;
 namespace PhpCsFixer\Fixer\CastNotation;
 
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\AbstractFixer;
-use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\FixerDefinition\VersionSpecification;
+use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\Tokenizer\Tokens;
 
 
@@ -31,7 +32,7 @@ final class LowercaseCastFixer extends AbstractFixer
         return new FixerDefinition(
         return new FixerDefinition(
             'Cast should be written in lower case.',
             'Cast should be written in lower case.',
             [
             [
-                new CodeSample(
+                new VersionSpecificCodeSample(
                     '<?php
                     '<?php
     $a = (BOOLEAN) $b;
     $a = (BOOLEAN) $b;
     $a = (BOOL) $b;
     $a = (BOOL) $b;
@@ -46,7 +47,25 @@ final class LowercaseCastFixer extends AbstractFixer
     $a = (OBJect) $b;
     $a = (OBJect) $b;
     $a = (UNset) $b;
     $a = (UNset) $b;
     $a = (Binary) $b;
     $a = (Binary) $b;
-'
+',
+                    new VersionSpecification(null, 70399)
+                ),
+                new VersionSpecificCodeSample(
+                    '<?php
+    $a = (BOOLEAN) $b;
+    $a = (BOOL) $b;
+    $a = (INTEGER) $b;
+    $a = (INT) $b;
+    $a = (DOUBLE) $b;
+    $a = (FLoaT) $b;
+    $a = (flOAT) $b;
+    $a = (sTRING) $b;
+    $a = (ARRAy) $b;
+    $a = (OBJect) $b;
+    $a = (UNset) $b;
+    $a = (Binary) $b;
+',
+                    new VersionSpecification(70400)
                 ),
                 ),
             ]
             ]
         );
         );

+ 12 - 2
src/Fixer/CastNotation/ShortScalarCastFixer.php

@@ -13,8 +13,9 @@
 namespace PhpCsFixer\Fixer\CastNotation;
 namespace PhpCsFixer\Fixer\CastNotation;
 
 
 use PhpCsFixer\AbstractFixer;
 use PhpCsFixer\AbstractFixer;
-use PhpCsFixer\FixerDefinition\CodeSample;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
 use PhpCsFixer\FixerDefinition\FixerDefinition;
+use PhpCsFixer\FixerDefinition\VersionSpecification;
+use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
 use PhpCsFixer\Tokenizer\Tokens;
 
 
@@ -30,7 +31,16 @@ final class ShortScalarCastFixer extends AbstractFixer
     {
     {
         return new FixerDefinition(
         return new FixerDefinition(
             'Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`.',
             'Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`.',
-            [new CodeSample("<?php\n\$a = (boolean) \$b;\n\$a = (integer) \$b;\n\$a = (double) \$b;\n\$a = (real) \$b;\n\n\$a = (binary) \$b;\n")]
+            [
+                new VersionSpecificCodeSample(
+                    "<?php\n\$a = (boolean) \$b;\n\$a = (integer) \$b;\n\$a = (double) \$b;\n\$a = (real) \$b;\n\n\$a = (binary) \$b;\n",
+                    new VersionSpecification(null, 70399)
+                ),
+                new VersionSpecificCodeSample(
+                    "<?php\n\$a = (boolean) \$b;\n\$a = (integer) \$b;\n\$a = (double) \$b;\n\n\$a = (binary) \$b;\n",
+                    new VersionSpecification(70400)
+                ),
+            ]
         );
         );
     }
     }
 
 

+ 60 - 23
tests/Fixer/CastNotation/LowercaseCastFixerTest.php

@@ -28,38 +28,75 @@ final class LowercaseCastFixerTest extends AbstractFixerTestCase
      * @param null|string $input
      * @param null|string $input
      *
      *
      * @dataProvider provideFixCases
      * @dataProvider provideFixCases
+     * @dataProvider provideFixDeprecatedCases
+     * @requires PHP < 7.4
      */
      */
     public function testFix($expected, $input = null)
     public function testFix($expected, $input = null)
     {
     {
         $this->doTest($expected, $input);
         $this->doTest($expected, $input);
     }
     }
 
 
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixCases
+     * @requires PHP 7.4
+     */
+    public function testFix74($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixDeprecatedCases
+     * @requires PHP 7.4
+     * @group legacy
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     */
+    public function testFix74Deprecated($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
     public function provideFixCases()
     public function provideFixCases()
     {
     {
-        $cases = [];
-        foreach (['boolean', 'bool', 'integer', 'int', 'double', 'float', 'real', 'float', 'string', 'array', 'object', 'unset', 'binary'] as $from) {
-            $cases[] =
-                [
-                    sprintf('<?php $b= (%s)$d;', $from),
-                    sprintf('<?php $b= (%s)$d;', strtoupper($from)),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=( %s) $d;', $from),
-                    sprintf('<?php $b=( %s) $d;', ucfirst($from)),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=(%s ) $d;', $from),
-                    sprintf('<?php $b=(%s ) $d;', strtoupper($from)),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=(  %s  ) $d;', $from),
-                    sprintf('<?php $b=(  %s  ) $d;', ucfirst($from)),
-                ];
+        foreach (['boolean', 'bool', 'integer', 'int', 'double', 'float', 'float', 'string', 'array', 'object', 'unset', 'binary'] as $from) {
+            foreach ($this->createCasesFor($from) as $case) {
+                yield $case;
+            }
         }
         }
+    }
+
+    public function provideFixDeprecatedCases()
+    {
+        return $this->createCasesFor('real');
+    }
 
 
-        return $cases;
+    private function createCasesFor($type)
+    {
+        yield [
+            sprintf('<?php $b= (%s)$d;', $type),
+            sprintf('<?php $b= (%s)$d;', strtoupper($type)),
+        ];
+        yield [
+            sprintf('<?php $b=( %s) $d;', $type),
+            sprintf('<?php $b=( %s) $d;', ucfirst($type)),
+        ];
+        yield [
+            sprintf('<?php $b=(%s ) $d;', $type),
+            sprintf('<?php $b=(%s ) $d;', strtoupper($type)),
+        ];
+        yield [
+            sprintf('<?php $b=(  %s  ) $d;', $type),
+            sprintf('<?php $b=(  %s  ) $d;', ucfirst($type)),
+        ];
     }
     }
 }
 }

+ 60 - 28
tests/Fixer/CastNotation/ShortScalarCastFixerTest.php

@@ -28,44 +28,52 @@ final class ShortScalarCastFixerTest extends AbstractFixerTestCase
      * @param null|string $input
      * @param null|string $input
      *
      *
      * @dataProvider provideFixCases
      * @dataProvider provideFixCases
+     * @dataProvider provideFixDeprecatedCases
+     * @requires PHP < 7.4
      */
      */
     public function testFix($expected, $input = null)
     public function testFix($expected, $input = null)
     {
     {
         $this->doTest($expected, $input);
         $this->doTest($expected, $input);
     }
     }
 
 
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixCases
+     * @requires PHP 7.4
+     */
+    public function testFix74($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
+    /**
+     * @param string      $expected
+     * @param null|string $input
+     *
+     * @dataProvider provideFixDeprecatedCases
+     * @requires PHP 7.4
+     * @group legacy
+     * @expectedDeprecation Unsilenced deprecation: The (real) cast is deprecated, use (float) instead
+     */
+    public function testFix74Deprecated($expected, $input = null)
+    {
+        $this->doTest($expected, $input);
+    }
+
     public function provideFixCases()
     public function provideFixCases()
     {
     {
-        $cases = [];
-        foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float', 'binary' => 'string'] as $from => $to) {
-            $cases[] =
-                [
-                    sprintf('<?php echo ( %s  )$a;', $to),
-                    sprintf('<?php echo ( %s  )$a;', $from),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=(%s) $d;', $to),
-                    sprintf('<?php $b=(%s) $d;', $from),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b= (%s)$d;', $to),
-                    sprintf('<?php $b= (%s)$d;', strtoupper($from)),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=( %s) $d;', $to),
-                    sprintf('<?php $b=( %s) $d;', ucfirst($from)),
-                ];
-            $cases[] =
-                [
-                    sprintf('<?php $b=(%s ) $d;', $to),
-                    sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
-                ];
+        foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'binary' => 'string'] as $from => $to) {
+            foreach ($this->createCasesFor($from, $to) as $case) {
+                yield $case;
+            }
         }
         }
+    }
 
 
-        return $cases;
+    public function provideFixDeprecatedCases()
+    {
+        return $this->createCasesFor('real', 'float');
     }
     }
 
 
     /**
     /**
@@ -90,4 +98,28 @@ final class ShortScalarCastFixerTest extends AbstractFixerTestCase
 
 
         return $cases;
         return $cases;
     }
     }
+
+    private function createCasesFor($from, $to)
+    {
+        yield [
+            sprintf('<?php echo ( %s  )$a;', $to),
+            sprintf('<?php echo ( %s  )$a;', $from),
+        ];
+        yield [
+            sprintf('<?php $b=(%s) $d;', $to),
+            sprintf('<?php $b=(%s) $d;', $from),
+        ];
+        yield [
+            sprintf('<?php $b= (%s)$d;', $to),
+            sprintf('<?php $b= (%s)$d;', strtoupper($from)),
+        ];
+        yield [
+            sprintf('<?php $b=( %s) $d;', $to),
+            sprintf('<?php $b=( %s) $d;', ucfirst($from)),
+        ];
+        yield [
+            sprintf('<?php $b=(%s ) $d;', $to),
+            sprintf('<?php $b=(%s ) $d;', ucfirst($from)),
+        ];
+    }
 }
 }

+ 0 - 4
tests/Fixer/ControlStructure/YodaStyleFixerTest.php

@@ -112,10 +112,6 @@ final class YodaStyleFixerTest extends AbstractFixerTestCase
                 '<?php echo 1 === (object) $a ? 8 : 7;',
                 '<?php echo 1 === (object) $a ? 8 : 7;',
                 '<?php echo (object) $a === 1 ? 8 : 7;',
                 '<?php echo (object) $a === 1 ? 8 : 7;',
             ],
             ],
-            [
-                '<?php 1===(real)$a?1:2;',
-                '<?php (real)$a===1?1:2;',
-            ],
             [
             [
                 '<?php echo 1 === (unset) $a ? 8 : 7;',
                 '<?php echo 1 === (unset) $a ? 8 : 7;',
                 '<?php echo (unset) $a === 1 ? 8 : 7;',
                 '<?php echo (unset) $a === 1 ? 8 : 7;',