diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index fbb1217057..028698e42b 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -29,6 +29,8 @@ use PHPStan\Type\Accessory\NonEmptyArrayType; use PHPStan\Type\ArrayType; use PHPStan\Type\BooleanType; +use PHPStan\Type\CallableType; +use PHPStan\Type\ClosureType; use PHPStan\Type\CompoundType; use PHPStan\Type\ConstantScalarType; use PHPStan\Type\ErrorType; @@ -167,7 +169,26 @@ public function getIterableValueType(): Type return $this->iterableValueType; } - return $this->iterableValueType = count($this->valueTypes) > 0 ? TypeCombinator::union(...$this->valueTypes) : new NeverType(true); + $count = count($this->valueTypes); + if ($count === 0) { + return $this->iterableValueType = new NeverType(true); + } + + if ($count > 16) { + $onlyClosureValues = true; + foreach ($this->valueTypes as $valueType) { + if (!$valueType instanceof ClosureType) { + $onlyClosureValues = false; + break; + } + } + + if ($onlyClosureValues) { + return $this->iterableValueType = new CallableType(); + } + } + + return $this->iterableValueType = TypeCombinator::union(...$this->valueTypes); } public function getKeyType(): Type diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index a3e9095a89..fed5cb2c75 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -1570,6 +1570,12 @@ public function testBug13714(): void $this->assertSame('Function Bug13714\array_find invoked with 2 parameters, 0 required.', $errors[6]->getMessage()); } + public function testBug13933(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/bug-13933.php'); + $this->assertNoErrors($errors); + } + /** * @param string[]|null $allAnalysedFiles * @return list diff --git a/tests/PHPStan/Analyser/data/bug-13933.php b/tests/PHPStan/Analyser/data/bug-13933.php new file mode 100644 index 0000000000..023c61bad3 --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-13933.php @@ -0,0 +1,246 @@ +