2020-12-10 00:40:38 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-06-10 10:46:24 +00:00
|
|
|
namespace Rector\Php80\NodeAnalyzer;
|
2020-12-10 00:40:38 +01:00
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
use PhpParser\Node;
|
2020-12-10 00:40:38 +01:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-12-27 02:04:31 +01:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-12-10 00:40:38 +01:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2021-08-24 14:55:08 +00:00
|
|
|
use PHPStan\Type\Generic\TemplateType;
|
2021-08-25 07:50:25 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
2021-05-22 20:37:33 +00:00
|
|
|
use PHPStan\Type\Type;
|
2021-08-20 13:43:25 +00:00
|
|
|
use PHPStan\Type\UnionType;
|
2021-02-19 13:01:23 +01:00
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
2020-12-27 02:04:31 +01:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-12-10 00:40:38 +01:00
|
|
|
use Rector\Core\ValueObject\MethodName;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2021-05-22 20:37:33 +00:00
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
|
|
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
2020-12-10 00:40:38 +01:00
|
|
|
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
|
2021-12-21 23:27:28 +00:00
|
|
|
use Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer;
|
2021-06-10 10:46:24 +00:00
|
|
|
final class PromotedPropertyCandidateResolver
|
2020-12-10 00:40:38 +01:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2020-12-10 00:40:38 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
2020-12-10 00:40:38 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $betterNodeFinder;
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
2020-12-27 02:04:31 +01:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $nodeComparator;
|
2021-05-22 20:37:33 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-12-21 23:27:28 +00:00
|
|
|
* @var \Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer
|
2021-05-22 20:37:33 +00:00
|
|
|
*/
|
2021-12-21 23:27:28 +00:00
|
|
|
private $varDocPropertyTypeInferer;
|
2021-05-22 20:37:33 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-22 20:37:33 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-22 20:37:33 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
|
|
|
*/
|
|
|
|
private $typeComparator;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-22 20:37:33 +00:00
|
|
|
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
|
|
|
*/
|
|
|
|
private $typeFactory;
|
2021-12-21 23:27:28 +00:00
|
|
|
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator, \Rector\TypeDeclaration\TypeInferer\VarDocPropertyTypeInferer $varDocPropertyTypeInferer, \Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\NodeTypeResolver\TypeComparator\TypeComparator $typeComparator, \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory $typeFactory)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-12-10 00:40:38 +01:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2020-12-27 02:04:31 +01:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-02-19 13:01:23 +01:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2021-12-21 23:27:28 +00:00
|
|
|
$this->varDocPropertyTypeInferer = $varDocPropertyTypeInferer;
|
2021-05-22 20:37:33 +00:00
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
|
|
$this->typeComparator = $typeComparator;
|
|
|
|
$this->typeFactory = $typeFactory;
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return PropertyPromotionCandidate[]
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function resolveFromClass(\PhpParser\Node\Stmt\Class_ $class) : array
|
2020-12-10 00:40:38 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$constructClassMethod = $class->getMethod(\Rector\Core\ValueObject\MethodName::CONSTRUCT);
|
|
|
|
if (!$constructClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod) {
|
2020-12-10 00:40:38 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$propertyPromotionCandidates = [];
|
|
|
|
foreach ($class->getProperties() as $property) {
|
2021-06-10 10:46:24 +00:00
|
|
|
$propertyCount = \count($property->props);
|
|
|
|
if ($propertyCount !== 1) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$propertyPromotionCandidate = $this->matchPropertyPromotionCandidate($property, $constructClassMethod);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$propertyPromotionCandidate instanceof \Rector\Php80\ValueObject\PropertyPromotionCandidate) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$propertyPromotionCandidates[] = $propertyPromotionCandidate;
|
|
|
|
}
|
|
|
|
return $propertyPromotionCandidates;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function matchPropertyPromotionCandidate(\PhpParser\Node\Stmt\Property $property, \PhpParser\Node\Stmt\ClassMethod $constructClassMethod) : ?\Rector\Php80\ValueObject\PropertyPromotionCandidate
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-12-10 00:40:38 +01:00
|
|
|
$onlyProperty = $property->props[0];
|
|
|
|
$propertyName = $this->nodeNameResolver->getName($onlyProperty);
|
2020-12-27 02:04:31 +01:00
|
|
|
$firstParamAsVariable = $this->resolveFirstParamUses($constructClassMethod);
|
2020-12-10 00:40:38 +01:00
|
|
|
// match property name to assign in constructor
|
|
|
|
foreach ((array) $constructClassMethod->stmts as $stmt) {
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($stmt instanceof \PhpParser\Node\Stmt\Expression) {
|
2020-12-10 00:40:38 +01:00
|
|
|
$stmt = $stmt->expr;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$stmt instanceof \PhpParser\Node\Expr\Assign) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$assign = $stmt;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeNameResolver->isLocalPropertyFetchNamed($assign->var, $propertyName)) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 1. is param
|
|
|
|
$assignedExpr = $assign->expr;
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$assignedExpr instanceof \PhpParser\Node\Expr\Variable) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-10 00:40:38 +01:00
|
|
|
$matchedParam = $this->matchClassMethodParamByAssignedVariable($constructClassMethod, $assignedExpr);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$matchedParam instanceof \PhpParser\Node\Param) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-22 20:37:33 +00:00
|
|
|
if ($this->shouldSkipParam($matchedParam, $property, $assignedExpr, $firstParamAsVariable)) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Rector\Php80\ValueObject\PropertyPromotionCandidate($property, $assign, $matchedParam);
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, int>
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function resolveFirstParamUses(\PhpParser\Node\Stmt\ClassMethod $classMethod) : array
|
2020-12-27 02:04:31 +01:00
|
|
|
{
|
|
|
|
$paramByFirstUsage = [];
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
$paramName = $this->nodeNameResolver->getName($param);
|
2021-05-10 22:23:08 +00:00
|
|
|
$firstParamVariable = $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (\PhpParser\Node $node) use($paramName) : bool {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Expr\Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-27 02:04:31 +01:00
|
|
|
}
|
|
|
|
return $this->nodeNameResolver->isName($node, $paramName);
|
|
|
|
});
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$firstParamVariable instanceof \PhpParser\Node) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$paramByFirstUsage[$paramName] = $firstParamVariable->getStartTokenPos();
|
|
|
|
}
|
|
|
|
return $paramByFirstUsage;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function matchClassMethodParamByAssignedVariable(\PhpParser\Node\Stmt\ClassMethod $classMethod, \PhpParser\Node\Expr\Variable $variable) : ?\PhpParser\Node\Param
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-01-22 19:47:02 +01:00
|
|
|
foreach ($classMethod->params as $param) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($variable, $param->var)) {
|
2021-01-22 19:47:02 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @param array<string, int> $firstParamAsVariable
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function isParamUsedBeforeAssign(\PhpParser\Node\Expr\Variable $variable, array $firstParamAsVariable) : bool
|
2020-12-27 02:04:31 +01:00
|
|
|
{
|
|
|
|
$variableName = $this->nodeNameResolver->getName($variable);
|
|
|
|
$firstVariablePosition = $firstParamAsVariable[$variableName] ?? null;
|
|
|
|
if ($firstVariablePosition === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-27 02:04:31 +01:00
|
|
|
}
|
|
|
|
return $firstVariablePosition < $variable->getStartTokenPos();
|
|
|
|
}
|
2021-05-22 20:37:33 +00:00
|
|
|
private function hasConflictingParamType(\PhpParser\Node\Param $param, \PHPStan\Type\Type $propertyType) : bool
|
|
|
|
{
|
|
|
|
if ($param->type === null) {
|
|
|
|
return \false;
|
|
|
|
}
|
2021-10-07 19:40:12 +00:00
|
|
|
$matchedParamType = $this->nodeTypeResolver->getType($param);
|
2021-05-22 20:37:33 +00:00
|
|
|
if ($param->default !== null) {
|
2021-10-07 18:52:22 +00:00
|
|
|
$defaultValueType = $this->nodeTypeResolver->getType($param->default);
|
2021-05-22 20:37:33 +00:00
|
|
|
$matchedParamType = $this->typeFactory->createMixedPassedOrUnionType([$matchedParamType, $defaultValueType]);
|
|
|
|
}
|
2021-08-25 07:50:25 +00:00
|
|
|
if (!$propertyType instanceof \PHPStan\Type\UnionType) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
if ($this->typeComparator->areTypesEqual($propertyType, $matchedParamType)) {
|
|
|
|
return \false;
|
2021-08-25 07:00:32 +00:00
|
|
|
}
|
2021-08-25 07:50:25 +00:00
|
|
|
// different types, check not has mixed and not has templated generic types
|
|
|
|
if (!$this->hasMixedType($propertyType)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
return !$this->hasTemplatedGenericType($propertyType);
|
2021-05-22 20:37:33 +00:00
|
|
|
}
|
2021-08-25 07:50:25 +00:00
|
|
|
private function hasTemplatedGenericType(\PHPStan\Type\UnionType $unionType) : bool
|
2021-08-24 14:55:08 +00:00
|
|
|
{
|
|
|
|
foreach ($unionType->getTypes() as $type) {
|
2021-08-25 07:50:25 +00:00
|
|
|
if ($type instanceof \PHPStan\Type\Generic\TemplateType) {
|
2021-08-24 14:55:08 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2021-08-25 07:50:25 +00:00
|
|
|
private function hasMixedType(\PHPStan\Type\UnionType $unionType) : bool
|
2021-08-24 14:55:08 +00:00
|
|
|
{
|
|
|
|
foreach ($unionType->getTypes() as $type) {
|
2021-08-25 07:50:25 +00:00
|
|
|
if ($type instanceof \PHPStan\Type\MixedType) {
|
2021-08-24 14:55:08 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2021-05-22 20:37:33 +00:00
|
|
|
/**
|
|
|
|
* @param int[] $firstParamAsVariable
|
|
|
|
*/
|
|
|
|
private function shouldSkipParam(\PhpParser\Node\Param $matchedParam, \PhpParser\Node\Stmt\Property $property, \PhpParser\Node\Expr\Variable $assignedVariable, array $firstParamAsVariable) : bool
|
|
|
|
{
|
|
|
|
// already promoted
|
|
|
|
if ($matchedParam->flags !== 0) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-08-24 14:55:08 +00:00
|
|
|
if ($this->isParamUsedBeforeAssign($assignedVariable, $firstParamAsVariable)) {
|
2021-05-22 20:37:33 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2021-08-24 14:55:08 +00:00
|
|
|
// @todo unknown type, not suitable?
|
2021-12-21 23:27:28 +00:00
|
|
|
$propertyType = $this->varDocPropertyTypeInferer->inferProperty($property);
|
2021-08-24 14:55:08 +00:00
|
|
|
return $this->hasConflictingParamType($matchedParam, $propertyType);
|
2021-05-22 20:37:33 +00:00
|
|
|
}
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|