2019-07-04 21:18:06 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-07-05 15:06:28 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2019-07-04 21:18:06 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2019-07-05 15:06:28 +00:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2019-07-04 21:18:06 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\StrlenZeroToIdenticalEmptyStringRector\StrlenZeroToIdenticalEmptyStringRectorTest
|
|
|
|
*/
|
2019-07-04 21:18:06 +00:00
|
|
|
final class StrlenZeroToIdenticalEmptyStringRector extends AbstractRector
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('', [
|
|
|
|
new CodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-07-04 21:18:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
$empty = strlen($value) === 0;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-07-04 21:18:06 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-07-04 21:18:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
$empty = $value === '';
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-07-04 21:18:06 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Identical::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Identical $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
$variable = null;
|
|
|
|
if ($node->left instanceof FuncCall) {
|
|
|
|
if (! $this->isName($node->left, 'strlen')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isValue($node->right, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$variable = $node->left->args[0]->value;
|
2019-07-09 15:06:58 +00:00
|
|
|
} elseif ($node->right instanceof FuncCall) {
|
2019-07-04 21:18:06 +00:00
|
|
|
if (! $this->isName($node->right, 'strlen')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isValue($node->left, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$variable = $node->right->args[0]->value;
|
2019-07-09 15:06:58 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2019-07-04 21:18:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 15:06:28 +00:00
|
|
|
/** @var Expr $variable */
|
|
|
|
return new Identical($variable, new String_(''));
|
2019-07-04 21:18:06 +00:00
|
|
|
}
|
|
|
|
}
|