[Php71] Remove PropertyFetchAnalyzer::isFilledByConstructParam (#2122)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-04-22 07:43:25 +07:00 committed by GitHub
parent aa345bc51d
commit 75fccc5d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 84 deletions

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
@ -19,9 +20,11 @@ use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
final class CountableAnalyzer
{
@ -29,7 +32,9 @@ final class CountableAnalyzer
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly ReflectionProvider $reflectionProvider,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}
@ -79,7 +84,7 @@ final class CountableAnalyzer
}
$nativeType = $phpPropertyReflection->getNativeType();
if ($this->isIterableOrFilledByConstructParam($nativeType, $expr)) {
if ($this->isIterableOrFilledAtConstruct($nativeType, $expr)) {
return false;
}
@ -100,13 +105,23 @@ final class CountableAnalyzer
return is_a($typeWithClassName->getClassName(), Array_::class, true);
}
private function isIterableOrFilledByConstructParam(Type $nativeType, PropertyFetch $propertyFetch): bool
private function isIterableOrFilledAtConstruct(Type $nativeType, PropertyFetch $propertyFetch): bool
{
if ($nativeType->isIterable()->yes()) {
return true;
}
return $this->propertyFetchAnalyzer->isFilledByConstructParam($propertyFetch);
$classLike = $this->betterNodeFinder->findParentType($propertyFetch, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return false;
}
if ($propertyFetch->name instanceof Expr) {
return false;
}
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch->name);
return $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
}
private function resolveProperty(

View File

@ -11,10 +11,8 @@ use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Comparing\NodeComparator;
@ -131,44 +129,6 @@ final class PropertyFetchAnalyzer
return $this->isLocalPropertyFetch($node->var);
}
public function isFilledByConstructParam(Property|PropertyFetch|StaticPropertyFetch $property): bool
{
$class = $this->betterNodeFinder->findParentType($property, Class_::class);
if (! $class instanceof Class_) {
return false;
}
$classMethod = $class->getMethod(MethodName::CONSTRUCT);
if (! $classMethod instanceof ClassMethod) {
return false;
}
$params = $classMethod->params;
if ($params === []) {
return false;
}
$stmts = (array) $classMethod->stmts;
if ($stmts === []) {
return false;
}
/** @var string $propertyName */
$propertyName = $property instanceof Property
? $this->nodeNameResolver->getName($property->props[0]->name)
: $this->nodeNameResolver->getName($property);
if ($property instanceof Property) {
$kindPropertyFetch = $property->isStatic()
? StaticPropertyFetch::class
: PropertyFetch::class;
} else {
$kindPropertyFetch = $property::class;
}
return $this->isParamFilledStmts($params, $stmts, $propertyName, $kindPropertyFetch);
}
public function isFilledViaMethodCallInConstructStmts(PropertyFetch $propertyFetch): bool
{
$class = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);
@ -242,44 +202,4 @@ final class PropertyFetchAnalyzer
}
);
}
/**
* @param Param[] $params
* @param Stmt[] $stmts
*/
private function isParamFilledStmts(
array $params,
array $stmts,
string $propertyName,
string $kindPropertyFetch
): bool {
foreach ($params as $param) {
$paramVariable = $param->var;
$isAssignWithParamVarName = $this->betterNodeFinder->findFirst($stmts, function (Node $node) use (
$propertyName,
$paramVariable,
$kindPropertyFetch
): bool {
if (! $node instanceof Assign) {
return false;
}
if ($kindPropertyFetch !== $node->var::class) {
return false;
}
if (! $this->nodeNameResolver->isName($node->var, $propertyName)) {
return false;
}
return $this->nodeComparator->areNodesEqual($node->expr, $paramVariable);
});
if ($isAssignWithParamVarName !== null) {
return true;
}
}
return false;
}
}