2020-12-10 00:40:38 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php80\NodeAnalyzer;
|
2020-12-10 00:40:38 +01:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Comparing\NodeComparator;
|
|
|
|
use Rector\PhpParser\Node\BetterNodeFinder;
|
2021-06-10 10:46:24 +00:00
|
|
|
final class PromotedPropertyCandidateResolver
|
2020-12-10 00:40:38 +01:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2020-12-10 00:40:38 +01:00
|
|
|
*/
|
2024-11-20 15:58:53 +00:00
|
|
|
private NodeNameResolver $nodeNameResolver;
|
2020-12-10 00:40:38 +01:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2020-12-10 00:40:38 +01:00
|
|
|
*/
|
2024-11-20 15:58:53 +00:00
|
|
|
private BetterNodeFinder $betterNodeFinder;
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2020-12-27 02:04:31 +01:00
|
|
|
*/
|
2024-11-20 15:58:53 +00:00
|
|
|
private NodeComparator $nodeComparator;
|
2022-05-08 08:49:04 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-05-08 08:49:04 +00:00
|
|
|
*/
|
2024-11-20 15:58:53 +00:00
|
|
|
private PropertyFetchAnalyzer $propertyFetchAnalyzer;
|
2022-11-28 15:11:02 +00:00
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, NodeComparator $nodeComparator, PropertyFetchAnalyzer $propertyFetchAnalyzer)
|
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;
|
2022-05-08 08:49:04 +00:00
|
|
|
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return PropertyPromotionCandidate[]
|
|
|
|
*/
|
2023-06-05 15:41:37 +00:00
|
|
|
public function resolveFromClass(Class_ $class, ClassMethod $constructClassMethod) : array
|
2020-12-10 00:40:38 +01:00
|
|
|
{
|
|
|
|
$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);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$propertyPromotionCandidate instanceof PropertyPromotionCandidate) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$propertyPromotionCandidates[] = $propertyPromotionCandidate;
|
|
|
|
}
|
|
|
|
return $propertyPromotionCandidates;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function matchPropertyPromotionCandidate(Property $property, ClassMethod $constructClassMethod) : ?PropertyPromotionCandidate
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2024-03-20 14:21:32 +00:00
|
|
|
if ($property->flags === 0) {
|
2024-03-20 14:11:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
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) {
|
2023-06-05 15:41:37 +00:00
|
|
|
if (!$stmt instanceof Expression) {
|
|
|
|
continue;
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
2023-06-05 15:41:37 +00:00
|
|
|
if (!$stmt->expr instanceof Assign) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-05 15:41:37 +00:00
|
|
|
$assign = $stmt->expr;
|
2022-05-08 08:49:04 +00:00
|
|
|
// promoted property must use non-static property only
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$assign->var instanceof PropertyFetch) {
|
2022-05-08 08:49:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$this->propertyFetchAnalyzer->isLocalPropertyFetchName($assign->var, $propertyName)) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// 1. is param
|
|
|
|
$assignedExpr = $assign->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$assignedExpr instanceof Variable) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-10 00:40:38 +01:00
|
|
|
$matchedParam = $this->matchClassMethodParamByAssignedVariable($constructClassMethod, $assignedExpr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$matchedParam instanceof Param) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-11-28 15:11:02 +00:00
|
|
|
if ($this->shouldSkipParam($matchedParam, $assignedExpr, $firstParamAsVariable)) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-05 15:41:37 +00:00
|
|
|
return new PropertyPromotionCandidate($property, $matchedParam, $stmt);
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, int>
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveFirstParamUses(ClassMethod $classMethod) : array
|
2020-12-27 02:04:31 +01:00
|
|
|
{
|
|
|
|
$paramByFirstUsage = [];
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
$paramName = $this->nodeNameResolver->getName($param);
|
2022-06-07 08:22:29 +00:00
|
|
|
$firstParamVariable = $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) use($paramName) : bool {
|
|
|
|
if (!$node instanceof 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);
|
|
|
|
});
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstParamVariable instanceof Node) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$paramByFirstUsage[$paramName] = $firstParamVariable->getStartTokenPos();
|
|
|
|
}
|
|
|
|
return $paramByFirstUsage;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function matchClassMethodParamByAssignedVariable(ClassMethod $classMethod, Variable $variable) : ?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
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isParamUsedBeforeAssign(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
|
|
|
/**
|
|
|
|
* @param int[] $firstParamAsVariable
|
|
|
|
*/
|
2022-11-28 15:11:02 +00:00
|
|
|
private function shouldSkipParam(Param $matchedParam, Variable $assignedVariable, array $firstParamAsVariable) : bool
|
2021-05-22 20:37:33 +00:00
|
|
|
{
|
|
|
|
// already promoted
|
2025-01-05 04:12:03 +00:00
|
|
|
if ($matchedParam->isPromoted()) {
|
2021-05-22 20:37:33 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-11-28 15:11:02 +00:00
|
|
|
return $this->isParamUsedBeforeAssign($assignedVariable, $firstParamAsVariable);
|
2021-05-22 20:37:33 +00:00
|
|
|
}
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|