mirror of https://github.com/rectorphp/rector.git
Support negative equality on StrEndsWith rule (#339)
This commit is contained in:
parent
1d1dc73fcb
commit
326a8d23e9
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;
|
||||
|
||||
class SubstrNotPrefix
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isMatch = $needle !== substr($haystack, -strlen($needle));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Php80\Rector\Identical\StrEndsWithRector\Fixture;
|
||||
|
||||
class SubstrNotPrefix
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$isMatch = !str_ends_with($haystack, $needle);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr;
|
|||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\BinaryOp\Identical;
|
||||
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\UnaryMinus;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -39,6 +40,8 @@ class SomeClass
|
|||
public function run()
|
||||
{
|
||||
$isMatch = substr($haystack, -strlen($needle)) === $needle;
|
||||
|
||||
$isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
@ -49,6 +52,8 @@ class SomeClass
|
|||
public function run()
|
||||
{
|
||||
$isMatch = str_ends_with($haystack, $needle);
|
||||
|
||||
$isNotMatch = !str_ends_with($haystack, $needle);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
@ -76,7 +81,7 @@ CODE_SAMPLE
|
|||
* Covers:
|
||||
* $isMatch = substr($haystack, -strlen($needle)) === $needle;
|
||||
*/
|
||||
private function refactorSubstr(BinaryOp $binaryOp): ?FuncCall
|
||||
private function refactorSubstr(BinaryOp $binaryOp): FuncCall | BooleanNot | null
|
||||
{
|
||||
if ($binaryOp->left instanceof FuncCall && $this->isName($binaryOp->left, 'substr')) {
|
||||
$substrFuncCall = $binaryOp->left;
|
||||
|
@ -95,7 +100,13 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
return $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);
|
||||
$endsWithNode = $this->nodeFactory->createFuncCall('str_ends_with', [$haystack, $needle]);
|
||||
|
||||
if ($binaryOp instanceof NotIdentical) {
|
||||
return new BooleanNot($endsWithNode);
|
||||
}
|
||||
|
||||
return $endsWithNode;
|
||||
}
|
||||
|
||||
private function refactorSubstrCompare(BinaryOp $binaryOp): ?FuncCall
|
||||
|
|
Loading…
Reference in New Issue