Skip to content

Commit

Permalink
Cache usual suspects
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 2, 2021
1 parent 83a6a64 commit dbe08a6
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 30 deletions.
20 changes: 19 additions & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class ClassReflection implements ReflectionWithFilename
/** @var string|false|null */
private $reflectionDocComment;

/** @var \PHPStan\Reflection\ClassReflection[]|null */
private ?array $cachedInterfaces = null;

/** @var \PHPStan\Reflection\ClassReflection|false|null */
private $cachedParentClass = null;

/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
* @param \PHPStan\Type\FileTypeMapper $fileTypeMapper
Expand Down Expand Up @@ -178,10 +184,14 @@ public function getFileNameWithPhpDocs(): ?string
*/
public function getParentClass()
{
if ($this->cachedParentClass !== null) {
return $this->cachedParentClass;
}

$parentClass = $this->reflection->getParentClass();

if ($parentClass === false) {
return false;
return $this->cachedParentClass = false;
}

$extendsTag = $this->getFirstExtendsTag();
Expand Down Expand Up @@ -210,6 +220,8 @@ public function getParentClass()
);
}

$this->cachedParentClass = $parentReflection;

return $parentReflection;
}

Expand Down Expand Up @@ -570,6 +582,10 @@ public function getParents(): array
*/
public function getInterfaces(): array
{
if ($this->cachedInterfaces !== null) {
return $this->cachedInterfaces;
}

$interfaces = [];

$parent = $this->getParentClass();
Expand Down Expand Up @@ -638,6 +654,8 @@ public function getInterfaces(): array
);
}

$this->cachedInterfaces = $interfaces;

return $interfaces;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class CallbackUnresolvedMethodPrototypeReflection implements UnresolvedMethodPro
/** @var callable(Type): Type */
private $transformStaticTypeCallback;

private ?MethodReflection $transformedMethod = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param MethodReflection $methodReflection
* @param ClassReflection $resolvedDeclaringClass
Expand All @@ -45,7 +49,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedMethodPrototypeReflection
{
return new self(
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self(
$this->methodReflection,
$this->resolvedDeclaringClass,
false,
Expand All @@ -60,9 +68,12 @@ public function getNakedMethod(): MethodReflection

public function getTransformedMethod(): MethodReflection
{
if ($this->transformedMethod !== null) {
return $this->transformedMethod;
}
$templateTypeMap = $this->resolvedDeclaringClass->getActiveTemplateTypeMap();

return new ResolvedMethodReflection(
return $this->transformedMethod = new ResolvedMethodReflection(
$this->transformMethodWithStaticType($this->resolvedDeclaringClass, $this->methodReflection),
$this->resolveTemplateTypeMapToBounds ? $templateTypeMap->resolveToBounds() : $templateTypeMap
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class CallbackUnresolvedPropertyPrototypeReflection implements UnresolvedPropert
/** @var callable(Type): Type */
private $transformStaticTypeCallback;

private ?PropertyReflection $transformedProperty = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param PropertyReflection $propertyReflection
* @param ClassReflection $resolvedDeclaringClass
Expand All @@ -41,7 +45,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototypeReflection
{
return new self(
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self(
$this->propertyReflection,
$this->resolvedDeclaringClass,
false,
Expand All @@ -56,9 +64,12 @@ public function getNakedProperty(): PropertyReflection

public function getTransformedProperty(): PropertyReflection
{
if ($this->transformedProperty !== null) {
return $this->transformedProperty;
}
$templateTypeMap = $this->resolvedDeclaringClass->getActiveTemplateTypeMap();

return new ResolvedPropertyReflection(
return $this->transformedProperty = new ResolvedPropertyReflection(
$this->transformPropertyWithStaticType($this->resolvedDeclaringClass, $this->propertyReflection),
$this->resolveTemplateTypeMapToBounds ? $templateTypeMap->resolveToBounds() : $templateTypeMap
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class CalledOnTypeUnresolvedMethodPrototypeReflection implements UnresolvedMetho

private Type $calledOnType;

private ?MethodReflection $transformedMethod = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

public function __construct(
MethodReflection $methodReflection,
ClassReflection $resolvedDeclaringClass,
Expand All @@ -40,7 +44,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedMethodPrototypeReflection
{
return new self(
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self(
$this->methodReflection,
$this->resolvedDeclaringClass,
false,
Expand All @@ -55,9 +63,12 @@ public function getNakedMethod(): MethodReflection

public function getTransformedMethod(): MethodReflection
{
if ($this->transformedMethod !== null) {
return $this->transformedMethod;
}
$templateTypeMap = $this->resolvedDeclaringClass->getActiveTemplateTypeMap();

return new ResolvedMethodReflection(
return $this->transformedMethod = new ResolvedMethodReflection(
$this->transformMethodWithStaticType($this->resolvedDeclaringClass, $this->methodReflection),
$this->resolveTemplateTypeMapToBounds ? $templateTypeMap->resolveToBounds() : $templateTypeMap
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class CalledOnTypeUnresolvedPropertyPrototypeReflection implements UnresolvedPro

private Type $fetchedOnType;

private ?PropertyReflection $transformedProperty = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

public function __construct(
PropertyReflection $propertyReflection,
ClassReflection $resolvedDeclaringClass,
Expand All @@ -36,7 +40,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototypeReflection
{
return new self(
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self(
$this->propertyReflection,
$this->resolvedDeclaringClass,
false,
Expand All @@ -51,9 +59,12 @@ public function getNakedProperty(): PropertyReflection

public function getTransformedProperty(): PropertyReflection
{
if ($this->transformedProperty !== null) {
return $this->transformedProperty;
}
$templateTypeMap = $this->resolvedDeclaringClass->getActiveTemplateTypeMap();

return new ResolvedPropertyReflection(
return $this->transformedProperty = new ResolvedPropertyReflection(
$this->transformPropertyWithStaticType($this->resolvedDeclaringClass, $this->propertyReflection),
$this->resolveTemplateTypeMapToBounds ? $templateTypeMap->resolveToBounds() : $templateTypeMap
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class IntersectionTypeUnresolvedMethodPrototypeReflection implements UnresolvedM
/** @var UnresolvedMethodPrototypeReflection[] */
private array $methodPrototypes;

private ?MethodReflection $transformedMethod = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param UnresolvedMethodPrototypeReflection[] $methodPrototypes
*/
Expand All @@ -27,7 +31,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedMethodPrototypeReflection
{
return new self($this->methodName, array_map(static function (UnresolvedMethodPrototypeReflection $prototype): UnresolvedMethodPrototypeReflection {
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self($this->methodName, array_map(static function (UnresolvedMethodPrototypeReflection $prototype): UnresolvedMethodPrototypeReflection {
return $prototype->doNotResolveTemplateTypeMapToBounds();
}, $this->methodPrototypes));
}
Expand All @@ -39,11 +47,14 @@ public function getNakedMethod(): MethodReflection

public function getTransformedMethod(): MethodReflection
{
if ($this->transformedMethod !== null) {
return $this->transformedMethod;
}
$methods = array_map(static function (UnresolvedMethodPrototypeReflection $prototype): MethodReflection {
return $prototype->getTransformedMethod();
}, $this->methodPrototypes);

return new IntersectionTypeMethodReflection($this->methodName, $methods);
return $this->transformedMethod = new IntersectionTypeMethodReflection($this->methodName, $methods);
}

public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class IntersectionTypeUnresolvedPropertyPrototypeReflection implements Unresolve
/** @var UnresolvedPropertyPrototypeReflection[] */
private array $propertyPrototypes;

private ?PropertyReflection $transformedProperty = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param UnresolvedPropertyPrototypeReflection[] $propertyPrototypes
*/
Expand All @@ -27,7 +31,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototypeReflection
{
return new self($this->propertyName, array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): UnresolvedPropertyPrototypeReflection {
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self($this->propertyName, array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): UnresolvedPropertyPrototypeReflection {
return $prototype->doNotResolveTemplateTypeMapToBounds();
}, $this->propertyPrototypes));
}
Expand All @@ -39,11 +47,14 @@ public function getNakedProperty(): PropertyReflection

public function getTransformedProperty(): PropertyReflection
{
if ($this->transformedProperty !== null) {
return $this->transformedProperty;
}
$properties = array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): PropertyReflection {
return $prototype->getTransformedProperty();
}, $this->propertyPrototypes);

return new IntersectionTypePropertyReflection($properties);
return $this->transformedProperty = new IntersectionTypePropertyReflection($properties);
}

public function withFechedOnType(Type $type): UnresolvedPropertyPrototypeReflection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class UnionTypeUnresolvedMethodPrototypeReflection implements UnresolvedMethodPr
/** @var UnresolvedMethodPrototypeReflection[] */
private array $methodPrototypes;

private ?MethodReflection $transformedMethod = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param UnresolvedMethodPrototypeReflection[] $methodPrototypes
*/
Expand All @@ -27,7 +31,11 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedMethodPrototypeReflection
{
return new self($this->methodName, array_map(static function (UnresolvedMethodPrototypeReflection $prototype): UnresolvedMethodPrototypeReflection {
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}

return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self($this->methodName, array_map(static function (UnresolvedMethodPrototypeReflection $prototype): UnresolvedMethodPrototypeReflection {
return $prototype->doNotResolveTemplateTypeMapToBounds();
}, $this->methodPrototypes));
}
Expand All @@ -39,11 +47,15 @@ public function getNakedMethod(): MethodReflection

public function getTransformedMethod(): MethodReflection
{
if ($this->transformedMethod !== null) {
return $this->transformedMethod;
}

$methods = array_map(static function (UnresolvedMethodPrototypeReflection $prototype): MethodReflection {
return $prototype->getTransformedMethod();
}, $this->methodPrototypes);

return new UnionTypeMethodReflection($this->methodName, $methods);
return $this->transformedMethod = new UnionTypeMethodReflection($this->methodName, $methods);
}

public function withCalledOnType(Type $type): UnresolvedMethodPrototypeReflection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class UnionTypeUnresolvedPropertyPrototypeReflection implements UnresolvedProper
/** @var UnresolvedPropertyPrototypeReflection[] */
private array $propertyPrototypes;

private ?PropertyReflection $transformedProperty = null;

private ?self $cachedDoNotResolveTemplateTypeMapToBounds = null;

/**
* @param UnresolvedPropertyPrototypeReflection[] $propertyPrototypes
*/
Expand All @@ -27,7 +31,10 @@ public function __construct(

public function doNotResolveTemplateTypeMapToBounds(): UnresolvedPropertyPrototypeReflection
{
return new self($this->propertyName, array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): UnresolvedPropertyPrototypeReflection {
if ($this->cachedDoNotResolveTemplateTypeMapToBounds !== null) {
return $this->cachedDoNotResolveTemplateTypeMapToBounds;
}
return $this->cachedDoNotResolveTemplateTypeMapToBounds = new self($this->propertyName, array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): UnresolvedPropertyPrototypeReflection {
return $prototype->doNotResolveTemplateTypeMapToBounds();
}, $this->propertyPrototypes));
}
Expand All @@ -39,11 +46,15 @@ public function getNakedProperty(): PropertyReflection

public function getTransformedProperty(): PropertyReflection
{
if ($this->transformedProperty !== null) {
return $this->transformedProperty;
}

$methods = array_map(static function (UnresolvedPropertyPrototypeReflection $prototype): PropertyReflection {
return $prototype->getTransformedProperty();
}, $this->propertyPrototypes);

return new UnionTypePropertyReflection($methods);
return $this->transformedProperty = new UnionTypePropertyReflection($methods);
}

public function withFechedOnType(Type $type): UnresolvedPropertyPrototypeReflection
Expand Down
Loading

0 comments on commit dbe08a6

Please sign in to comment.