Skip to content

Commit

Permalink
[Core] Remove unused PropertyManipulator::isInlineStmtWithConstructMe…
Browse files Browse the repository at this point in the history
…thod() (#2056)

* [Core] Remove unused PropertyManipulator::isInlineStmtWithConstructMethod()

* clean up

* fix

* clean up
  • Loading branch information
samsonasik committed Apr 11, 2022
1 parent f00dfc0 commit 255a178
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -41,6 +40,7 @@
use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver;
use Rector\ReadWrite\Guard\VariableToConstantGuard;
use Rector\ReadWrite\NodeAnalyzer\ReadWritePropertyAnalyzer;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\PackageBuilder\Php\TypeChecker;

/**
Expand Down Expand Up @@ -82,7 +82,8 @@ public function __construct(
private readonly NodeNameResolver $nodeNameResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PromotedPropertyResolver $promotedPropertyResolver
private readonly PromotedPropertyResolver $promotedPropertyResolver,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -167,8 +168,10 @@ public function isPropertyChangeableExceptConstructor(Property | Param $property
}

// skip for constructor? it is allowed to set value in constructor method
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch);
$classMethod = $this->betterNodeFinder->findParentType($propertyFetch, ClassMethod::class);
if ($this->isInlineStmtWithConstructMethod($propertyFetch, $classMethod)) {

if ($this->isPropertyAssignedOnlyInConstructor($classLike, $propertyName, $classMethod)) {
continue;
}

Expand Down Expand Up @@ -226,25 +229,21 @@ public function resolveExistingClassPropertyNameByType(Class_ $class, Type $type
return null;
}

public function isInlineStmtWithConstructMethod(
PropertyFetch|StaticPropertyFetch $propertyFetch,
private function isPropertyAssignedOnlyInConstructor(
ClassLike $classLike,
string $propertyName,
?ClassMethod $classMethod
): bool {
if (! $classMethod instanceof ClassMethod) {
return false;
}

// there is property unset in Test class, so only check on __construct
if (! $this->nodeNameResolver->isName($classMethod->name, MethodName::CONSTRUCT)) {
return false;
}

$currentStmt = $propertyFetch->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentStmt instanceof Stmt) {
return false;
}

$parent = $currentStmt->getAttribute(AttributeKey::PARENT_NODE);
return $parent === $classMethod;
return $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
}

private function isChangeableContext(PropertyFetch | StaticPropertyFetch $propertyFetch): bool
Expand Down

0 comments on commit 255a178

Please sign in to comment.