Skip to content

Commit

Permalink
TypeSpecifier - support instanceof parent
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 9, 2018
1 parent 5950aa8 commit af17140
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Analyser/TypeSpecifier.php
Expand Up @@ -26,6 +26,7 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NonexistentParentClassType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
Expand Down Expand Up @@ -66,6 +67,15 @@ public function specifyTypesInCondition(Scope $scope, Expr $expr, int $context =
$type = new ObjectType($scope->getClassReflection()->getName());
} elseif ($lowercasedClassName === 'static' && $scope->isInClass()) {
$type = new StaticType($scope->getClassReflection()->getName());
} elseif ($lowercasedClassName === 'parent') {
if (
$scope->isInClass()
&& $scope->getClassReflection()->getParentClass() !== false
) {
$type = new ObjectType($scope->getClassReflection()->getParentClass()->getName());
} else {
$type = new NonexistentParentClassType();
}
} else {
$type = new ObjectType($className);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -2545,6 +2545,10 @@ public function dataInstanceOf(): array
'$this(InstanceOfNamespace\Foo)&InstanceOfNamespace\BarInterface',
'$this',
],
[
'InstanceOfNamespace\BarParent',
'$parent',
],
];
}

Expand Down
12 changes: 10 additions & 2 deletions tests/PHPStan/Analyser/data/instanceof.php
Expand Up @@ -10,14 +10,20 @@ interface BarInterface

}

class Foo
abstract class BarParent
{

}

class Foo extends BarParent
{

public function someMethod(Expr $foo)
{
$bar = $foo;
$baz = doFoo();
$intersected = new Foo();
$parent = doFoo();

if ($baz instanceof Foo) {
// ...
Expand All @@ -29,7 +35,9 @@ public function someMethod(Expr $foo)
if ($self instanceof self) {
if ($intersected instanceof BarInterface) {
if ($this instanceof BarInterface) {
die;
if ($parent instanceof parent) {
die;
}
}
}
}
Expand Down

0 comments on commit af17140

Please sign in to comment.