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 b8dc69b commit b887783
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 148 deletions.
90 changes: 45 additions & 45 deletions packages/NodeCollector/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,51 +360,51 @@ 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 [];
}
// /**
// * @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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,49 +46,49 @@ public function __construct(
$this->nodeTypeResolver = $nodeTypeResolver;
}

public function collect(Node $node): void
{
if (! $node instanceof ClassConstFetch) {
return;
}

$constantName = $this->nodeNameResolver->getName($node->name);
if ($constantName === 'class') {
// this is not a manual constant
return;
}
if ($constantName === null) {
// this is not a manual constant
return;
}

$resolvedClassType = $this->nodeTypeResolver->resolve($node->class);

$className = $this->resolveClassTypeThatContainsConstantOrFirstUnioned($resolvedClassType, $constantName);
if ($className === null) {
return;
}

// current class
$classOfUse = $node->getAttribute(AttributeKey::CLASS_NAME);
if ($classOfUse === null) {
return;
}

$this->classConstantFetchByClassAndName[$className][$constantName][] = $classOfUse;

$this->classConstantFetchByClassAndName[$className][$constantName] = array_unique(
$this->classConstantFetchByClassAndName[$className][$constantName]
);
}

/**
* @return array<string, array<string, class-string[]>>
*/
public function getClassConstantFetchByClassAndName(): array
{
return $this->classConstantFetchByClassAndName;
}
// public function collect(Node $node): void
// {
// if (! $node instanceof ClassConstFetch) {
// return;
// }
//
// $constantName = $this->nodeNameResolver->getName($node->name);
// if ($constantName === 'class') {
// // this is not a manual constant
// return;
// }
// if ($constantName === null) {
// // this is not a manual constant
// return;
// }
//
// $resolvedClassType = $this->nodeTypeResolver->resolve($node->class);
//
// $className = $this->resolveClassTypeThatContainsConstantOrFirstUnioned($resolvedClassType, $constantName);
// if ($className === null) {
// return;
// }
//
// // current class
// $classOfUse = $node->getAttribute(AttributeKey::CLASS_NAME);
// if ($classOfUse === null) {
// return;
// }
//
// $this->classConstantFetchByClassAndName[$className][$constantName][] = $classOfUse;
//
// $this->classConstantFetchByClassAndName[$className][$constantName] = array_unique(
// $this->classConstantFetchByClassAndName[$className][$constantName]
// );
// }
////
//// /**
// * @return array<string, array<string, class-string[]>>
// */
// public function getClassConstantFetchByClassAndName(): array
// {
// return $this->classConstantFetchByClassAndName;
// }

private function resolveClassTypeThatContainsConstantOrFirstUnioned(
Type $resolvedClassType,
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 b887783

Please sign in to comment.