nodeNameResolver = $nodeNameResolver;
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeComparator = $nodeComparator;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
}
/**
* @return PropertyPromotionCandidate[]
*/
public function resolveFromClass(Class_ $class, ClassMethod $constructClassMethod) : array
{
$propertyPromotionCandidates = [];
foreach ($class->getProperties() as $property) {
$propertyCount = \count($property->props);
if ($propertyCount !== 1) {
continue;
}
$propertyPromotionCandidate = $this->matchPropertyPromotionCandidate($property, $constructClassMethod);
if (!$propertyPromotionCandidate instanceof PropertyPromotionCandidate) {
continue;
}
$propertyPromotionCandidates[] = $propertyPromotionCandidate;
}
return $propertyPromotionCandidates;
}
private function matchPropertyPromotionCandidate(Property $property, ClassMethod $constructClassMethod) : ?PropertyPromotionCandidate
{
if ($property->flags === 0) {
return null;
}
$onlyProperty = $property->props[0];
$propertyName = $this->nodeNameResolver->getName($onlyProperty);
$firstParamAsVariable = $this->resolveFirstParamUses($constructClassMethod);
// match property name to assign in constructor
foreach ((array) $constructClassMethod->stmts as $stmt) {
if (!$stmt instanceof Expression) {
continue;
}
if (!$stmt->expr instanceof Assign) {
continue;
}
$assign = $stmt->expr;
// promoted property must use non-static property only
if (!$assign->var instanceof PropertyFetch) {
continue;
}
if (!$this->propertyFetchAnalyzer->isLocalPropertyFetchName($assign->var, $propertyName)) {
continue;
}
// 1. is param
$assignedExpr = $assign->expr;
if (!$assignedExpr instanceof Variable) {
continue;
}
$matchedParam = $this->matchClassMethodParamByAssignedVariable($constructClassMethod, $assignedExpr);
if (!$matchedParam instanceof Param) {
continue;
}
if ($this->shouldSkipParam($matchedParam, $assignedExpr, $firstParamAsVariable)) {
continue;
}
return new PropertyPromotionCandidate($property, $matchedParam, $stmt);
}
return null;
}
/**
* @return array