2020-06-02 07:28:51 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-06-02 07:28:51 +00:00
|
|
|
namespace Rector\Php80\NodeManipulator;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2020-06-29 21:19:37 +00:00
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2020-06-02 07:28:51 +00:00
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2020-06-29 21:19:37 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-06-02 07:28:51 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PHPStan\Type\ArrayType;
|
2021-09-27 15:43:15 +00:00
|
|
|
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
|
2021-02-19 12:01:23 +00:00
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
2020-06-02 07:28:51 +00:00
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
2021-11-28 16:42:02 +00:00
|
|
|
use Rector\Core\Util\StringUtils;
|
2020-06-02 07:28:51 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
2020-08-23 09:39:09 +00:00
|
|
|
use Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch;
|
2020-06-02 07:28:51 +00:00
|
|
|
use Rector\PostRector\Collector\NodesToRemoveCollector;
|
2021-12-04 07:57:42 +00:00
|
|
|
use RectorPrefix20211204\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
2020-06-02 07:28:51 +00:00
|
|
|
final class TokenManipulator
|
|
|
|
{
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PhpParser\Node\Expr|null
|
2020-06-02 07:28:51 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $assignedNameExpr;
|
2020-06-02 07:28:51 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
|
2020-06-02 07:28:51 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $simpleCallableNodeTraverser;
|
2020-06-02 07:28:51 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2020-06-02 07:28:51 +00:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
2020-06-02 07:28:51 +00:00
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\PostRector\Collector\NodesToRemoveCollector
|
2020-06-02 07:28:51 +00:00
|
|
|
*/
|
|
|
|
private $nodesToRemoveCollector;
|
2020-07-31 20:20:45 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
2020-07-31 20:20:45 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $valueResolver;
|
|
|
|
/**
|
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
|
|
|
*/
|
|
|
|
private $nodeComparator;
|
2021-09-27 15:43:15 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\Core\NodeAnalyzer\ArgsAnalyzer
|
|
|
|
*/
|
|
|
|
private $argsAnalyzer;
|
2021-12-04 07:57:42 +00:00
|
|
|
public function __construct(\RectorPrefix20211204\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser $simpleCallableNodeTraverser, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\PostRector\Collector\NodesToRemoveCollector $nodesToRemoveCollector, \Rector\Core\PhpParser\Node\Value\ValueResolver $valueResolver, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator, \Rector\Core\NodeAnalyzer\ArgsAnalyzer $argsAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-16 21:45:18 +00:00
|
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
2020-06-02 07:28:51 +00:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
|
|
$this->nodesToRemoveCollector = $nodesToRemoveCollector;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2021-02-19 12:01:23 +00:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2021-09-27 15:43:15 +00:00
|
|
|
$this->argsAnalyzer = $argsAnalyzer;
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function refactorArrayToken(array $nodes, \PhpParser\Node\Expr $singleTokenExpr) : void
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
|
|
|
$this->replaceTokenDimFetchZeroWithGetTokenName($nodes, $singleTokenExpr);
|
|
|
|
// replace "$token[1]"; with "$token->value"
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) : ?PropertyFetch {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
|
2020-12-20 21:05:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isArrayDimFetchWithDimIntegerValue($node, 1)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 18:52:22 +00:00
|
|
|
$tokenStaticType = $this->nodeTypeResolver->getType($node->var);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$tokenStaticType instanceof \PHPStan\Type\ArrayType) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Expr\PropertyFetch($node->var, 'text');
|
2020-06-02 07:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function refactorNonArrayToken(array $nodes, \PhpParser\Node\Expr $singleTokenExpr) : void
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
|
|
|
// replace "$content = $token;" → "$content = $token->text;"
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) use($singleTokenExpr) {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\Assign) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($node->expr, $singleTokenExpr)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 18:52:22 +00:00
|
|
|
$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($tokenStaticType instanceof \PHPStan\Type\ArrayType) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->expr = new \PhpParser\Node\Expr\PropertyFetch($singleTokenExpr, 'text');
|
2020-06-02 07:28:51 +00:00
|
|
|
});
|
|
|
|
// replace "$name = null;" → "$name = $token->getTokenName();"
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) use($singleTokenExpr) : ?Assign {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\Assign) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 18:52:22 +00:00
|
|
|
$tokenStaticType = $this->nodeTypeResolver->getType($node->expr);
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($tokenStaticType instanceof \PHPStan\Type\ArrayType) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->assignedNameExpr === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($node->var, $this->assignedNameExpr)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($node->expr, 'null')) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->expr = new \PhpParser\Node\Expr\MethodCall($singleTokenExpr, 'getTokenName');
|
2020-06-02 07:28:51 +00:00
|
|
|
return $node;
|
|
|
|
});
|
|
|
|
}
|
2020-10-11 14:17:43 +00:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function refactorTokenIsKind(array $nodes, \PhpParser\Node\Expr $singleTokenExpr) : void
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) use($singleTokenExpr) : ?MethodCall {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\BinaryOp\Identical) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-08-23 09:39:09 +00:00
|
|
|
$arrayDimFetchAndConstFetch = $this->matchArrayDimFetchAndConstFetch($node);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$arrayDimFetchAndConstFetch instanceof \Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isArrayDimFetchWithDimIntegerValue($arrayDimFetchAndConstFetch->getArrayDimFetch(), 0)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-08-23 09:39:09 +00:00
|
|
|
$arrayDimFetch = $arrayDimFetchAndConstFetch->getArrayDimFetch();
|
|
|
|
$constFetch = $arrayDimFetchAndConstFetch->getConstFetch();
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($arrayDimFetch->var, $singleTokenExpr)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$constName = $this->nodeNameResolver->getName($constFetch);
|
|
|
|
if ($constName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-28 16:42:02 +00:00
|
|
|
if (!\Rector\Core\Util\StringUtils::isMatch($constName, '#^T_#')) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return $this->createIsTConstTypeMethodCall($arrayDimFetch, $arrayDimFetchAndConstFetch->getConstFetch());
|
2020-06-02 07:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function removeIsArray(array $nodes, \PhpParser\Node\Expr\Variable $singleTokenVariable) : void
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) use($singleTokenVariable) {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\FuncCall) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeNameResolver->isName($node, 'is_array')) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
if (!$this->argsAnalyzer->isArgInstanceInArgsPosition($node->args, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Arg $firstArg */
|
|
|
|
$firstArg = $node->args[0];
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($firstArg->value, $singleTokenVariable)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->shouldSkipNodeRemovalForPartOfIf($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// remove correct node
|
|
|
|
$nodeToRemove = $this->matchParentNodeInCaseOfIdenticalTrue($node);
|
|
|
|
$this->nodesToRemoveCollector->addNodeToRemove($nodeToRemove);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Replace $token[0] with $token->getTokenName() call
|
|
|
|
*
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function replaceTokenDimFetchZeroWithGetTokenName(array $nodes, \PhpParser\Node\Expr $singleTokenExpr) : void
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, function (\PhpParser\Node $node) use($singleTokenExpr) : ?MethodCall {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\FuncCall) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeNameResolver->isName($node, 'token_name')) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-09-27 15:43:15 +00:00
|
|
|
if (!$this->argsAnalyzer->isArgInstanceInArgsPosition($node->args, 0)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Arg $firstArg */
|
|
|
|
$firstArg = $node->args[0];
|
|
|
|
$possibleTokenArray = $firstArg->value;
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$possibleTokenArray instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-10-07 18:52:22 +00:00
|
|
|
$tokenStaticType = $this->nodeTypeResolver->getType($possibleTokenArray->var);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$tokenStaticType instanceof \PHPStan\Type\ArrayType) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($possibleTokenArray->dim === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isValue($possibleTokenArray->dim, 0)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($possibleTokenArray->var, $singleTokenExpr)) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// save token variable name for later
|
2021-05-10 22:23:08 +00:00
|
|
|
$parentNode = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\Assign) {
|
2020-06-02 07:28:51 +00:00
|
|
|
$this->assignedNameExpr = $parentNode->var;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Expr\MethodCall($singleTokenExpr, 'getTokenName');
|
2020-06-02 07:28:51 +00:00
|
|
|
});
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isArrayDimFetchWithDimIntegerValue(\PhpParser\Node\Expr\ArrayDimFetch $arrayDimFetch, int $value) : bool
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-01-19 23:29:52 +00:00
|
|
|
if ($arrayDimFetch->dim === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
2021-01-19 23:29:52 +00:00
|
|
|
return $this->valueResolver->isValue($arrayDimFetch->dim, $value);
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function matchArrayDimFetchAndConstFetch(\PhpParser\Node\Expr\BinaryOp\Identical $identical) : ?\Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($identical->left instanceof \PhpParser\Node\Expr\ArrayDimFetch && $identical->right instanceof \PhpParser\Node\Expr\ConstFetch) {
|
|
|
|
return new \Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch($identical->left, $identical->right);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$identical->right instanceof \PhpParser\Node\Expr\ArrayDimFetch) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$identical->left instanceof \PhpParser\Node\Expr\ConstFetch) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Rector\Php80\ValueObject\ArrayDimFetchAndConstFetch($identical->right, $identical->left);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function createIsTConstTypeMethodCall(\PhpParser\Node\Expr\ArrayDimFetch $arrayDimFetch, \PhpParser\Node\Expr\ConstFetch $constFetch) : \PhpParser\Node\Expr\MethodCall
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PhpParser\Node\Expr\MethodCall($arrayDimFetch->var, 'is', [new \PhpParser\Node\Arg($constFetch)]);
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function shouldSkipNodeRemovalForPartOfIf(\PhpParser\Node\Expr\FuncCall $funcCall) : bool
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$parentNode = $funcCall->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
2020-06-02 07:28:51 +00:00
|
|
|
// cannot remove x from if(x)
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($parentNode instanceof \PhpParser\Node\Stmt\If_ && $parentNode->cond === $funcCall) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\BooleanNot) {
|
|
|
|
$parentParentNode = $parentNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentParentNode instanceof \PhpParser\Node\Stmt\If_) {
|
2020-06-02 07:28:51 +00:00
|
|
|
$parentParentNode->cond = $parentNode;
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-06-02 07:28:51 +00:00
|
|
|
}
|
2021-06-18 17:31:16 +00:00
|
|
|
/**
|
|
|
|
* @return \PhpParser\Node\Expr\BinaryOp\Identical|\PhpParser\Node\Expr\FuncCall
|
|
|
|
*/
|
|
|
|
private function matchParentNodeInCaseOfIdenticalTrue(\PhpParser\Node\Expr\FuncCall $funcCall)
|
2020-06-02 07:28:51 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$parentNode = $funcCall->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\BinaryOp\Identical) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$isRightValueTrue = $this->valueResolver->isValue($parentNode->right, \true);
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($parentNode->left === $funcCall && $isRightValueTrue) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return $parentNode;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$isLeftValueTrue = $this->valueResolver->isValue($parentNode->left, \true);
|
2021-01-30 23:20:05 +00:00
|
|
|
if ($parentNode->right === $funcCall && $isLeftValueTrue) {
|
2020-06-02 07:28:51 +00:00
|
|
|
return $parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $funcCall;
|
|
|
|
}
|
|
|
|
}
|