rector/packages/NodeTypeResolver/NodeTypeCorrector/PregMatchTypeCorrector.php
Abdul Malik Ikhsan fc10fce13d
[Rectify] [Php81] Enable Rectify on Readonly Property only (#1384)
* re-enable rectify and ecs

* [Rectify] [Php81] Enable Rectify on Readonly Property only

* comment

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
2021-12-04 15:32:52 +03:00

100 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\NodeTypeCorrector;
use PhpParser\Node;
use PhpParser\Node\Arg;
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\NodeAnalyzer\ArgsAnalyzer;
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
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ParentScopeFinder $parentScopeFinder,
private readonly NodeComparator $nodeComparator,
private readonly ArgsAnalyzer $argsAnalyzer
) {
}
/**
* Special case for "preg_match(), preg_match_all()" - with 3rd argument
* @see https://github.com/rectorphp/rector/issues/786
*/
public function correct(Node $node, Type $originalType): Type
{
if (! $node instanceof Variable) {
return $originalType;
}
if ($originalType instanceof ArrayType) {
return $originalType;
}
$variableUsages = $this->getVariableUsages($node);
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;
}
if (! $this->nodeNameResolver->isNames($funcCallNode, ['preg_match', 'preg_match_all'])) {
continue;
}
if (! $this->argsAnalyzer->isArgInstanceInArgsPosition($funcCallNode->args, 2)) {
continue;
}
/** @var Arg $thirdArg */
$thirdArg = $funcCallNode->args[2];
// are the same variables
if (! $this->nodeComparator->areNodesEqual($thirdArg->value, $node)) {
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, function (Node $node) use ($variable): bool {
if (! $node instanceof Variable) {
return false;
}
return $node->name === $variable->name;
});
}
}