Skip to content

[PropertyInfo] Adds static cache to PhpStanExtractor #54894

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

Merged
merged 1 commit into from
May 31, 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use Symfony\Component\PropertyInfo\PhpStan\NameScope;
use Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type as LegacyType;
Expand Down Expand Up @@ -55,6 +56,9 @@ final class PhpStanExtractor implements PropertyTypeExtractorInterface, Construc
private array $accessorPrefixes;
private array $arrayMutatorPrefixes;

/** @var array<string, NameScope> */
private array $contexts = [];

/**
* @param list<string>|null $mutatorPrefixes
* @param list<string>|null $accessorPrefixes
Expand Down Expand Up @@ -86,7 +90,6 @@ public function getTypes(string $class, string $property, array $context = []):
{
/** @var PhpDocNode|null $docNode */
[$docNode, $source, $prefix, $declaringClass] = $this->getDocBlock($class, $property);
$nameScope = $this->nameScopeFactory->create($class, $declaringClass);
if (null === $docNode) {
return null;
}
Expand Down Expand Up @@ -120,6 +123,7 @@ public function getTypes(string $class, string $property, array $context = []):
continue;
}

$nameScope ??= $this->contexts[$class.'/'.$declaringClass] ??= $this->nameScopeFactory->create($class, $declaringClass);
foreach ($this->phpStanTypeHelper->getTypes($tagDocNode->value, $nameScope) as $type) {
switch ($type->getClassName()) {
case 'self':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\PropertyInfo\PhpStan;

use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\ContextFactory;

/**
Expand All @@ -20,6 +21,9 @@
*/
final class NameScopeFactory
{
/** @var array<string, Context> */
private array $contexts = [];

public function create(string $calledClassName, ?string $declaringClassName = null): NameScope
{
$declaringClassName ??= $calledClassName;
Expand Down Expand Up @@ -60,7 +64,7 @@ private function extractFromFullClassName(\ReflectionClass $reflection): array
}

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

return [$namespace, $context->getNamespaceAliases()];
}
Expand Down
Loading