Skip to content

Commit

Permalink
refactor ClassReflection call-site variance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil authored and ondrejmirtes committed Jun 23, 2023
1 parent 7e4cff9 commit c296ac2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
30 changes: 15 additions & 15 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ class ClassReflection

private ?TemplateTypeMap $activeTemplateTypeMap = null;

private ?TemplateTypeVarianceMap $templateTypeVarianceMap = null;
private ?TemplateTypeVarianceMap $defaultCallSiteVarianceMap = null;

private ?TemplateTypeVarianceMap $activeTemplateTypeVarianceMap = null;
private ?TemplateTypeVarianceMap $callSiteVarianceMap = null;

/** @var array<string,ClassReflection>|null */
private ?array $ancestors = null;
Expand Down Expand Up @@ -147,7 +147,7 @@ public function __construct(
private ?TemplateTypeMap $resolvedTemplateTypeMap,
private ?ResolvedPhpDocBlock $stubPhpDocBlock,
private ?string $extraCacheKey = null,
private ?TemplateTypeVarianceMap $resolvedTemplateTypeVarianceMap = null,
private ?TemplateTypeVarianceMap $resolvedCallSiteVarianceMap = null,
)
{
}
Expand Down Expand Up @@ -252,7 +252,7 @@ public function getDisplayName(bool $withTemplateTypes = true): string
return $name . '<' . implode(',', array_map(
static fn (Type $type, TemplateTypeVariance $variance): string => TypeProjectionHelper::describe($type, $variance, VerbosityLevel::typeOnly()),
$this->getActiveTemplateTypeMap()->getTypes(),
$this->getActiveTemplateTypeVarianceMap()->getVariances(),
$this->getCallSiteVarianceMap()->getVariances(),
)) . '>';
}

Expand All @@ -269,7 +269,7 @@ public function getCacheKey(): string
$cacheKey .= '<' . implode(',', array_map(
static fn (Type $type, TemplateTypeVariance $variance): string => TypeProjectionHelper::describe($type, $variance, VerbosityLevel::cache()),
$this->getActiveTemplateTypeMap()->getTypes(),
$this->getActiveTemplateTypeVarianceMap()->getVariances(),
$this->getCallSiteVarianceMap()->getVariances(),
)) . '>';
}

Expand Down Expand Up @@ -1255,30 +1255,30 @@ public function getPossiblyIncompleteActiveTemplateTypeMap(): TemplateTypeMap
return $this->resolvedTemplateTypeMap ?? $this->getTemplateTypeMap();
}

public function getTemplateTypeVarianceMap(): TemplateTypeVarianceMap
private function getDefaultCallSiteVarianceMap(): TemplateTypeVarianceMap
{
if ($this->templateTypeVarianceMap !== null) {
return $this->templateTypeVarianceMap;
if ($this->defaultCallSiteVarianceMap !== null) {
return $this->defaultCallSiteVarianceMap;
}

$resolvedPhpDoc = $this->getResolvedPhpDoc();
if ($resolvedPhpDoc === null) {
$this->templateTypeVarianceMap = TemplateTypeVarianceMap::createEmpty();
return $this->templateTypeVarianceMap;
$this->defaultCallSiteVarianceMap = TemplateTypeVarianceMap::createEmpty();
return $this->defaultCallSiteVarianceMap;
}

$map = [];
foreach ($this->getTemplateTags() as $templateTag) {
$map[$templateTag->getName()] = TemplateTypeVariance::createInvariant();
}

$this->templateTypeVarianceMap = new TemplateTypeVarianceMap($map);
return $this->templateTypeVarianceMap;
$this->defaultCallSiteVarianceMap = new TemplateTypeVarianceMap($map);
return $this->defaultCallSiteVarianceMap;
}

public function getActiveTemplateTypeVarianceMap(): TemplateTypeVarianceMap
public function getCallSiteVarianceMap(): TemplateTypeVarianceMap
{
return $this->activeTemplateTypeVarianceMap ??= $this->resolvedTemplateTypeVarianceMap ?? $this->getTemplateTypeVarianceMap();
return $this->callSiteVarianceMap ??= $this->resolvedCallSiteVarianceMap ?? $this->getDefaultCallSiteVarianceMap();
}

public function isGeneric(): bool
Expand Down Expand Up @@ -1387,7 +1387,7 @@ public function withTypes(array $types): self
$this->typeMapFromList($types),
$this->stubPhpDocBlock,
null,
$this->resolvedTemplateTypeVarianceMap,
$this->resolvedCallSiteVarianceMap,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static function createFromReflection(ClassReflection $reflection): self
$reflection->typeMapToList($reflection->getActiveTemplateTypeMap()),
null,
null,
$reflection->varianceMapToList($reflection->getActiveTemplateTypeVarianceMap()),
$reflection->varianceMapToList($reflection->getCallSiteVarianceMap()),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getStaticObjectType(): ObjectType
if ($this->staticObjectType === null) {
if ($this->classReflection->isGeneric()) {
$typeMap = $this->classReflection->getActiveTemplateTypeMap()->map(static fn (string $name, Type $type): Type => TemplateTypeHelper::toArgument($type));
$varianceMap = $this->classReflection->getActiveTemplateTypeVarianceMap();
$varianceMap = $this->classReflection->getCallSiteVarianceMap();
return $this->staticObjectType = new GenericObjectType(
$this->classReflection->getName(),
$this->classReflection->typeMapToList($typeMap),
Expand Down

0 comments on commit c296ac2

Please sign in to comment.