Skip to content

Commit 767435e

Browse files
committed
infer type on strict comparison with get_class()
1 parent cdb0fe6 commit 767435e

3 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,7 @@ public function specifyTypesInCondition(
329329
&& isset($exprNode->getArgs()[0])
330330
&& $constantType instanceof ConstantStringType
331331
) {
332-
return $this->specifyTypesInCondition(
333-
$scope,
334-
new Instanceof_(
335-
$exprNode->getArgs()[0]->value,
336-
new Name($constantType->getValue()),
337-
),
338-
$context,
339-
$rootExpr,
340-
);
332+
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context, $rootExpr);
341333
}
342334
}
343335

@@ -1042,6 +1034,23 @@ private function specifyTypesForConstantStringBinaryExpression(
10421034
}
10431035
}
10441036

1037+
if (
1038+
$exprNode instanceof FuncCall
1039+
&& $exprNode->name instanceof Name
1040+
&& strtolower($exprNode->name->toString()) === 'get_class'
1041+
&& isset($exprNode->getArgs()[0])
1042+
) {
1043+
return $this->specifyTypesInCondition(
1044+
$scope,
1045+
new Instanceof_(
1046+
$exprNode->getArgs()[0]->value,
1047+
new Name($constantType->getValue()),
1048+
),
1049+
$context,
1050+
$rootExpr,
1051+
);
1052+
}
1053+
10451054
if (
10461055
$exprNode instanceof FuncCall
10471056
&& $exprNode->name instanceof Name

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ public function dataFileAsserts(): iterable
10531053
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-offset-unset.php');
10541054
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8008.php');
10551055
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5552.php');
1056+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1239.php');
10561057
}
10571058

10581059
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug1239;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
public function __construct()
10+
{
11+
if (get_class($this) === Bar::class) {
12+
assertType('$this(Bug1239\Foo)&Bug1239\Bar', $this);
13+
$this->bat();
14+
}
15+
}
16+
}
17+
18+
class Bar extends Foo
19+
{
20+
public function bat(): void
21+
{
22+
}
23+
}

0 commit comments

Comments
 (0)