Skip to content

Commit

Permalink
NodeRepository usage cleanup (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 24, 2021
1 parent 328b5b4 commit 4f619aa
Show file tree
Hide file tree
Showing 21 changed files with 81 additions and 425 deletions.
68 changes: 0 additions & 68 deletions packages/NodeCollector/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,26 @@

namespace Rector\NodeCollector\NodeCollector;

use PhpParser\Node;
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\ClassMethod;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use ReflectionMethod;

/**
* This service contains all the parsed nodes. E.g. all the functions, method call, classes, static calls etc. It's
* useful in case of context analysis, e.g. find all the usage of class method to detect, if the method is used.
*/
final class NodeRepository
{
/**
* @var array<class-string, ClassMethod[]>
*/
private array $classMethodsByType = [];

public function __construct(
private ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector,
private NodeNameResolver $nodeNameResolver,
Expand All @@ -43,51 +33,6 @@ public function __construct(
) {
}

public function collect(Node $node): void
{
if ($node instanceof ClassMethod) {
$this->addMethod($node);
}
}

public function findClassMethod(string $className, string $methodName): ?ClassMethod
{
if (\str_contains($methodName, '\\')) {
$message = sprintf('Class and method arguments are switched in "%s"', __METHOD__);
throw new ShouldNotHappenException($message);
}

if (isset($this->classMethodsByType[$className][$methodName])) {
return $this->classMethodsByType[$className][$methodName];
}

if (! $this->reflectionProvider->hasClass($className)) {
return null;
}

$classReflection = $this->reflectionProvider->getClass($className);
foreach ($classReflection->getParents() as $parentClassReflection) {
if (isset($this->classMethodsByType[$parentClassReflection->getName()][$methodName])) {
return $this->classMethodsByType[$parentClassReflection->getName()][$methodName];
}
}

return null;
}

/**
* @param MethodReflection|ReflectionMethod $methodReflection
*/
public function findClassMethodByMethodReflection(object $methodReflection): ?ClassMethod
{
$methodName = $methodReflection->getName();

$declaringClass = $methodReflection->getDeclaringClass();
$className = $declaringClass->getName();

return $this->findClassMethod($className, $methodName);
}

/**
* @return PropertyFetch[]
*/
Expand Down Expand Up @@ -239,19 +184,6 @@ public function findClassLike(string $classLikeName): ?ClassLike
);
}

private function addMethod(ClassMethod $classMethod): void
{
$className = $classMethod->getAttribute(AttributeKey::CLASS_NAME);

// anonymous
if ($className === null) {
return;
}

$methodName = $this->nodeNameResolver->getName($classMethod);
$this->classMethodsByType[$className][$methodName] = $classMethod;
}

private function isChildOrEqualClassLike(string $desiredClass, ?string $currentClassName): bool
{
if ($currentClassName === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function enterNode(Node $node)
$this->parsedNodeCollector->collect($node);
}

$this->nodeRepository->collect($node);
$this->parsedPropertyFetchNodeCollector->collect($node);

return null;
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ parameters:
checkGenericClassInNonGenericObjectType: false

excludes_analyse:
# temporary stinrgable migration from template type provider
- src/Console/Command/InitCommand.php

- */config.php
- tests/debug_functions.php

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,14 @@

namespace Rector\Tests\CodeQuality\Rector\Array_\ArrayThisCallToThisMethodCallRector\Fixture;

class Fixture
{
public function run()
{
$values = [$this, 'giveMeMore'];
}

public function giveMeMore()
{
return 'more';
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Array_\ArrayThisCallToThisMethodCallRector\Fixture;

class Fixture
{
public function run()
{
$values = $this->giveMeMore();
}

private function giveMeMore()
public function giveMeMore()
{
return 'more';
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source\vendor\SomeInterfaceWithReturnType;

final class SkipParentProtected implements SomeInterfaceWithReturnType
{
Expand All @@ -9,8 +12,3 @@ final class SkipParentProtected implements SomeInterfaceWithReturnType
throw new ShouldNotHappenException();
}
}

interface SomeInterfaceWithReturnType
{
public function run(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Source\vendor;

interface SomeInterfaceWithReturnType
{
public function run(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodReferenceAnalyzer;
use Rector\NodeCollector\ValueObject\ArrayCallable;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionMethod;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -117,8 +115,6 @@ public function refactor(Node $node): ?Node
$nativeReflectionClass = $classReflection->getNativeReflection();

$nativeReflectionMethod = $nativeReflectionClass->getMethod($arrayCallable->getMethod());
$this->privatizeClassMethod($nativeReflectionMethod);

if ($nativeReflectionMethod->getNumberOfParameters() === 0) {
return new MethodCall(new Variable('this'), $arrayCallable->getMethod());
}
Expand Down Expand Up @@ -153,18 +149,4 @@ private function isInsideProperty(Array_ $array): bool

return $parentProperty !== null;
}

private function privatizeClassMethod(ReflectionMethod $reflectionMethod): void
{
$classMethod = $this->nodeRepository->findClassMethodByMethodReflection($reflectionMethod);
if (! $classMethod instanceof ClassMethod) {
return;
}

if ($classMethod->isPrivate()) {
return;
}

$this->visibilityManipulator->makePrivate($classMethod);
}
}

0 comments on commit 4f619aa

Please sign in to comment.