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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public function testCollectClassNames()
$this->assertSame('Dummy', $typeContext->declaringClassName);
}

public function testCacheResultWhenToStringTypeResolver()
{
$typeContext = $this->typeContextFactory->createFromClassName(Dummy::class, AbstractDummy::class);
$cachedtypeContext = $this->typeContextFactory->createFromClassName(Dummy::class, AbstractDummy::class);
$this->assertSame($typeContext, $cachedtypeContext);
}

public function testCollectNamespace()
{
$namespace = 'Symfony\\Component\\TypeInfo\\Tests\\Fixtures';
Expand Down
69 changes: 43 additions & 26 deletions src/Symfony/Component/TypeInfo/TypeContext/TypeContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
* @author Pierre-Yves Landuré <landure@gmail.com>
*/
final class TypeContextFactory
{
Expand All @@ -41,6 +42,16 @@ final class TypeContextFactory
*/
private static array $reflectionClassCache = [];

/**
* @var array<string,array<string,TypeContext>
*/
private array $intermediateTypeContextCache = [];

/**
* @var array<string,array<string,TypeContext>
*/
private array $typeContextCache = [];

private ?Lexer $phpstanLexer = null;
private ?PhpDocParser $phpstanParser = null;

Expand All @@ -57,26 +68,7 @@ public function createFromClassName(string $calledClassName, ?string $declaringC
{
$declaringClassName ??= $calledClassName;

$calledClassPath = explode('\\', $calledClassName);
$declaringClassPath = explode('\\', $declaringClassName);

$declaringClassReflection = self::$reflectionClassCache[$declaringClassName] ??= new \ReflectionClass($declaringClassName);

$typeContext = new TypeContext(
end($calledClassPath),
end($declaringClassPath),
trim($declaringClassReflection->getNamespaceName(), '\\'),
$this->collectUses($declaringClassReflection),
);

return new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$this->collectTemplates($declaringClassReflection, $typeContext),
$this->collectTypeAliases($declaringClassReflection, $typeContext),
);
return $this->typeContextCache[$declaringClassName][$calledClassName] ??= $this->createNewInstanceFromClassName($calledClassName, $declaringClassName);
}

public function createFromReflection(\Reflector $reflection): ?TypeContext
Expand All @@ -94,12 +86,7 @@ public function createFromReflection(\Reflector $reflection): ?TypeContext
return null;
}

$typeContext = new TypeContext(
$declaringClassReflection->getShortName(),
$declaringClassReflection->getShortName(),
$declaringClassReflection->getNamespaceName(),
$this->collectUses($declaringClassReflection),
);
$typeContext = $this->createIntermediateTypeContext($declaringClassReflection->getShortName(), $declaringClassReflection);

$templates = match (true) {
$reflection instanceof \ReflectionFunctionAbstract => $this->collectTemplates($reflection, $typeContext) + $this->collectTemplates($declaringClassReflection, $typeContext),
Expand All @@ -117,6 +104,36 @@ public function createFromReflection(\Reflector $reflection): ?TypeContext
);
}

private function createNewInstanceFromClassName(string $calledClassName, string $declaringClassName): TypeContext
{
$calledClassPath = explode('\\', $calledClassName);

$declaringClassReflection = self::$reflectionClassCache[$declaringClassName] ??= new \ReflectionClass($declaringClassName);

$typeContext = $this->createIntermediateTypeContext(end($calledClassPath), $declaringClassReflection);

return new TypeContext(
$typeContext->calledClassName,
$typeContext->declaringClassName,
$typeContext->namespace,
$typeContext->uses,
$this->collectTemplates($declaringClassReflection, $typeContext),
$this->collectTypeAliases($declaringClassReflection, $typeContext),
);
}

private function createIntermediateTypeContext(string $calledClassShortName, \ReflectionClass $declaringClassReflection): TypeContext
{
$declaringClassName = $declaringClassReflection->getName();

return $this->intermediateTypeContextCache[$declaringClassName][$calledClassShortName] ??= new TypeContext(
$calledClassShortName,
$declaringClassReflection->getShortName(),
trim($declaringClassReflection->getNamespaceName(), '\\'),
$this->collectUses($declaringClassReflection),
);
}

/**
* @return array<string, string>
*/
Expand Down
Loading