From 230c0f1b856a34f760404ff02034b0c49972f1b3 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Thu, 25 Sep 2025 07:02:13 -0500 Subject: [PATCH] feat: add greater and smaller support to StrlenZeroToIdenticalEmptyStringRector --- .../Fixture/greater_and_smaller.php.inc | 37 +++++++++++++++++++ .../Fixture/skip_less_than_zero.php.inc | 8 ++++ .../Fixture/stringable_object.php.inc | 26 ++----------- .../Source/Stringable.php | 13 +++++++ ...StrlenZeroToIdenticalEmptyStringRector.php | 37 ++++++++++++------- 5 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/greater_and_smaller.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/skip_less_than_zero.php.inc create mode 100644 rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Source/Stringable.php diff --git a/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/greater_and_smaller.php.inc b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/greater_and_smaller.php.inc new file mode 100644 index 00000000000..b937337293a --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/greater_and_smaller.php.inc @@ -0,0 +1,37 @@ + 0; + 0 < strlen($string); + + strlen($mixed) > 0; + 0 < strlen($mixed); + + strlen($stringable) > 0; + 0 < strlen($stringable); +}; + +?> +----- + diff --git a/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/skip_less_than_zero.php.inc b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/skip_less_than_zero.php.inc new file mode 100644 index 00000000000..d58cdfbe5eb --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/skip_less_than_zero.php.inc @@ -0,0 +1,8 @@ + strlen($string); +}; diff --git a/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/stringable_object.php.inc b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/stringable_object.php.inc index e53607f29f7..415c2789577 100644 --- a/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/stringable_object.php.inc +++ b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Fixture/stringable_object.php.inc @@ -2,22 +2,13 @@ namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture; -class Stringable { - private string $string = ''; - - public function __toString() : string { - return $this->string; - } -} +use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable; class StringableObject { - public function run() + public function run(Stringable $value) { - $value = new Stringable(); - $empty = strlen($value) === 0; - $empty = 0 === strlen($value); } } @@ -28,22 +19,13 @@ class StringableObject namespace Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Fixture; -class Stringable { - private string $string = ''; - - public function __toString() : string { - return $this->string; - } -} +use Rector\Tests\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector\Source\Stringable; class StringableObject { - public function run() + public function run(Stringable $value) { - $value = new Stringable(); - $empty = (string) $value === ''; - $empty = (string) $value === ''; } } diff --git a/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Source/Stringable.php b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Source/Stringable.php new file mode 100644 index 00000000000..46081d30436 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector/Source/Stringable.php @@ -0,0 +1,13 @@ +string; + } +} diff --git a/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php b/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php index 6637dc64ac8..2043a5fb8b5 100644 --- a/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php +++ b/rules/CodeQuality/Rector/Identical/StrlenZeroToIdenticalEmptyStringRector.php @@ -6,7 +6,10 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\BinaryOp\Greater; use PhpParser\Node\Expr\BinaryOp\Identical; +use PhpParser\Node\Expr\BinaryOp\NotIdentical; +use PhpParser\Node\Expr\BinaryOp\Smaller; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; use Rector\PhpParser\Node\Value\ValueResolver; @@ -59,27 +62,24 @@ public function run(string $value) */ public function getNodeTypes(): array { - return [Identical::class]; + return [Greater::class, Smaller::class, Identical::class]; } /** - * @param Identical $node + * @param Greater|Smaller|Identical $node */ public function refactor(Node $node): ?Node { if ($node->left instanceof FuncCall) { - return $this->processIdentical($node->right, $node->left); - } - - if ($node->right instanceof FuncCall) { - return $this->processIdentical($node->left, $node->right); + $funcCall = $node->left; + $expr = $node->right; + } elseif ($node->right instanceof FuncCall) { + $expr = $node->left; + $funcCall = $node->right; + } else { + return null; } - return null; - } - - private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical - { if (! $this->isName($funcCall, 'strlen')) { return null; } @@ -92,6 +92,13 @@ private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical return null; } + if ( + ($node instanceof Greater && $node->right instanceof FuncCall) + || ($node instanceof Smaller && $node->left instanceof FuncCall) + ) { + return null; + } + $variable = $funcCall->getArgs()[0] ->value; @@ -102,9 +109,11 @@ private function processIdentical(Expr $expr, FuncCall $funcCall): ?Identical ->yes(); if (! $isStringType) { - return new Identical(new Expr\Cast\String_($variable), new String_('')); + $variable = new Expr\Cast\String_($variable); } - return new Identical($variable, new String_('')); + return $node instanceof Identical + ? new Identical($variable, new String_('')) + : new NotIdentical($variable, new String_('')); } }