Skip to content

Commit f83240f

Browse files
committed
Fixed methods caching issue because of same cache key for implicit/explicit mixed
1 parent 1ea1a96 commit f83240f

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

src/Reflection/ClassReflection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function getCacheKey(): string
239239

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

src/Type/MixedType.php

+14
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ function () use ($level): string {
244244
$description .= sprintf('~%s', $this->subtractedType->describe($level));
245245
}
246246

247+
return $description;
248+
},
249+
function () use ($level): string {
250+
$description = 'mixed';
251+
if ($this->subtractedType !== null) {
252+
$description .= sprintf('~%s', $this->subtractedType->describe($level));
253+
}
254+
255+
if ($this->isExplicitMixed) {
256+
$description .= '=explicit';
257+
} else {
258+
$description .= '=implicit';
259+
}
260+
247261
return $description;
248262
}
249263
);

src/Type/ObjectType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function accepts(Type $type, bool $strictTypes): TrinaryLogic
132132

133133
public function isSuperTypeOf(Type $type): TrinaryLogic
134134
{
135-
$description = $type->describe(VerbosityLevel::precise());
135+
$description = $type->describe(VerbosityLevel::cache());
136136
if (isset($this->superTypes[$description])) {
137137
return $this->superTypes[$description];
138138
}

src/Type/TypeCombinator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static function union(Type ...$types): Type
181181
}
182182
if ($types[$i] instanceof ConstantScalarType) {
183183
$type = $types[$i];
184-
$scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::precise()))] = $type;
184+
$scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::cache()))] = $type;
185185
unset($types[$i]);
186186
continue;
187187
}
@@ -206,7 +206,7 @@ public static function union(Type ...$types): Type
206206
continue;
207207
}
208208
if ($innerType instanceof AccessoryType || $innerType instanceof CallableType) {
209-
$intermediateAccessoryTypes[$innerType->describe(VerbosityLevel::precise())] = $innerType;
209+
$intermediateAccessoryTypes[$innerType->describe(VerbosityLevel::cache())] = $innerType;
210210
continue;
211211
}
212212
}

src/Type/VerbosityLevel.php

+31-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class VerbosityLevel
88
private const TYPE_ONLY = 1;
99
private const VALUE = 2;
1010
private const PRECISE = 3;
11+
private const CACHE = 4;
1112

1213
/** @var self[] */
1314
private static $registry;
@@ -41,6 +42,11 @@ public static function precise(): self
4142
return self::create(self::PRECISE);
4243
}
4344

45+
public static function cache(): self
46+
{
47+
return self::create(self::CACHE);
48+
}
49+
4450
public static function getRecommendedLevelByType(Type $type): self
4551
{
4652
if (TypeUtils::containsCallable($type) || count(TypeUtils::getConstantArrays($type)) > 0) {
@@ -54,23 +60,45 @@ public static function getRecommendedLevelByType(Type $type): self
5460
* @param callable(): string $typeOnlyCallback
5561
* @param callable(): string $valueCallback
5662
* @param callable(): string|null $preciseCallback
63+
* @param callable(): string|null $cacheCallback
5764
* @return string
5865
*/
5966
public function handle(
6067
callable $typeOnlyCallback,
6168
callable $valueCallback,
62-
?callable $preciseCallback = null
69+
?callable $preciseCallback = null,
70+
?callable $cacheCallback = null
6371
): string
6472
{
6573
if ($this->value === self::TYPE_ONLY) {
6674
return $typeOnlyCallback();
6775
}
6876

69-
if ($this->value === self::VALUE || $preciseCallback === null) {
77+
if ($this->value === self::VALUE) {
78+
return $valueCallback();
79+
}
80+
81+
if ($this->value === self::PRECISE) {
82+
if ($preciseCallback !== null) {
83+
return $preciseCallback();
84+
}
85+
86+
return $valueCallback();
87+
}
88+
89+
if ($this->value === self::CACHE) {
90+
if ($cacheCallback !== null) {
91+
return $cacheCallback();
92+
}
93+
94+
if ($preciseCallback !== null) {
95+
return $preciseCallback();
96+
}
97+
7098
return $valueCallback();
7199
}
72100

73-
return $preciseCallback();
101+
throw new \PHPStan\ShouldNotHappenException();
74102
}
75103

76104
}

0 commit comments

Comments
 (0)