Skip to content

Commit a5410fc

Browse files
authored
Do not degrade closures in array shapes
1 parent 62fd66e commit a5410fc

7 files changed

Lines changed: 98 additions & 5 deletions

File tree

‎src/PhpDoc/TypeNodeResolver.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,10 @@ private function resolveArrayShapeNode(ArrayShapeNode $typeNode, NameScope $name
10541054
}
10551055

10561056
foreach ($typeNode->items as $itemNode) {
1057+
if ($itemNode->valueType instanceof CallableTypeNode) {
1058+
$builder->disableClosureDegradation();
1059+
}
1060+
10571061
$offsetType = $this->resolveArrayShapeOffsetType($itemNode, $nameScope);
10581062
$builder->setOffsetValueType($offsetType, $this->resolve($itemNode->valueType, $nameScope), $itemNode->optional);
10591063
}

‎src/Type/Constant/ConstantArrayTypeBuilder.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ final class ConstantArrayTypeBuilder
3131
{
3232

3333
public const ARRAY_COUNT_LIMIT = 256;
34-
private const CLOSURES_COUNT_LIMIT = 16;
34+
private const CLOSURES_COUNT_LIMIT = 32;
3535

3636
private bool $degradeToGeneralArray = false;
3737

38-
private bool $degradeClosures = false;
38+
private ?bool $degradeClosures = null;
3939

4040
private bool $oversized = false;
4141

@@ -84,7 +84,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $opt
8484
}
8585

8686
if (!$this->degradeToGeneralArray) {
87-
if ($valueType instanceof ClosureType) {
87+
if ($valueType instanceof ClosureType && $this->degradeClosures !== false) {
8888
$numClosures = 1;
8989
foreach ($this->valueTypes as $innerType) {
9090
if (!($innerType instanceof ClosureType)) {
@@ -300,6 +300,11 @@ public function degradeToGeneralArray(bool $oversized = false): void
300300
$this->oversized = $this->oversized || $oversized;
301301
}
302302

303+
public function disableClosureDegradation(): void
304+
{
305+
$this->degradeClosures = false;
306+
}
307+
303308
public function getArray(): Type
304309
{
305310
$keyTypesCount = count($this->keyTypes);
@@ -313,7 +318,7 @@ public function getArray(): Type
313318
return new ConstantArrayType($keyTypes, $this->valueTypes, $this->nextAutoIndexes, $this->optionalKeys, $this->isList);
314319
}
315320

316-
if ($this->degradeClosures) {
321+
if ($this->degradeClosures === true) {
317322
$itemTypes = [];
318323
$itemTypes[] = new CallableType();
319324
foreach ($this->valueTypes as $valueType) {

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ private static function findTestFiles(): iterable
247247
yield __DIR__ . '/../Rules/Methods/data/bug-4801.php';
248248
yield __DIR__ . '/../Rules/Arrays/data/narrow-superglobal.php';
249249
yield __DIR__ . '/../Rules/Methods/data/bug-12927.php';
250+
yield __DIR__ . '/../Rules/Properties/data/bug-14012.php';
250251
}
251252

252253
/**

‎tests/PHPStan/Analyser/nsrt/degrade-closures.php‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,23 @@
2020
$arr[] = static function () {};
2121
$arr[] = static function () {};
2222
$arr[] = static function () {};
23-
assertType('array{Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void}', $arr);
23+
$arr[] = static function () {};
24+
$arr[] = static function () {};
25+
$arr[] = static function () {};
26+
$arr[] = static function () {};
27+
$arr[] = static function () {};
28+
$arr[] = static function () {};
29+
$arr[] = static function () {};
30+
$arr[] = static function () {};
31+
$arr[] = static function () {};
32+
$arr[] = static function () {};
33+
$arr[] = static function () {};
34+
$arr[] = static function () {};
35+
$arr[] = static function () {};
36+
$arr[] = static function () {};
37+
$arr[] = static function () {};
38+
$arr[] = static function () {};
39+
assertType('array{Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void, Closure(): void}', $arr);
2440

2541
$arr[] = static function () {};
2642
assertType('non-empty-list<callable(): mixed>&oversized-array', $arr);

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,4 +2660,11 @@ public function testBug8936(): void
26602660
$this->analyse([__DIR__ . '/data/bug-8936.php'], []);
26612661
}
26622662

2663+
public function testBug14012(): void
2664+
{
2665+
$this->checkExplicitMixed = true;
2666+
$this->checkImplicitMixed = false;
2667+
$this->analyse([__DIR__ . '/../Properties/data/bug-14012.php'], []);
2668+
}
2669+
26632670
}

‎tests/PHPStan/Rules/Properties/MissingPropertyTypehintRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public function testPromotedProperties(): void
7676
$this->analyse([__DIR__ . '/data/promoted-properties-missing-typehint.php'], []);
7777
}
7878

79+
public function testBug14012(): void
80+
{
81+
$this->analyse([__DIR__ . '/data/bug-14012.php'], []);
82+
}
83+
7984
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Bug14012;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
use function PHPStan\Testing\assertType;
8+
9+
final class ExpectationMethodResolver
10+
{
11+
/**
12+
* @var array{
13+
* hasMethod: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
14+
* hasProperty: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
15+
* isArray: \Closure(Scope, Node\Arg): Node\Expr,
16+
* isBool: \Closure(Scope, Node\Arg): Node\Expr,
17+
* isCallable: \Closure(Scope, Node\Arg): Node\Expr,
18+
* isCountable: \Closure(Scope, Node\Arg): Node\Expr,
19+
* isFalse: \Closure(Scope, Node\Arg): Node\Expr,
20+
* isFloat: \Closure(Scope, Node\Arg): Node\Expr,
21+
* isInstanceOf: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
22+
* isInt: \Closure(Scope, Node\Arg): Node\Expr,
23+
* isIterable: \Closure(Scope, Node\Arg): Node\Expr,
24+
* isList: \Closure(Scope, Node\Arg): Node\Expr,
25+
* isMap: \Closure(Scope, Node\Arg): Node\Expr,
26+
* isNaturalInt: \Closure(Scope, Node\Arg): Node\Expr,
27+
* isNegativeInt: \Closure(Scope, Node\Arg): Node\Expr,
28+
* isNonEmptyString: \Closure(Scope, Node\Arg): Node\Expr,
29+
* isNull: \Closure(Scope, Node\Arg): Node\Expr,
30+
* isNumeric: \Closure(Scope, Node\Arg): Node\Expr,
31+
* isObject: \Closure(Scope, Node\Arg): Node\Expr,
32+
* isPositiveInt: \Closure(Scope, Node\Arg): Node\Expr,
33+
* isResource: \Closure(Scope, Node\Arg): Node\Expr,
34+
* isSameAs: \Closure(Scope, Node\Arg, Node\Arg): Node\Expr,
35+
* isScalar: \Closure(Scope, Node\Arg): Node\Expr,
36+
* isString: \Closure(Scope, Node\Arg): Node\Expr,
37+
* isTrue: \Closure(Scope, Node\Arg): Node\Expr,
38+
* }
39+
*/
40+
public static array $resolvers = [];
41+
}
42+
43+
assertType("array{hasMethod: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg, PhpParser\Node\Arg): PhpParser\Node\Expr, hasProperty: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg, PhpParser\Node\Arg): PhpParser\Node\Expr, isArray: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isBool: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isCallable: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isCountable: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isFalse: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isFloat: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isInstanceOf: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg, PhpParser\Node\Arg): PhpParser\Node\Expr, isInt: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isIterable: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isList: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isMap: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isNaturalInt: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isNegativeInt: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isNonEmptyString: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isNull: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isNumeric: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isObject: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isPositiveInt: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isResource: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isSameAs: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg, PhpParser\Node\Arg): PhpParser\Node\Expr, isScalar: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isString: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr, isTrue: Closure(PHPStan\Analyser\Scope, PhpParser\Node\Arg): PhpParser\Node\Expr}", ExpectationMethodResolver::$resolvers);
44+
45+
/**
46+
* @param callable(Scope, Node\Arg):Node\Expr $callable
47+
*/
48+
function doFoo($callable):void {}
49+
doFoo(ExpectationMethodResolver::$resolvers['isArray']);
50+
51+
/**
52+
* @param \Closure(Scope, Node\Arg):Node\Expr $callable
53+
*/
54+
function doBar($callable):void {}
55+
doBar(ExpectationMethodResolver::$resolvers['isArray']);

0 commit comments

Comments
 (0)