Skip to content

Commit b4e4a29

Browse files
jiripudilondrejmirtes
authored andcommitted
is_a(): support class-string in parameter $class
1 parent 86cf97d commit b4e4a29

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Type/Php/IsAFunctionTypeSpecifyingExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
5757
} elseif ($classNameArgExprType instanceof ConstantStringType) {
5858
$objectType = new ObjectType($classNameArgExprType->getValue());
5959
$types = $this->typeSpecifier->create($node->args[0]->value, $objectType, $context);
60+
} elseif ($classNameArgExprType instanceof GenericClassStringType) {
61+
$objectType = $classNameArgExprType->getGenericType();
62+
$types = $this->typeSpecifier->create($node->args[0]->value, $objectType, $context);
6063
} elseif ($context->true()) {
6164
$objectType = new ObjectWithoutClassType();
6265
$types = $this->typeSpecifier->create($node->args[0]->value, $objectType, $context);

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
use PhpParser\Node\Scalar\String_;
1818
use PhpParser\Node\VarLikeIdentifier;
1919
use PHPStan\Type\ArrayType;
20+
use PHPStan\Type\ClassStringType;
2021
use PHPStan\Type\Constant\ConstantBooleanType;
22+
use PHPStan\Type\Generic\GenericClassStringType;
2123
use PHPStan\Type\MixedType;
2224
use PHPStan\Type\NullType;
2325
use PHPStan\Type\ObjectType;
@@ -57,6 +59,8 @@ protected function setUp(): void
5759
$this->scope = $this->scope->assignVariable('stringOrFalse', new UnionType([new StringType(), new ConstantBooleanType(false)]));
5860
$this->scope = $this->scope->assignVariable('array', new ArrayType(new MixedType(), new MixedType()));
5961
$this->scope = $this->scope->assignVariable('foo', new MixedType());
62+
$this->scope = $this->scope->assignVariable('classString', new ClassStringType());
63+
$this->scope = $this->scope->assignVariable('genericClassString', new GenericClassStringType(new ObjectType('Bar')));
6064
}
6165

6266
/**
@@ -437,6 +441,22 @@ public function dataCondition(): array
437441
['$foo' => 'static(DateTime)'],
438442
['$foo' => '~static(DateTime)'],
439443
],
444+
[
445+
new FuncCall(new Name('is_a'), [
446+
new Arg(new Variable('foo')),
447+
new Arg(new Variable('classString')),
448+
]),
449+
['$foo' => 'object'],
450+
[],
451+
],
452+
[
453+
new FuncCall(new Name('is_a'), [
454+
new Arg(new Variable('foo')),
455+
new Arg(new Variable('genericClassString')),
456+
]),
457+
['$foo' => 'Bar'],
458+
['$foo' => '~Bar'],
459+
],
440460
[
441461
new FuncCall(new Name('is_a'), [
442462
new Arg(new Variable('foo')),

0 commit comments

Comments
 (0)