Skip to content

Commit

Permalink
Cleanup NodeRepository (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 28, 2021
1 parent 1a87a3d commit d8a7f6c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 124 deletions.
58 changes: 10 additions & 48 deletions packages/NodeCollector/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@

namespace Rector\NodeCollector\NodeCollector;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;

/**
* This service contains all the parsed nodes. E.g. all the functions, method call, classes, static calls etc. It's
Expand All @@ -28,31 +22,17 @@ public function __construct(
private NodeNameResolver $nodeNameResolver,
private ParsedNodeCollector $parsedNodeCollector,
private ReflectionProvider $reflectionProvider,
private NodeTypeResolver $nodeTypeResolver
) {
}

public function hasClassChildren(Class_ $desiredClass): bool
{
$desiredClassName = $desiredClass->getAttribute(AttributeKey::CLASS_NAME);
$desiredClassName = $this->nodeNameResolver->getName($desiredClass);
if ($desiredClassName === null) {
return false;
}

foreach ($this->parsedNodeCollector->getClasses() as $classNode) {
$currentClassName = $classNode->getAttribute(AttributeKey::CLASS_NAME);
if ($currentClassName === null) {
continue;
}

if (! $this->isChildOrEqualClassLike($desiredClassName, $currentClassName)) {
continue;
}

return true;
}

return false;
return $this->findChildrenOfClass($desiredClassName) !== [];
}

/**
Expand Down Expand Up @@ -85,6 +65,7 @@ public function findClassesAndInterfacesByType(string $type): array
}

/**
* @param class-string $class
* @return Class_[]
*/
public function findChildrenOfClass(string $class): array
Expand All @@ -103,40 +84,20 @@ public function findChildrenOfClass(string $class): array
return $childrenClasses;
}

/**
* @param class-string $class
*/
public function findInterface(string $class): ?Interface_
{
return $this->parsedNodeCollector->findInterface($class);
}

public function findClass(string $name): ?Class_
{
return $this->parsedNodeCollector->findClass($name);
}

/**
* @param PropertyFetch|StaticPropertyFetch $expr
* @param class-string $name
*/
public function findPropertyByPropertyFetch(Expr $expr): ?Property
public function findClass(string $name): ?Class_
{
$propertyCaller = $expr instanceof StaticPropertyFetch ? $expr->class : $expr->var;

$propertyCallerType = $this->nodeTypeResolver->getStaticType($propertyCaller);
if (! $propertyCallerType instanceof TypeWithClassName) {
return null;
}

$className = $this->nodeTypeResolver->getFullyQualifiedClassName($propertyCallerType);
$class = $this->findClass($className);
if (! $class instanceof Class_) {
return null;
}

$propertyName = $this->nodeNameResolver->getName($expr->name);
if ($propertyName === null) {
return null;
}

return $class->getProperty($propertyName);
return $this->parsedNodeCollector->findClass($name);
}

public function findTrait(string $name): ?Trait_
Expand Down Expand Up @@ -171,6 +132,7 @@ private function isChildOrEqualClassLike(string $desiredClass, ?string $currentC
if (! $currentClassReflection->isSubclassOf($desiredClassReflection->getName())) {
return false;
}

return $currentClassName !== $desiredClass;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(FuncCallToMethodCallRector::class)
->call('configure', [[
FuncCallToMethodCallRector::FUNC_CALL_TO_CLASS_METHOD_CALL => ValueObjectInliner::inline([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function matchTypeProvidingExpr(
$functionLike,
$objectType
);

if ($expr !== null) {
if ($expr instanceof Variable) {
$this->addClassMethodParamForVariable($expr, $objectType, $functionLike);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function refactor(Node $node): ?Node
$classMethod,
$funcNameToMethodCallName->getNewObjectType()
);

return $this->nodeFactory->createMethodCall(
$expr,
$funcNameToMethodCallName->getNewMethodName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,19 @@ private function resolveReturnPropertyType(ClassMethod $classMethod): array
return [];
}

$propertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch(
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch(
$return->expr
);
if (! $propertyReflection instanceof PhpPropertyReflection) {
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return [];
}

if ($propertyReflection->getNativeType() instanceof MixedType) {
// all property must have type declaration
if ($phpPropertyReflection->getNativeType() instanceof MixedType) {
return [];
}

$propertyTypes[] = $propertyReflection->getNativeType();
$propertyTypes[] = $phpPropertyReflection->getNativeType();
}

return $propertyTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\TypeDeclaration\Reflection\ReflectionTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -28,7 +28,7 @@
final class ParamTypeFromStrictTypedPropertyRector extends AbstractRector
{
public function __construct(
private ReflectionTypeResolver $reflectionTypeResolver
private ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -127,39 +127,30 @@ public function decorateParamWithType(FunctionLike $functionLike, Param $param):
}

/**
* @return Node\Identifier|Node\Name|UnionType|NullableType|null
* @return Node\Name|UnionType|NullableType|null
*/
private function matchPropertySingleTypeNode(PropertyFetch $propertyFetch): ?Node
{
$property = $this->nodeRepository->findPropertyByPropertyFetch($propertyFetch);
if (! $property instanceof Property) {
// code from /vendor
$propertyFetchType = $this->reflectionTypeResolver->resolvePropertyFetchType($propertyFetch);
if (! $propertyFetchType instanceof Type) {
return null;
}

return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyFetchType);
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($propertyFetch);
if (! $phpPropertyReflection instanceof PhpPropertyReflection) {
return null;
}

if ($property->type === null) {
$propertyType = $phpPropertyReflection->getNativeType();

if ($propertyType instanceof MixedType) {
return null;
}

// move type to param if not union type
if ($property->type instanceof UnionType) {
if ($propertyType instanceof \PHPStan\Type\UnionType) {
return null;
}

if ($property->type instanceof NullableType) {
if ($propertyType instanceof NullableType) {
return null;
}

// needed to avoid reprinting original tokens bug
$typeNode = clone $property->type;
$typeNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $typeNode;
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType);
}

private function hasTypeChangedBeforeAssign(Assign $assign, string $paramName, Type $originalType): bool
Expand Down
49 changes: 0 additions & 49 deletions rules/TypeDeclaration/Reflection/ReflectionTypeResolver.php

This file was deleted.

5 changes: 4 additions & 1 deletion rules/TypeDeclaration/TypeInferer/SilentVoidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -48,7 +49,9 @@ public function hasExclusiveVoid(ClassMethod | Closure | Function_ $functionLike
return false;
}

if ($classLike instanceof Stmt\Class_ && $this->externalFullyQualifiedAnalyzer->hasExternalFullyQualifieds($classLike)) {
if ($classLike instanceof Class_ && $this->externalFullyQualifiedAnalyzer->hasExternalFullyQualifieds(
$classLike
)) {
return false;
}

Expand Down

0 comments on commit d8a7f6c

Please sign in to comment.