mirror of https://github.com/rectorphp/rector.git
102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\NodeTypeResolver\NodeTypeCorrector;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Arg;
|
|
use PhpParser\Node\Expr;
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PHPStan\Type\ArrayType;
|
|
use PHPStan\Type\MixedType;
|
|
use PHPStan\Type\Type;
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
use Rector\NodeNestingScope\ParentScopeFinder;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
final class PregMatchTypeCorrector
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
|
*/
|
|
private $betterNodeFinder;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
|
*/
|
|
private $nodeNameResolver;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\NodeNestingScope\ParentScopeFinder
|
|
*/
|
|
private $parentScopeFinder;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
|
*/
|
|
private $nodeComparator;
|
|
public function __construct(BetterNodeFinder $betterNodeFinder, NodeNameResolver $nodeNameResolver, ParentScopeFinder $parentScopeFinder, NodeComparator $nodeComparator)
|
|
{
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
$this->parentScopeFinder = $parentScopeFinder;
|
|
$this->nodeComparator = $nodeComparator;
|
|
}
|
|
/**
|
|
* Special case for "preg_match(), preg_match_all()" - with 3rd argument
|
|
* @see https://github.com/rectorphp/rector/issues/786
|
|
*/
|
|
public function correct(Expr $expr, Type $originalType) : Type
|
|
{
|
|
if (!$expr instanceof Variable) {
|
|
return $originalType;
|
|
}
|
|
if ($originalType instanceof ArrayType) {
|
|
return $originalType;
|
|
}
|
|
$variableUsages = $this->getVariableUsages($expr);
|
|
foreach ($variableUsages as $variableUsage) {
|
|
$possiblyArg = $variableUsage->getAttribute(AttributeKey::PARENT_NODE);
|
|
if (!$possiblyArg instanceof Arg) {
|
|
continue;
|
|
}
|
|
$funcCallNode = $possiblyArg->getAttribute(AttributeKey::PARENT_NODE);
|
|
if (!$funcCallNode instanceof FuncCall) {
|
|
continue;
|
|
}
|
|
$thirdArg = $funcCallNode->getArgs()[2] ?? null;
|
|
if (!$thirdArg instanceof Arg) {
|
|
continue;
|
|
}
|
|
if (!$this->nodeNameResolver->isNames($funcCallNode, ['preg_match', 'preg_match_all'])) {
|
|
continue;
|
|
}
|
|
// are the same variables
|
|
if (!$this->nodeComparator->areNodesEqual($thirdArg->value, $expr)) {
|
|
continue;
|
|
}
|
|
return new ArrayType(new MixedType(), new MixedType());
|
|
}
|
|
return $originalType;
|
|
}
|
|
/**
|
|
* @return Node[]
|
|
*/
|
|
private function getVariableUsages(Variable $variable) : array
|
|
{
|
|
$scope = $this->parentScopeFinder->find($variable);
|
|
if ($scope === null) {
|
|
return [];
|
|
}
|
|
return $this->betterNodeFinder->find((array) $scope->stmts, static function (Node $node) use($variable) : bool {
|
|
if (!$node instanceof Variable) {
|
|
return \false;
|
|
}
|
|
return $node->name === $variable->name;
|
|
});
|
|
}
|
|
}
|