Skip to content

Commit

Permalink
fixup! use php version right in config
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 9, 2021
1 parent d69a82c commit a8d16cf
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/NodeManipulator/ClassDependencyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\ValueObject\PropertyMetadata;

Expand Down Expand Up @@ -57,6 +58,10 @@ final class ClassDependencyManipulator
* @var PropertyPresenceChecker
*/
private $propertyPresenceChecker;
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;

public function __construct(
ChildAndParentClassManipulator $childAndParentClassManipulator,
Expand All @@ -65,7 +70,8 @@ public function __construct(
NodeFactory $nodeFactory,
StmtsManipulator $stmtsManipulator,
PhpVersionProvider $phpVersionProvider,
PropertyPresenceChecker $propertyPresenceChecker
PropertyPresenceChecker $propertyPresenceChecker,
NodeNameResolver $nodeNameResolver
) {
$this->classMethodAssignManipulator = $classMethodAssignManipulator;
$this->nodeFactory = $nodeFactory;
Expand All @@ -74,12 +80,15 @@ public function __construct(
$this->classInsertManipulator = $classInsertManipulator;
$this->phpVersionProvider = $phpVersionProvider;
$this->propertyPresenceChecker = $propertyPresenceChecker;
$this->nodeNameResolver = $nodeNameResolver;
}

public function addConstructorDependency(Class_ $class, PropertyMetadata $propertyMetadata): void
{
if ($this->propertyPresenceChecker->hasClassContextPropertyByName($class, $propertyMetadata->getName())) {
return;
if ($this->isParamInConstructor($class, $propertyMetadata->getName())) {
return;
}
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::PROPERTY_PROMOTION)) {
Expand Down Expand Up @@ -204,4 +213,20 @@ private function createParentClassMethodCall(string $methodName): Expression

return new Expression($staticCall);
}

private function isParamInConstructor(Class_ $class, string $propertyName): bool
{
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return false;
}

foreach ($constructClassMethod->params as $param) {
if ($this->nodeNameResolver->isName($param, $propertyName)) {
return true;
}
}

return false;
}
}

0 comments on commit a8d16cf

Please sign in to comment.