Browse Source

ShortScalarCastFixer - Change binary cast to string cast as well

Nat Zimmermann 6 years ago
parent
commit
5ac307d4a0

+ 2 - 1
README.rst

@@ -1451,7 +1451,8 @@ Choose from the list of available rules:
 * **short_scalar_cast** [@Symfony]
 
   Cast ``(boolean)`` and ``(integer)`` should be written as ``(bool)`` and
-  ``(int)``, ``(double)`` and ``(real)`` as ``(float)``.
+  ``(int)``, ``(double)`` and ``(real)`` as ``(float)``, ``(binary)`` as
+  ``(string)``.
 
 * **silenced_deprecation_error** [@Symfony:risky]
 

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

@@ -29,8 +29,8 @@ final class ShortScalarCastFixer extends AbstractFixer
     public function getDefinition()
     {
         return new FixerDefinition(
-            'Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`.',
-            [new CodeSample("<?php\n\$a = (boolean) \$b;\n\$a = (integer) \$b;\n\$a = (double) \$b;\n\$a = (real) \$b;\n")]
+            '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")]
         );
     }
 
@@ -52,6 +52,7 @@ final class ShortScalarCastFixer extends AbstractFixer
             'integer' => 'int',
             'double' => 'float',
             'real' => 'float',
+            'binary' => 'string',
         ];
 
         for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) {

+ 2 - 2
tests/Fixer/CastNotation/ShortScalarCastFixerTest.php

@@ -37,7 +37,7 @@ final class ShortScalarCastFixerTest extends AbstractFixerTestCase
     public function provideFixCases()
     {
         $cases = [];
-        foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float'] as $from => $to) {
+        foreach (['boolean' => 'bool', 'integer' => 'int', 'double' => 'float', 'real' => 'float', 'binary' => 'string'] as $from => $to) {
             $cases[] =
                 [
                     sprintf('<?php echo ( %s  )$a;', $to),
@@ -81,7 +81,7 @@ final class ShortScalarCastFixerTest extends AbstractFixerTestCase
     public function provideNoFixCases()
     {
         $cases = [];
-        foreach (['string', 'array', 'object', 'unset', 'binary'] as $cast) {
+        foreach (['string', 'array', 'object', 'unset'] as $cast) {
             $cases[] = [sprintf('<?php $b=(%s) $d;', $cast)];
             $cases[] = [sprintf('<?php $b=( %s ) $d;', $cast)];
             $cases[] = [sprintf('<?php $b=(%s ) $d;', ucfirst($cast))];