Skip to content

Commit 86cf97d

Browse files
jiripudilondrejmirtes
authored andcommitted
CallableType: accept [class-string, constant-string] array as callable
1 parent 72479ee commit 86cf97d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Type\CompoundType;
1414
use PHPStan\Type\ConstantType;
1515
use PHPStan\Type\ErrorType;
16+
use PHPStan\Type\Generic\GenericClassStringType;
1617
use PHPStan\Type\Generic\TemplateMixedType;
1718
use PHPStan\Type\Generic\TemplateTypeMap;
1819
use PHPStan\Type\Generic\TemplateTypeVariance;
@@ -367,6 +368,8 @@ public function findTypeAndMethodName(): ?ConstantArrayTypeAndMethod
367368
return ConstantArrayTypeAndMethod::createUnknown();
368369
}
369370
$type = new ObjectType($broker->getClass($classOrObject->getValue())->getName());
371+
} elseif ($classOrObject instanceof GenericClassStringType) {
372+
$type = $classOrObject->getGenericType();
370373
} elseif ((new \PHPStan\Type\ObjectWithoutClassType())->isSuperTypeOf($classOrObject)->yes()) {
371374
$type = $classOrObject;
372375
} else {

tests/PHPStan/Type/CallableTypeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use PHPStan\Reflection\PassedByReference;
77
use PHPStan\TrinaryLogic;
88
use PHPStan\Type\Accessory\HasMethodType;
9+
use PHPStan\Type\Constant\ConstantArrayType;
10+
use PHPStan\Type\Constant\ConstantIntegerType;
11+
use PHPStan\Type\Constant\ConstantStringType;
12+
use PHPStan\Type\Generic\GenericClassStringType;
913
use PHPStan\Type\Generic\TemplateTypeFactory;
1014
use PHPStan\Type\Generic\TemplateTypeScope;
1115
use PHPStan\Type\Generic\TemplateTypeVariance;
@@ -338,6 +342,17 @@ public function dataAccepts(): array
338342
TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new CallableType()),
339343
TrinaryLogic::createYes(),
340344
],
345+
[
346+
new CallableType(),
347+
new ConstantArrayType([
348+
new ConstantIntegerType(0),
349+
new ConstantIntegerType(1),
350+
], [
351+
new GenericClassStringType(new ObjectType(\Closure::class)),
352+
new ConstantStringType('bind'),
353+
]),
354+
TrinaryLogic::createYes(),
355+
],
341356
];
342357
}
343358

tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use PHPStan\TrinaryLogic;
66
use PHPStan\Type\ArrayType;
77
use PHPStan\Type\CallableType;
8+
use PHPStan\Type\Generic\GenericClassStringType;
89
use PHPStan\Type\Generic\TemplateTypeFactory;
910
use PHPStan\Type\Generic\TemplateTypeScope;
1011
use PHPStan\Type\Generic\TemplateTypeVariance;
1112
use PHPStan\Type\IntegerType;
1213
use PHPStan\Type\IterableType;
1314
use PHPStan\Type\MixedType;
15+
use PHPStan\Type\ObjectType;
1416
use PHPStan\Type\StringType;
1517
use PHPStan\Type\Type;
1618
use PHPStan\Type\TypeCombinator;
@@ -709,6 +711,17 @@ public function dataIsCallable(): iterable
709711
]),
710712
TrinaryLogic::createNo(),
711713
];
714+
715+
yield 'class-string' => [
716+
new ConstantArrayType([
717+
new ConstantIntegerType(0),
718+
new ConstantIntegerType(1),
719+
], [
720+
new GenericClassStringType(new ObjectType(\Closure::class)),
721+
new ConstantStringType('bind'),
722+
]),
723+
TrinaryLogic::createYes(),
724+
];
712725
}
713726

714727
}

0 commit comments

Comments
 (0)