Skip to content

Commit 5154e11

Browse files
committed
Improve type inference for $object::class === static::class
1 parent b7973e4 commit 5154e11

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ public function specifyTypesInCondition(
330330

331331
$rightType = $scope->getType($expr->right);
332332
if (
333+
$context->true() &&
333334
$expr->left instanceof ClassConstFetch &&
334335
$expr->left->class instanceof Expr &&
335336
$expr->left->name instanceof Node\Identifier &&
@@ -347,6 +348,59 @@ public function specifyTypesInCondition(
347348
$rootExpr,
348349
);
349350
}
351+
if (
352+
$context->true()
353+
&& $expr->left instanceof ClassConstFetch
354+
&& $expr->left->class instanceof Expr
355+
&& $expr->left->name instanceof Node\Identifier
356+
&& $expr->right instanceof ClassConstFetch
357+
&& $rightType instanceof GenericClassStringType
358+
&& strtolower($expr->left->name->toString()) === 'class'
359+
) {
360+
return $this->create(
361+
$expr->left->class,
362+
$rightType->getGenericType(),
363+
$context,
364+
false,
365+
$scope,
366+
$rootExpr,
367+
);
368+
}
369+
if (
370+
$context->true()
371+
&& $expr->left instanceof FuncCall
372+
&& $expr->left->name instanceof Name
373+
&& strtolower($expr->left->name->toString()) === 'get_class'
374+
&& isset($expr->left->getArgs()[0])
375+
&& $rightType instanceof ConstantStringType
376+
) {
377+
return $this->specifyTypesInCondition(
378+
$scope,
379+
new Instanceof_(
380+
$expr->left->getArgs()[0]->value,
381+
new Name($rightType->getValue()),
382+
),
383+
$context,
384+
$rootExpr,
385+
);
386+
}
387+
if (
388+
$context->true()
389+
&& $expr->left instanceof FuncCall
390+
&& $expr->left->name instanceof Name
391+
&& strtolower($expr->left->name->toString()) === 'get_class'
392+
&& isset($expr->left->getArgs()[0])
393+
&& $rightType instanceof GenericClassStringType
394+
) {
395+
return $this->create(
396+
$expr->left->getArgs()[0]->value,
397+
$rightType->getGenericType(),
398+
$context,
399+
false,
400+
$scope,
401+
$rootExpr,
402+
);
403+
}
350404
if ($context->false()) {
351405
$identicalType = $scope->getType($expr);
352406
if ($identicalType instanceof ConstantBooleanType) {
@@ -1192,7 +1246,7 @@ private function processBooleanConditionalTypes(Scope $scope, SpecifiedTypes $le
11921246
}
11931247

11941248
/**
1195-
* @return (Expr|ConstantScalarType)[]|null
1249+
* @return array{Expr, ConstantScalarType}|null
11961250
*/
11971251
private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\BinaryOp $binaryOperation): ?array
11981252
{

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,7 @@ public function dataFileAsserts(): iterable
10441044
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8004.php');
10451045
yield from $this->gatherAssertTypes(__DIR__ . '/data/global-namespace.php');
10461046
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-offset-unset.php');
1047+
yield from $this->gatherAssertTypes(__DIR__ . '/data/static-class-const-equals.php');
10471048
}
10481049

10491050
/**
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+
}

0 commit comments

Comments
 (0)