diff --git a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture.php.inc b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture.php.inc index 501e94f22ca..88c49e2810e 100644 --- a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture.php.inc +++ b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture.php.inc @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; class Fixture { - public function run() + /** + * @param non-empty-string $needle + */ + public function run(string $haystack, string $needle) { $isMatch = substr($haystack, -strlen($needle)) === $needle; @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; class Fixture { - public function run() + /** + * @param non-empty-string $needle + */ + public function run(string $haystack, string $needle) { $isMatch = str_ends_with($haystack, $needle); diff --git a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture_equal.php.inc b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture_equal.php.inc deleted file mode 100644 index 8c7b7b41348..00000000000 --- a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/fixture_equal.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/include_case_sensitive.php.inc b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/include_case_sensitive.php.inc index 220aeb56d7e..ce4679c2ab2 100644 --- a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/include_case_sensitive.php.inc +++ b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/include_case_sensitive.php.inc @@ -4,6 +4,9 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; final class IncludeCaseSensitive { + /** + * @param non-empty-string $needle + */ public function run($haystack, $needle) { return substr_compare($haystack, $needle, -strlen($needle), null, false) === 0; @@ -18,6 +21,9 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; final class IncludeCaseSensitive { + /** + * @param non-empty-string $needle + */ public function run($haystack, $needle) { return str_ends_with($haystack, $needle); diff --git a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/skip_empty_needle.php.inc b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/skip_empty_needle.php.inc new file mode 100644 index 00000000000..887b8dd7406 --- /dev/null +++ b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/skip_empty_needle.php.inc @@ -0,0 +1,21 @@ + ------ - diff --git a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/substr_not_prefix.php.inc b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/substr_not_prefix.php.inc index c2e20cdc770..c641c797196 100644 --- a/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/substr_not_prefix.php.inc +++ b/rules-tests/Php80/Rector/Identical/StrEndsWithRector/Fixture/substr_not_prefix.php.inc @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; class SubstrNotPrefix { - public function run() + /** + * @param non-empty-string $needle + */ + public function run(string $haystack, string $needle) { $isNotMatch = substr($haystack, -strlen($needle)) !== $needle; @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture; class SubstrNotPrefix { - public function run() + /** + * @param non-empty-string $needle + */ + public function run(string $haystack, string $needle) { $isNotMatch = !str_ends_with($haystack, $needle); diff --git a/rules/Php80/Rector/Identical/StrEndsWithRector.php b/rules/Php80/Rector/Identical/StrEndsWithRector.php index 9ef68a54e5a..c7a0256ce86 100644 --- a/rules/Php80/Rector/Identical/StrEndsWithRector.php +++ b/rules/Php80/Rector/Identical/StrEndsWithRector.php @@ -19,6 +19,7 @@ use PhpParser\Node\Scalar\String_; use Rector\NodeAnalyzer\BinaryOpAnalyzer; use Rector\PhpParser\Node\Value\ValueResolver; +use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; @@ -155,6 +156,18 @@ private function refactorSubstr(BinaryOp $binaryOp): FuncCall|BooleanNot|null return null; } + $needleType = ScopeFetcher::fetch($binaryOp)->getType($comparedNeedleExpr); + + // str_ends_with() is always true for an empty needle, while the original comparison is not + if (! $needleType->isNonEmptyString()->yes()) { + return null; + } + + // loose comparison of numeric strings compares them as numbers + if (($binaryOp instanceof Equal || $binaryOp instanceof NotEqual) && ! $needleType->isNumericString()->no()) { + return null; + } + $haystack = $substrFuncCall->getArgs()[0] ->value; @@ -201,6 +214,11 @@ private function refactorSubstrCompare(BinaryOp $binaryOp): FuncCall|BooleanNot| return null; } + // str_ends_with() is always true for an empty needle, while substr_compare() is not 0 + if (! ScopeFetcher::fetch($binaryOp)->getType($needle)->isNonEmptyString()->yes()) { + return null; + } + $isPositive = $binaryOp instanceof Identical || $binaryOp instanceof Equal; return $this->buildReturnNode($haystack, $needle, $isPositive);