Skip to content

Commit 8ab5e97

Browse files
authored
[Performance] Call cached class names collection on FamilyRelationsAnalyzer (#5879)
* [Performance] Call cached class names collection on FamilyRelationsAnalyzer * cs fix * Fix * clean up * clean up
1 parent 50407e2 commit 8ab5e97

File tree

6 files changed

+49
-48
lines changed

6 files changed

+49
-48
lines changed

rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Rector\DeadCode\PhpDoc\TagRemover;
66

7-
use PHPStan\Type\Type;
87
use PhpParser\Node\FunctionLike;
98
use PHPStan\PhpDocParser\Ast\Node;
109
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
1110
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
11+
use PHPStan\Type\Type;
1212
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1313
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1414
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
@@ -22,8 +22,11 @@ public function __construct(
2222
) {
2323
}
2424

25-
public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $functionLike, ?Type $type = null): bool
26-
{
25+
public function removeParamTagsIfUseless(
26+
PhpDocInfo $phpDocInfo,
27+
FunctionLike $functionLike,
28+
?Type $type = null
29+
): bool {
2730
$hasChanged = false;
2831

2932
$phpDocNodeTraverser = new PhpDocNodeTraverser();

src/DependencyInjection/LazyContainerFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ static function (Container $container): DynamicSourceLocatorProvider {
471471
DynamicSourceLocatorProvider::class,
472472
static function (DynamicSourceLocatorProvider $dynamicSourceLocatorProvider, Container $container): void {
473473
$dynamicSourceLocatorProvider->autowire(
474-
$container->make(ReflectionProvider::class),
475474
$container->make(Cache::class),
476475
$container->make(FileHasher::class)
477476
);

src/FamilyTree/Reflection/FamilyRelationsAnalyzer.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
use PhpParser\Node\Name;
88
use PhpParser\Node\Stmt\Class_;
99
use PhpParser\Node\Stmt\Interface_;
10+
use PHPStan\Broker\ClassNotFoundException;
1011
use PHPStan\Reflection\ClassReflection;
1112
use PHPStan\Reflection\ReflectionProvider;
13+
use Rector\Caching\Cache;
14+
use Rector\Caching\Enum\CacheKey;
1215
use Rector\NodeNameResolver\NodeNameResolver;
16+
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
1317
use Rector\Util\Reflection\PrivatesAccessor;
1418

15-
final readonly class FamilyRelationsAnalyzer
19+
final class FamilyRelationsAnalyzer
1620
{
1721
public function __construct(
18-
private ReflectionProvider $reflectionProvider,
19-
private NodeNameResolver $nodeNameResolver,
20-
private PrivatesAccessor $privatesAccessor
22+
private readonly ReflectionProvider $reflectionProvider,
23+
private readonly NodeNameResolver $nodeNameResolver,
24+
private readonly PrivatesAccessor $privatesAccessor,
25+
private readonly DynamicSourceLocatorProvider $dynamicSourceLocatorProvider,
26+
private readonly Cache $cache,
27+
private bool $hasClassNamesCachedOrLoadOneLocator = false
2128
) {
2229
}
2330

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

40+
$this->loadClasses();
41+
3342
/** @var ClassReflection[] $classReflections */
3443
$classReflections = $this->privatesAccessor->getPrivateProperty($this->reflectionProvider, 'classes');
3544
$childrenClassReflections = [];
@@ -90,4 +99,28 @@ public function getClassLikeAncestorNames(Class_ | Interface_ | Name $classOrNam
9099
/** @var string[] $ancestorNames */
91100
return $ancestorNames;
92101
}
102+
103+
private function loadClasses(): void
104+
{
105+
if ($this->hasClassNamesCachedOrLoadOneLocator) {
106+
return;
107+
}
108+
109+
$key = CacheKey::CLASSNAMES_HASH_KEY . '_' . $this->dynamicSourceLocatorProvider->getCacheClassNameKey();
110+
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
111+
112+
if (is_string($classNamesCache)) {
113+
$classNamesCache = json_decode($classNamesCache);
114+
if (is_array($classNamesCache)) {
115+
foreach ($classNamesCache as $classNameCache) {
116+
try {
117+
$this->reflectionProvider->getClass($classNameCache);
118+
} catch (ClassNotFoundException) {
119+
}
120+
}
121+
}
122+
}
123+
124+
$this->hasClassNamesCachedOrLoadOneLocator = true;
125+
}
93126
}

src/NodeTypeResolver/Reflection/BetterReflection/SourceLocatorProvider/DynamicSourceLocatorProvider.php

+3-38
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
use PHPStan\BetterReflection\Reflector\DefaultReflector;
88
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
99
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
10-
use PHPStan\Broker\ClassNotFoundException;
1110
use PHPStan\File\CouldNotReadFileException;
1211
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
1312
use PHPStan\Reflection\BetterReflection\SourceLocator\NewOptimizedDirectorySourceLocator;
1413
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory;
1514
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator;
16-
use PHPStan\Reflection\ReflectionProvider;
1715
use Rector\Caching\Cache;
1816
use Rector\Caching\Enum\CacheKey;
1917
use Rector\Contract\DependencyInjection\ResetableInterface;
@@ -37,8 +35,6 @@ final class DynamicSourceLocatorProvider implements ResetableInterface
3735

3836
private ?AggregateSourceLocator $aggregateSourceLocator = null;
3937

40-
private ReflectionProvider $reflectionProvider;
41-
4238
private Cache $cache;
4339

4440
private FileHasher $fileHasher;
@@ -49,13 +45,8 @@ public function __construct(
4945
) {
5046
}
5147

52-
public function autowire(
53-
ReflectionProvider $reflectionProvider,
54-
Cache $cache,
55-
FileHasher $fileHasher
56-
): void
48+
public function autowire(Cache $cache, FileHasher $fileHasher): void
5749
{
58-
$this->reflectionProvider = $reflectionProvider;
5950
$this->cache = $cache;
6051
$this->fileHasher = $fileHasher;
6152
}
@@ -138,19 +129,6 @@ public function reset(): void
138129
$this->aggregateSourceLocator = null;
139130
}
140131

141-
/**
142-
* @param class-string[] $classNamesCache
143-
*/
144-
private function locateCachedClassNames(array $classNamesCache): void
145-
{
146-
foreach ($classNamesCache as $classNameCache) {
147-
try {
148-
$this->reflectionProvider->getClass($classNameCache);
149-
} catch (ClassNotFoundException) {
150-
}
151-
}
152-
}
153-
154132
/**
155133
* @param OptimizedSingleFileSourceLocator[]|NewOptimizedDirectorySourceLocator[] $sourceLocators
156134
*/
@@ -169,11 +147,7 @@ private function collectClasses(AggregateSourceLocator $aggregateSourceLocator,
169147
$classNamesCache = $this->cache->load($key, CacheKey::CLASSNAMES_HASH_KEY);
170148

171149
if (is_string($classNamesCache)) {
172-
$classNamesCache = json_decode($classNamesCache);
173-
if (is_array($classNamesCache)) {
174-
$this->locateCachedClassNames($classNamesCache);
175-
return;
176-
}
150+
return;
177151
}
178152

179153
$reflector = new DefaultReflector($aggregateSourceLocator);
@@ -183,16 +157,7 @@ private function collectClasses(AggregateSourceLocator $aggregateSourceLocator,
183157
try {
184158
$reflections = $reflector->reflectAllClasses();
185159
foreach ($reflections as $reflection) {
186-
$className = $reflection->getName();
187-
188-
// make 'classes' collection
189-
try {
190-
$this->reflectionProvider->getClass($className);
191-
} catch (ClassNotFoundException) {
192-
continue;
193-
}
194-
195-
$classNames[] = $className;
160+
$classNames[] = $reflection->getName();
196161
}
197162
} catch (CouldNotReadFileException) {
198163
}

src/NodeTypeResolver/TypeAnalyzer/ArrayTypeAnalyzer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function __construct(
1717
public function isArrayType(Expr $expr): bool
1818
{
1919
$nodeType = $this->nodeTypeResolver->getNativeType($expr);
20-
return $nodeType->isArray()->yes();
20+
return $nodeType->isArray()
21+
->yes();
2122
}
2223
}

src/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private function hasChildrenDifferentTypeClassMethod(
8888
): bool {
8989
$methodName = $classMethod->name->toString();
9090
foreach ($childrenClassReflections as $childClassReflection) {
91-
if (!$childClassReflection->hasNativeMethod($methodName)) {
91+
if (! $childClassReflection->hasNativeMethod($methodName)) {
9292
continue;
9393
}
9494

0 commit comments

Comments
 (0)