Skip to content

Commit 2dda630

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Improve type inference for $object::class === static::class
Amp-Thread-ID: https://ampcode.com/threads/T-01a19219-4619-76d7-89f1-3dc38b076ad6
1 parent 091c5a7 commit 2dda630

3 files changed

Lines changed: 105 additions & 11 deletions

File tree

‎src/Analyser/ExprHandler/Helper/IdenticalNarrowingHelper.php‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,13 @@ private function specifyGeneral(
385385

386386
// a `$a::class` side falls back only where the old instanceof-style
387387
// blocks would fire: a true context with a single class-name string
388-
// on the other side; everything else narrows generically
388+
// or a `static::class`-like fetch on the other side; everything else
389+
// narrows generically
389390
if ($context->true()) {
390391
foreach ([
391-
[$unwrappedLeft, $left, $leftResult, $rightResult],
392-
[$unwrappedRight, $right, $rightResult, $leftResult],
393-
] as [$sideUnwrapped, $side, $sideResult, $otherResult]) {
392+
[$unwrappedLeft, $left, $leftResult, $unwrappedRight, $rightResult],
393+
[$unwrappedRight, $right, $rightResult, $unwrappedLeft, $leftResult],
394+
] as [$sideUnwrapped, $side, $sideResult, $otherUnwrapped, $otherResult]) {
394395
if (!($sideUnwrapped instanceof Expr\ClassConstFetch) || !($sideUnwrapped->class instanceof Expr)) {
395396
continue;
396397
}
@@ -400,8 +401,21 @@ private function specifyGeneral(
400401
if ($sideUnwrapped->name instanceof Expr || $sideUnwrapped->name->toLowerString() !== 'class') {
401402
continue;
402403
}
403-
$otherStrings = $otherResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted)->getConstantStrings();
404+
$otherType = $otherResult->getTypeOnScope($evaluationScope, $evaluationScope->nativeTypesPromoted);
405+
$otherStrings = $otherType->getConstantStrings();
404406
if (count($otherStrings) !== 1 || $otherStrings[0]->getValue() === '') {
407+
// $a::class === static::class narrows $a to the class-string's object type
408+
if ($otherUnwrapped instanceof Expr\ClassConstFetch && $otherUnwrapped->class instanceof Name) {
409+
$otherObjectType = $otherType->getClassStringObjectType();
410+
if ($otherObjectType->isObject()->yes()) {
411+
return $this->defaultNarrowingHelper->createForSubject(
412+
$sideUnwrapped->class,
413+
$otherObjectType,
414+
$context,
415+
$evaluationScope,
416+
)->unionWith($this->defaultNarrowingHelper->createSubjectTypes($evaluationScope, $side, $sideResult, $otherType, $context));
417+
}
418+
}
405419
continue;
406420
}
407421
if (!$this->reflectionProvider->hasClass($otherStrings[0]->getValue())) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace StaticClassConstEquals;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface A {
8+
public function equals(A $other): bool;
9+
}
10+
11+
class B implements A {
12+
public function equals(A $other): bool {
13+
if ($other::class === static::class) {
14+
assertType('static(StaticClassConstEquals\\B)', $other);
15+
return true;
16+
} else {
17+
assertType('StaticClassConstEquals\\A', $other);
18+
return false;
19+
}
20+
}
21+
}
22+
23+
class C implements A {
24+
public function equals(A $other): bool {
25+
if (get_class($other) === static::class) {
26+
assertType('static(StaticClassConstEquals\\C)', $other);
27+
return true;
28+
} else {
29+
assertType('StaticClassConstEquals\\A', $other);
30+
return false;
31+
}
32+
}
33+
}
34+
35+
class D implements A {
36+
public function equals(A $other): bool {
37+
if ($other::class === self::class) {
38+
assertType('StaticClassConstEquals\\D', $other);
39+
return true;
40+
} else {
41+
assertType('StaticClassConstEquals\\A', $other);
42+
return false;
43+
}
44+
}
45+
}
46+
47+
class E implements A {
48+
public function equals(A $other): bool {
49+
if (get_class($other) === self::class) {
50+
assertType('StaticClassConstEquals\\E', $other);
51+
return true;
52+
} else {
53+
assertType('StaticClassConstEquals\\A', $other);
54+
return false;
55+
}
56+
}
57+
}

‎turbo-ext/src/IdenticalNarrowingHelper.cpp‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,14 +1011,16 @@ class IdenticalNarrowingHelper
10111011

10121012
// a `$a::class` side falls back only where the old instanceof-style
10131013
// blocks would fire: a true context with a single class-name string
1014-
// on the other side; everything else narrows generically
1014+
// or a `static::class`-like fetch on the other side; everything else
1015+
// narrows generically
10151016
bool contextTrue;
10161017
if (UNEXPECTED(!ctxTrue(context, contextTrue))) return zv::Val();
10171018
if (contextTrue) {
10181019
for (int i = 0; i < 2; i++) {
10191020
zval *sideUnwrapped = i == 0 ? unwrappedLeft : unwrappedRight;
10201021
zval *side = i == 0 ? left : right;
10211022
zval *sideResult = i == 0 ? leftResult : rightResult;
1023+
zval *otherUnwrapped = i == 0 ? unwrappedRight : unwrappedLeft;
10221024
zval *otherResult = i == 0 ? rightResult : leftResult;
10231025
if (!isA(sideUnwrapped, PT_CLASS_CLASS_CONST_FETCH) || !isA(classOf(sideUnwrapped), PT_CLASS_EXPR)) {
10241026
if (UNEXPECTED(EG(exception))) return zv::Val();
@@ -1031,11 +1033,32 @@ class IdenticalNarrowingHelper
10311033
if (UNEXPECTED(otherType.isUndef())) return zv::Val();
10321034
zv::Val otherStrings = constantStringsOf(otherType.raw());
10331035
if (UNEXPECTED(otherStrings.isUndef())) return zv::Val();
1034-
if (countOf(otherStrings.raw()) != 1) continue;
1035-
zval *otherString = firstOf(otherStrings.raw());
1036-
zv::Val value = constantStringValue(otherString);
1037-
if (UNEXPECTED(value.isUndef())) return zv::Val();
1038-
if (Z_TYPE_P(value.raw()) == IS_STRING && ZSTR_LEN(Z_STR_P(value.raw())) == 0) continue;
1036+
bool singleNonEmpty = countOf(otherStrings.raw()) == 1;
1037+
zval *otherString = NULL;
1038+
zv::Val value;
1039+
if (singleNonEmpty) {
1040+
otherString = firstOf(otherStrings.raw());
1041+
value = constantStringValue(otherString);
1042+
if (UNEXPECTED(value.isUndef())) return zv::Val();
1043+
singleNonEmpty = !(Z_TYPE_P(value.raw()) == IS_STRING && ZSTR_LEN(Z_STR_P(value.raw())) == 0);
1044+
}
1045+
if (!singleNonEmpty) {
1046+
// $a::class === static::class narrows $a to the class-string's object type
1047+
bool otherIsClassConstFetch = isA(otherUnwrapped, PT_CLASS_CLASS_CONST_FETCH) && isA(classOf(otherUnwrapped), PT_CLASS_NAME);
1048+
if (UNEXPECTED(EG(exception))) return zv::Val();
1049+
if (otherIsClassConstFetch) {
1050+
zv::Val otherObjectType = typeCall(otherType.raw(), "getClassStringObjectType", PT_LC("getclassstringobjecttype"));
1051+
if (UNEXPECTED(otherObjectType.isUndef())) return zv::Val();
1052+
zend_long isObject = trinaryByName(otherObjectType.raw(), "isObject", PT_LC("isobject"));
1053+
if (UNEXPECTED(isObject < 0)) return zv::Val();
1054+
if (isObject == PT_TRI_YES) {
1055+
zv::Val classTypes = createForSubject(classOf(sideUnwrapped), otherObjectType.raw(), context, evaluationScope);
1056+
if (UNEXPECTED(classTypes.isUndef())) return zv::Val();
1057+
return unionWith(std::move(classTypes), createSubjectTypes(evaluationScope, side, sideResult, otherType.raw(), context));
1058+
}
1059+
}
1060+
continue;
1061+
}
10391062

10401063
bool known;
10411064
if (UNEXPECTED(!hasClass(value.raw(), known))) return zv::Val();

0 commit comments

Comments
 (0)