Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance] Call cached class names collection on FamilyRelationsAnalyzer #5879

Merged
merged 5 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Rector\DeadCode\PhpDoc\TagRemover;

use PHPStan\Type\Type;
use PhpParser\Node\FunctionLike;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
Expand All @@ -22,8 +22,11 @@ public function __construct(
) {
}

public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $functionLike, ?Type $type = null): bool
{
public function removeParamTagsIfUseless(
PhpDocInfo $phpDocInfo,
FunctionLike $functionLike,
?Type $type = null
): bool {
$hasChanged = false;

$phpDocNodeTraverser = new PhpDocNodeTraverser();
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ static function (Container $container): DynamicSourceLocatorProvider {
DynamicSourceLocatorProvider::class,
static function (DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Container $container): void {
$dynamicSourceLocatorProvider->autowire(
$container->make(ReflectionProvider::class),
$container->make(Cache::class),
$container->make(FileHasher::class)
);
Expand Down
41 changes: 37 additions & 4 deletions src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Caching\Cache;
use Rector\Caching\Enum\CacheKey;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\Util\Reflection\PrivatesAccessor;

final readonly class FamilyRelationsAnalyzer
final class FamilyRelationsAnalyzer
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private NodeNameResolver $nodeNameResolver,
private PrivatesAccessor $privatesAccessor
private readonly ReflectionProvider $reflectionProvider,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PrivatesAccessor $privatesAccessor,
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
private readonly Cache $cache,
private bool $hasClassNamesCachedOrLoadOneLocator = false
) {
}

Expand All @@ -30,6 +37,8 @@ public function getChildrenOfClassReflection(ClassReflection $desiredClassReflec
return [];
}

$this->loadClasses();

/** @var ClassReflection[] $classReflections */
$classReflections = $this->privatesAccessor->getPrivateProperty($this->reflectionProvider, 'classes');
$childrenClassReflections = [];
Expand Down Expand Up @@ -90,4 +99,28 @@ public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrNam
/** @var string[] $ancestorNames */
return $ancestorNames;
}

private function loadClasses(): void
{
if ($this->hasClassNamesCachedOrLoadOneLocator) {
return;
}

$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->dynamicSourceLocatorProvider->getCacheClassNameKey();
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);

if (is_string($classNamesCache)) {
$classNamesCache = json_decode($classNamesCache);
if (is_array($classNamesCache)) {
foreach ($classNamesCache as $classNameCache) {
try {
$this->reflectionProvider->getClass($classNameCache);
} catch (ClassNotFoundException) {
}
}
}
}

$this->hasClassNamesCachedOrLoadOneLocator = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\File\CouldNotReadFileException;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\NewOptimizedDirectorySourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Caching\Cache;
use Rector\Caching\Enum\CacheKey;
use Rector\Contract\DependencyInjection\ResetableInterface;
Expand All @@ -37,8 +35,6 @@ final class DynamicSourceLocatorProvider implements ResetableInterface

private ?AggregateSourceLocator $aggregateSourceLocator = null;

private ReflectionProvider $reflectionProvider;

private Cache $cache;

private FileHasher $fileHasher;
Expand All @@ -49,13 +45,8 @@ public function __construct(
) {
}

public function autowire(
ReflectionProvider $reflectionProvider,
Cache $cache,
FileHasher $fileHasher
): void
public function autowire(Cache $cache, FileHasher $fileHasher): void
{
$this->reflectionProvider = $reflectionProvider;
$this->cache = $cache;
$this->fileHasher = $fileHasher;
}
Expand Down Expand Up @@ -138,19 +129,6 @@ public function reset(): void
$this->aggregateSourceLocator = null;
}

/**
* @param class-string[] $classNamesCache
*/
private function locateCachedClassNames(array $classNamesCache): void
{
foreach ($classNamesCache as $classNameCache) {
try {
$this->reflectionProvider->getClass($classNameCache);
} catch (ClassNotFoundException) {
}
}
}

/**
* @param OptimizedSingleFileSourceLocator[]|NewOptimizedDirectorySourceLocator[] $sourceLocators
*/
Expand All @@ -169,11 +147,7 @@ private function collectClasses(AggregateSourceLocator $aggregateSourceLocator,
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);

if (is_string($classNamesCache)) {
$classNamesCache = json_decode($classNamesCache);
if (is_array($classNamesCache)) {
$this->locateCachedClassNames($classNamesCache);
return;
}
return;
}

$reflector = new DefaultReflector($aggregateSourceLocator);
Expand All @@ -183,16 +157,7 @@ private function collectClasses(AggregateSourceLocator $aggregateSourceLocator,
try {
$reflections = $reflector->reflectAllClasses();
foreach ($reflections as $reflection) {
$className = $reflection->getName();

// make 'classes' collection
try {
$this->reflectionProvider->getClass($className);
} catch (ClassNotFoundException) {
continue;
}

$classNames[] = $className;
$classNames[] = $reflection->getName();
}
} catch (CouldNotReadFileException) {
}
Expand Down
3 changes: 2 additions & 1 deletion src/NodeTypeResolver/TypeAnalyzer/ArrayTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
public function isArrayType(Expr $expr): bool
{
$nodeType = $this->nodeTypeResolver->getNativeType($expr);
return $nodeType->isArray()->yes();
return $nodeType->isArray()
->yes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function hasChildrenDifferentTypeClassMethod(
): bool {
$methodName = $classMethod->name->toString();
foreach ($childrenClassReflections as $childClassReflection) {
if (!$childClassReflection->hasNativeMethod($methodName)) {
if (! $childClassReflection->hasNativeMethod($methodName)) {
continue;
}

Expand Down