Skip to content

Commit

Permalink
[Php71] Remove PropertyFetchAnalyzer::isFilledByConstructParam (#2122)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 22, 2022
1 parent aa345bc commit 75fccc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 84 deletions.
23 changes: 19 additions & 4 deletions rules/Php71/NodeAnalyzer/CountableAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -19,17 +20,21 @@
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
{
public function __construct(
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
) {
}

Expand Down Expand Up @@ -79,7 +84,7 @@ public function isCastableArrayType(Expr $expr, ArrayType $arrayType): bool
}

$nativeType = $phpPropertyReflection->getNativeType();
if ($this->isIterableOrFilledByConstructParam($nativeType, $expr)) {
if ($this->isIterableOrFilledAtConstruct($nativeType, $expr)) {
return false;
}

Expand All @@ -100,13 +105,23 @@ private function isCallerObjectClassNameStmtOrArray(TypeWithClassName $typeWithC
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(
Expand Down
80 changes: 0 additions & 80 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
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;
Expand Down Expand Up @@ -131,44 +129,6 @@ public function isVariableAssignToThisPropertyFetch(Node $node, string $variable
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);
Expand Down Expand Up @@ -242,44 +202,4 @@ function (Node $subNode) use ($propertyFetch): bool {
}
);
}

/**
* @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;
}
}

0 comments on commit 75fccc5

Please sign in to comment.