2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Identical;
|
2019-07-04 21:18:06 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\StrlenZeroToIdenticalEmptyStringRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class StrlenZeroToIdenticalEmptyStringRector extends AbstractRector
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes strlen comparison to 0 to direct empty string compare', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-07-04 21:18:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2021-10-11 23:20:15 +00:00
|
|
|
public function run(string $value)
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
|
|
|
$empty = strlen($value) === 0;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-07-04 21:18:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2021-10-11 23:20:15 +00:00
|
|
|
public function run(string $value)
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
|
|
|
$empty = $value === '';
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-07-04 21:18:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-07-04 21:18:06 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Identical::class];
|
2019-07-04 21:18:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Identical $node
|
2019-07-04 21:18:06 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-07-04 21:18:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->left instanceof FuncCall) {
|
2021-10-11 23:20:15 +00:00
|
|
|
return $this->processIdentical($node->right, $node->left);
|
2021-09-27 15:43:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node->right instanceof FuncCall) {
|
2021-10-11 23:20:15 +00:00
|
|
|
return $this->processIdentical($node->left, $node->right);
|
2021-09-27 15:43:15 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processIdentical(Expr $expr, FuncCall $funcCall) : ?Identical
|
2021-09-27 15:43:15 +00:00
|
|
|
{
|
|
|
|
if (!$this->isName($funcCall, 'strlen')) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if ($funcCall->isFirstClassCallable()) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
if (!$this->valueResolver->isValue($expr, 0)) {
|
2021-09-27 15:43:15 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-13 17:20:02 +00:00
|
|
|
$variable = $funcCall->getArgs()[0]->value;
|
2021-10-11 23:20:15 +00:00
|
|
|
// Needs string cast if variable type is not string
|
|
|
|
// see https://github.com/rectorphp/rector/issues/6700
|
2022-11-15 00:01:27 +00:00
|
|
|
$isStringType = $this->nodeTypeResolver->getNativeType($variable)->isString()->yes();
|
2021-10-11 23:20:15 +00:00
|
|
|
if (!$isStringType) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Identical(new Expr\Cast\String_($variable), new String_(''));
|
2021-09-27 15:43:15 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Identical($variable, new String_(''));
|
2019-07-04 21:18:06 +00:00
|
|
|
}
|
|
|
|
}
|