Skip to content

Commit 6e853da

Browse files
authored
Faster FiniteTypeSet->key() (#6228)
1 parent 6c642f1 commit 6e853da

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

‎phpstan-baseline.neon‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,12 @@ parameters:
10831083
count: 1
10841084
path: src/Type/FileTypeMapper.php
10851085

1086+
-
1087+
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
1088+
identifier: phpstanApi.instanceofType
1089+
count: 2
1090+
path: src/Type/FiniteTypeSet.php
1091+
10861092
-
10871093
rawMessage: 'Doing instanceof PHPStan\Type\FloatType is error-prone and deprecated. Use Type::isFloat() instead.'
10881094
identifier: phpstanApi.instanceofType

‎src/Type/FiniteTypeSet.php‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,18 @@ public static function create(array $types): ?self
102102
*/
103103
public static function key(Type $type): ?string
104104
{
105-
if ($type instanceof TemplateType) {
105+
if ($type instanceof TemplateType || $type instanceof UnionType || $type instanceof IntersectionType) {
106106
return null;
107107
}
108108

109-
// Only a bare case is safe to key by class + case name: for anything else -
110-
// $this & Enum::C, a whole single-case enum, an enum subtracted to one case -
111-
// EnumCaseObjectType::equals() is false because it requires an EnumCaseObjectType,
112-
// which makes instanceof exactly the question being asked here. Type::getEnumCases()
113-
// would answer it too, but only by resolving a ClassReflection - and a key has to be
114-
// derivable from the type alone, on every comparison, without reflection.
115-
// Key by class + case name, the identity equals() compares (describe() would also
116-
// fold in a subtracted type, which equals() ignores).
117109
$enumCaseObject = $type->getEnumCaseObject();
118-
if ($enumCaseObject !== null && $enumCaseObject->equals($type)) {
110+
if ($enumCaseObject !== null) {
119111
return self::ENUM_CASE_KEY_PREFIX . $enumCaseObject->getClassName() . '::' . $enumCaseObject->getEnumCaseName();
120112
}
121113

122-
$scalarTypes = $type->getConstantScalarTypes();
123-
if (count($scalarTypes) === 1 && $scalarTypes[0]->equals($type)) {
124-
$value = $scalarTypes[0]->getValue();
114+
$scalarValues = $type->getConstantScalarValues();
115+
if (count($scalarValues) === 1) {
116+
$value = $scalarValues[0];
125117
if ($value === null) {
126118
return self::NULL_KEY;
127119
}
@@ -152,9 +144,13 @@ public static function key(Type $type): ?string
152144
*/
153145
private static function kind(Type $type): string
154146
{
155-
$enumCaseObject = $type->getEnumCaseObject();
156-
if ($enumCaseObject !== null && $enumCaseObject->equals($type)) {
157-
return self::ENUM_CASE_KEY_PREFIX . $enumCaseObject->getClassName();
147+
if (!$type instanceof UnionType
148+
&& !$type instanceof IntersectionType
149+
) {
150+
$enumCaseObject = $type->getEnumCaseObject();
151+
if ($enumCaseObject !== null) {
152+
return self::ENUM_CASE_KEY_PREFIX . $enumCaseObject->getClassName();
153+
}
158154
}
159155

160156
return get_class($type);

0 commit comments

Comments
 (0)