Skip to content

Commit

Permalink
Fixed methods caching issue because of same cache key for implicit/ex…
Browse files Browse the repository at this point in the history
…plicit mixed
  • Loading branch information
ondrejmirtes committed Feb 12, 2020
1 parent 1ea1a96 commit f83240f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function getCacheKey(): string

if ($this->resolvedTemplateTypeMap !== null) {
$cacheKey .= '<' . implode(',', array_map(static function (Type $type): string {
return $type->describe(VerbosityLevel::precise());
return $type->describe(VerbosityLevel::cache());
}, $this->resolvedTemplateTypeMap->getTypes())) . '>';
}

Expand Down
14 changes: 14 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,20 @@ function () use ($level): string {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
}

return $description;
},
function () use ($level): string {
$description = 'mixed';
if ($this->subtractedType !== null) {
$description .= sprintf('~%s', $this->subtractedType->describe($level));
}

if ($this->isExplicitMixed) {
$description .= '=explicit';
} else {
$description .= '=implicit';
}

return $description;
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic

public function isSuperTypeOf(Type $type): TrinaryLogic
{
$description = $type->describe(VerbosityLevel::precise());
$description = $type->describe(VerbosityLevel::cache());
if (isset($this->superTypes[$description])) {
return $this->superTypes[$description];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static function union(Type ...$types): Type
}
if ($types[$i] instanceof ConstantScalarType) {
$type = $types[$i];
$scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::precise()))] = $type;
$scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::cache()))] = $type;
unset($types[$i]);
continue;
}
Expand All @@ -206,7 +206,7 @@ public static function union(Type ...$types): Type
continue;
}
if ($innerType instanceof AccessoryType || $innerType instanceof CallableType) {
$intermediateAccessoryTypes[$innerType->describe(VerbosityLevel::precise())] = $innerType;
$intermediateAccessoryTypes[$innerType->describe(VerbosityLevel::cache())] = $innerType;
continue;
}
}
Expand Down
34 changes: 31 additions & 3 deletions src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class VerbosityLevel
private const TYPE_ONLY = 1;
private const VALUE = 2;
private const PRECISE = 3;
private const CACHE = 4;

/** @var self[] */
private static $registry;
Expand Down Expand Up @@ -41,6 +42,11 @@ public static function precise(): self
return self::create(self::PRECISE);
}

public static function cache(): self
{
return self::create(self::CACHE);
}

public static function getRecommendedLevelByType(Type $type): self
{
if (TypeUtils::containsCallable($type) || count(TypeUtils::getConstantArrays($type)) > 0) {
Expand All @@ -54,23 +60,45 @@ public static function getRecommendedLevelByType(Type $type): self
* @param callable(): string $typeOnlyCallback
* @param callable(): string $valueCallback
* @param callable(): string|null $preciseCallback
* @param callable(): string|null $cacheCallback
* @return string
*/
public function handle(
callable $typeOnlyCallback,
callable $valueCallback,
?callable $preciseCallback = null
?callable $preciseCallback = null,
?callable $cacheCallback = null
): string
{
if ($this->value === self::TYPE_ONLY) {
return $typeOnlyCallback();
}

if ($this->value === self::VALUE || $preciseCallback === null) {
if ($this->value === self::VALUE) {
return $valueCallback();
}

if ($this->value === self::PRECISE) {
if ($preciseCallback !== null) {
return $preciseCallback();
}

return $valueCallback();
}

if ($this->value === self::CACHE) {
if ($cacheCallback !== null) {
return $cacheCallback();
}

if ($preciseCallback !== null) {
return $preciseCallback();
}

return $valueCallback();
}

return $preciseCallback();
throw new \PHPStan\ShouldNotHappenException();
}

}

0 comments on commit f83240f

Please sign in to comment.