-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2c7dba
commit 0fec032
Showing
44 changed files
with
837 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
services: | ||
Rector\Php80\Rector\FunctionLike\UnionTypesRector: null | ||
Rector\Php80\Rector\NotIdentical\StrContainsRector: null | ||
Rector\Php80\Rector\Identical\StrStartsWithRector: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
services: | ||
_defaults: | ||
public: true | ||
autowire: true | ||
|
||
Rector\Php80\: | ||
resource: '../src' | ||
exclude: | ||
- '../src/Rector/**/*Rector.php' | ||
- '../src/ValueObject/*' |
51 changes: 51 additions & 0 deletions
51
rules/php80/src/MatchAndRefactor/AbstractMatchAndRefactor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\MatchAndRefactor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use Rector\Core\PhpParser\Node\Value\ValueResolver; | ||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
abstract class AbstractMatchAndRefactor | ||
{ | ||
/** | ||
* @var NodeNameResolver | ||
*/ | ||
protected $nodeNameResolver; | ||
|
||
/** | ||
* @var ValueResolver | ||
*/ | ||
protected $valueResolver; | ||
|
||
/** | ||
* @var BetterStandardPrinter | ||
*/ | ||
protected $betterStandardPrinter; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public function autowireAbstractMatchAndRefactor( | ||
NodeNameResolver $nodeNameResolver, | ||
ValueResolver $valueResolver, | ||
BetterStandardPrinter $betterStandardPrinter | ||
): void { | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
$this->valueResolver = $valueResolver; | ||
$this->betterStandardPrinter = $betterStandardPrinter; | ||
} | ||
|
||
protected function isFuncCallName(Node $node, string $name): bool | ||
{ | ||
if (! $node instanceof FuncCall) { | ||
return false; | ||
} | ||
|
||
return $this->nodeNameResolver->isName($node, $name); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
rules/php80/src/MatchAndRefactor/StrncmpMatchAndRefactor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\MatchAndRefactor; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
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\Name; | ||
use Rector\Php80\ValueObject\StrncmpFuncCallToHaystack; | ||
|
||
final class StrncmpMatchAndRefactor extends AbstractMatchAndRefactor | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private const FUNCTION_NAME = 'strncmp'; | ||
|
||
/** | ||
* @param Identical|NotIdentical $binaryOp | ||
*/ | ||
public function match(BinaryOp $binaryOp): ?StrncmpFuncCallToHaystack | ||
{ | ||
$isPositive = $binaryOp instanceof Identical ? true : false; | ||
|
||
if ($this->isFuncCallName($binaryOp->left, self::FUNCTION_NAME)) { | ||
/** @var FuncCall $funcCall */ | ||
$funcCall = $binaryOp->left; | ||
return new StrncmpFuncCallToHaystack($funcCall, $isPositive); | ||
} | ||
|
||
if ($this->isFuncCallName($binaryOp->right, self::FUNCTION_NAME)) { | ||
/** @var FuncCall $funcCall */ | ||
$funcCall = $binaryOp->right; | ||
return new StrncmpFuncCallToHaystack($funcCall, $isPositive); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function refactor(StrncmpFuncCallToHaystack $strncmpFuncCallToHaystack): ?Node | ||
{ | ||
$strncmpFuncCall = $strncmpFuncCallToHaystack->getStrncmpFuncCall(); | ||
|
||
$needleExpr = $strncmpFuncCall->args[1]->value; | ||
|
||
if (! $this->isFuncCallName($strncmpFuncCall->args[2]->value, 'strlen')) { | ||
return null; | ||
} | ||
|
||
/** @var FuncCall $strlenFuncCall */ | ||
$strlenFuncCall = $strncmpFuncCall->args[2]->value; | ||
$strlenArgumentValue = $strlenFuncCall->args[0]->value; | ||
|
||
if (! $this->betterStandardPrinter->areNodesEqual($needleExpr, $strlenArgumentValue)) { | ||
return null; | ||
} | ||
|
||
$strStartsWith = $this->createStrStartsWith($strncmpFuncCall, $needleExpr); | ||
if ($strncmpFuncCallToHaystack->isPositive()) { | ||
return $strStartsWith; | ||
} | ||
|
||
return new BooleanNot($strStartsWith); | ||
} | ||
|
||
private function createStrStartsWith(FuncCall $strncmpFuncCall, Expr $needleExpr): FuncCall | ||
{ | ||
$haystackExpr = $strncmpFuncCall->args[0]->value; | ||
$args = [new Arg($haystackExpr), new Arg($needleExpr)]; | ||
|
||
return new FuncCall(new Name('str_starts_with'), $args); | ||
} | ||
} |
Oops, something went wrong.