Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

final class SkipEmptyNeedle
{
public function run(string $haystack, string $needle)
{
$isMatch = substr($haystack, -strlen($needle)) === $needle;

$isNotMatch = $needle !== substr($haystack, -strlen($needle));

$isMatch = substr_compare($haystack, $needle, -strlen($needle)) === 0;

$isNotMatch = 0 != substr_compare($haystack, $needle, -strlen($needle));

$isMatch = substr($haystack, -0) === '';

$isMatch = substr_compare($haystack, '', -0) === 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SkipFixtureEqual
{
public function run()
{
$isMatch = substr($haystack, -strlen($needle)) == $needle;

$isMatch = $needle == substr($haystack, -strlen($needle));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

final class SkipLooseNumeric
{
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isMatch = substr($haystack, -strlen($needle)) == $needle;

$isNotMatch = $needle != substr($haystack, -strlen($needle));

$isMatch = substr($haystack, -3) == '1e1';

$isNotMatch = '1e1' != substr($haystack, -3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SkipSubstrNotEqualPrefix
{
public function run()
{
$isNotMatch = substr($haystack, -strlen($needle)) != $needle;

$isNotMatch = $needle != substr($haystack, -strlen($needle));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompare
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isMatch = substr_compare($haystack, $needle, -strlen($needle)) === 0;

Expand All @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompare
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isMatch = str_ends_with($haystack, $needle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompare
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isMatch = substr_compare($haystack, $needle, -strlen($needle)) == 0;

Expand All @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompare
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isMatch = str_ends_with($haystack, $needle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompareNot
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isNotMatch = substr_compare($haystack, $needle, -strlen($needle)) !== 0;

Expand All @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompareNot
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isNotMatch = !str_ends_with($haystack, $needle);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompareNot
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isNotMatch = substr_compare($haystack, $needle, -strlen($needle)) != 0;

Expand All @@ -20,7 +23,10 @@ namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;

class SubstrCompareNot
{
public function run()
/**
* @param non-empty-string $needle
*/
public function run(string $haystack, string $needle)
{
$isNotMatch = !str_ends_with($haystack, $needle);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions rules/Php80/Rector/Identical/StrEndsWithRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Loading