[PHP 7.1] Add BinaryOpBetweenNumberAndStringRector

This commit is contained in:
Tomas Votruba 2019-05-19 12:37:51 +02:00
parent 00465053a1
commit b3f55428b3
4 changed files with 151 additions and 0 deletions

View File

@ -7,3 +7,4 @@ services:
Rector\Php\Rector\Assign\AssignArrayToStringRector: ~
Rector\Php\Rector\FuncCall\CountOnNullRector: ~
Rector\Php\Rector\FuncCall\RemoveExtraParametersRector: ~
Rector\Php\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector: ~

View File

@ -0,0 +1,88 @@
<?php declare(strict_types=1);
namespace Rector\Php\Rector\BinaryOp;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://3v4l.org/XPEEl
* @see https://3v4l.org/ObNQZ
*/
final class BinaryOpBetweenNumberAndStringRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Change binary operation between some number + string to PHP 7.1 compatible version',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 + '';
$value = 5 + 'hi';
$value = 5 + 'Tom';
$value = 5 * '';
$value = 5 * 'hi';
$name = 'Tom';
$value = 5 * $name;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 + 0;
$value = 5 + 0
$value = 5 + 0;
$value = 5 * 0;
$value = 5 * 0;
$name = 'Tom';
$value = 5 * 0;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [BinaryOp::class];
}
/**
* @param BinaryOp $node
*/
public function refactor(Node $node): ?Node
{
if ($this->isStringyType($node->left) && $this->isIntegerType($node->right)) {
$node->left = new Node\Scalar\LNumber(0);
return $node;
}
if ($this->isStringyType($node->right) && $this->isIntegerType($node->left)) {
$node->right = new Node\Scalar\LNumber(0);
return $node;
}
return null;
}
}

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector;
use Rector\Php\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class BinaryOpBetweenNumberAndStringRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
}
protected function getRectorClass(): string
{
return BinaryOpBetweenNumberAndStringRector::class;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Rector\Php\Tests\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\Fixture;
class SomeClass
{
public function run()
{
$value = 5 + '';
$value = 5 + 'hi';
$value = 5 + 'Tom';
$value = 5 * '';
$value = 5 * 'hi';
$name = 'Tom';
$value = 5 * $name;
}
}
?>
-----
<?php
namespace Rector\Php\Tests\Rector\BinaryOp\BinaryOpBetweenNumberAndStringRector\Fixture;
class SomeClass
{
public function run()
{
$value = 5 + 0;
$value = 5 + 0;
$value = 5 + 0;
$value = 5 * 0;
$value = 5 * 0;
$name = 'Tom';
$value = 5 * 0;
}
}
?>