Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Mar 23, 2021
1 parent dc1c2c7 commit 6e798e8
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 282 deletions.
53 changes: 0 additions & 53 deletions packages/NodeCollector/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ final class NodeRepository
*/
private $parsedPropertyFetchNodeCollector;

/**
* @var ParsedClassConstFetchNodeCollector
*/
private $parsedClassConstFetchNodeCollector;

/**
* @var ParsedNodeCollector
*/
Expand Down Expand Up @@ -132,7 +127,6 @@ public function __construct(
ArrayCallableMethodReferenceAnalyzer $arrayCallableMethodReferenceAnalyzer,
ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector,
NodeNameResolver $nodeNameResolver,
ParsedClassConstFetchNodeCollector $parsedClassConstFetchNodeCollector,
ParsedNodeCollector $parsedNodeCollector,
TypeUnwrapper $typeUnwrapper,
ReflectionProvider $reflectionProvider,
Expand All @@ -141,7 +135,6 @@ public function __construct(
$this->nodeNameResolver = $nodeNameResolver;
$this->arrayCallableMethodReferenceAnalyzer = $arrayCallableMethodReferenceAnalyzer;
$this->parsedPropertyFetchNodeCollector = $parsedPropertyFetchNodeCollector;
$this->parsedClassConstFetchNodeCollector = $parsedClassConstFetchNodeCollector;
$this->parsedNodeCollector = $parsedNodeCollector;
$this->typeUnwrapper = $typeUnwrapper;
$this->reflectionProvider = $reflectionProvider;
Expand Down Expand Up @@ -360,52 +353,6 @@ public function findCallsByClassMethod(ClassMethod $classMethod): array
return $this->findCallsByClassAndMethod($class, $method);
}

/**
* @return ClassReflection[]
*/
public function findDirectClassConstantFetches(ClassReflection $classReflection, string $desiredConstantName): array
{
$classConstantFetchByClassAndName = $this->parsedClassConstFetchNodeCollector->getClassConstantFetchByClassAndName();
$classTypes = $classConstantFetchByClassAndName[$classReflection->getName()][$desiredConstantName] ?? [];

return $this->resolveClassReflectionsFromClassTypes($classTypes);
}

/**
* @return ClassReflection[]
*/
public function findIndirectClassConstantFetches(
ClassReflection $classReflection,
string $desiredConstantName
): array {
$classConstantFetchByClassAndName = $this->parsedClassConstFetchNodeCollector->getClassConstantFetchByClassAndName();

foreach ($classConstantFetchByClassAndName as $className => $classesByConstantName) {
if (! $this->reflectionProvider->hasClass($className)) {
return [];
}

$currentClassReflection = $this->reflectionProvider->getClass($className);
if (! isset($classesByConstantName[$desiredConstantName])) {
continue;
}

if (! $classReflection->isSubclassOf($currentClassReflection->getName()) &&
! $currentClassReflection->isSubclassOf($classReflection->getName())) {
continue;
}

// include child usages and parent usages
if ($currentClassReflection->getName() === $classReflection->getName()) {
continue;
}

return $this->resolveClassReflectionsFromClassTypes($classesByConstantName[$desiredConstantName]);
}

return [];
}

public function hasClassChildren(Class_ $desiredClass): bool
{
$desiredClassName = $desiredClass->getAttribute(AttributeKey::CLASS_NAME);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeCollector\NodeCollector\NodeRepository;
use Rector\NodeCollector\NodeCollector\ParsedClassConstFetchNodeCollector;
use Rector\NodeCollector\NodeCollector\ParsedNodeCollector;
use Rector\NodeCollector\NodeCollector\ParsedPropertyFetchNodeCollector;

Expand All @@ -28,21 +27,14 @@ final class NodeCollectorNodeVisitor extends NodeVisitorAbstract
*/
private $parsedPropertyFetchNodeCollector;

/**
* @var ParsedClassConstFetchNodeCollector
*/
private $parsedClassConstFetchNodeCollector;

public function __construct(
ParsedClassConstFetchNodeCollector $parsedClassConstFetchNodeCollector,
NodeRepository $nodeRepository,
ParsedNodeCollector $parsedNodeCollector,
ParsedPropertyFetchNodeCollector $parsedPropertyFetchNodeCollector
) {
$this->nodeRepository = $nodeRepository;
$this->parsedNodeCollector = $parsedNodeCollector;
$this->parsedPropertyFetchNodeCollector = $parsedPropertyFetchNodeCollector;
$this->parsedClassConstFetchNodeCollector = $parsedClassConstFetchNodeCollector;
}

public function enterNode(Node $node)
Expand All @@ -53,7 +45,6 @@ public function enterNode(Node $node)

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

return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector\Fixture;

final class SkipUsedConstant
{
private const SOME_CONST = 'dead';

public function run()
{
return self::SOME_CONST;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\ApiPhpDocTagNode;
use Rector\Core\NodeManipulator\ClassConstManipulator;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -91,13 +92,23 @@ public function refactor(Node $node): ?Node
return null;
}

$classObjectType = new ObjectType($classReflection->getName());

/** @var ClassConstFetch[] $classConstFetches */
$classConstFetches = $this->betterNodeFinder->findInstanceOf($classLike->stmts, ClassConstFetch::class);
foreach ($classConstFetches as $classConstFetch) {
dump($classConstFetch);
dump($node);
if (! $this->nodeNameResolver->areNamesEqual($classConstFetch->name, $node->consts[0]->name)) {
continue;
}

$constFetchClassType = $this->nodeTypeResolver->resolve($classConstFetch->class);

// constant is used!
if ($constFetchClassType->isSuperTypeOf($classObjectType)->yes()) {
return null;
}
}

//
// $directUsingClassReflections = $this->nodeRepository->findDirectClassConstantFetches(
// $classReflection,
// $constant
Expand Down

0 comments on commit 6e798e8

Please sign in to comment.