Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[PropertyInfo] Adds static cache to PhpStanExtractor
  • Loading branch information
mvhirsch authored and fabpot committed May 31, 2024
commit 9697351b3db9378ff67a5889ce799e6c33d147e0
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