Skip to content

Commit 9697351

Browse files
mvhirschfabpot
authored andcommitted
[PropertyInfo] Adds static cache to PhpStanExtractor
1 parent 5a00a8b commit 9697351

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPStan\PhpDocParser\Parser\PhpDocParser;
2222
use PHPStan\PhpDocParser\Parser\TokenIterator;
2323
use PHPStan\PhpDocParser\Parser\TypeParser;
24+
use Symfony\Component\PropertyInfo\PhpStan\NameScope;
2425
use Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
2526
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2627
use Symfony\Component\PropertyInfo\Type as LegacyType;
@@ -55,6 +56,9 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
5556
private array $accessorPrefixes;
5657
private array $arrayMutatorPrefixes;
5758

59+
/** @var array<string, NameScope> */
60+
private array $contexts = [];
61+
5862
/**
5963
* @param list<string>|null $mutatorPrefixes
6064
* @param list<string>|null $accessorPrefixes
@@ -86,7 +90,6 @@ public function getTypes(string $class, string $property, array $context = []):
8690
{
8791
/** @var PhpDocNode|null $docNode */
8892
[$docNode, $source, $prefix, $declaringClass] = $this->getDocBlock($class, $property);
89-
$nameScope = $this->nameScopeFactory->create($class, $declaringClass);
9093
if (null === $docNode) {
9194
return null;
9295
}
@@ -120,6 +123,7 @@ public function getTypes(string $class, string $property, array $context = []):
120123
continue;
121124
}
122125

126+
$nameScope ??= $this->contexts[$class.'/'.$declaringClass] ??= $this->nameScopeFactory->create($class, $declaringClass);
123127
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $nameScope) as $type) {
124128
switch ($type->getClassName()) {
125129
case 'self':

src/Symfony/Component/PropertyInfo/PhpStan/NameScopeFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\PropertyInfo\PhpStan;
1313

14+
use phpDocumentor\Reflection\Types\Context;
1415
use phpDocumentor\Reflection\Types\ContextFactory;
1516

1617
/**
@@ -20,6 +21,9 @@
2021
*/
2122
final class NameScopeFactory
2223
{
24+
/** @var array<string, Context> */
25+
private array $contexts = [];
26+
2327
public function create(string $calledClassName, ?string $declaringClassName = null): NameScope
2428
{
2529
$declaringClassName ??= $calledClassName;
@@ -60,7 +64,7 @@ private function extractFromFullClassName(\ReflectionClass $reflection): array
6064
}
6165

6266
$factory = new ContextFactory();
63-
$context = $factory->createForNamespace($namespace, $contents);
67+
$context = $this->contexts[$namespace.$fileName] ??= $factory->createForNamespace($namespace, $contents);
6468

6569
return [$namespace, $context->getNamespaceAliases()];
6670
}

0 commit comments

Comments
 (0)