Skip to content

Commit 6148fb6

Browse files
VincentLangletphpstan-bot
authored andcommitted
Report missing iterable value type for array part of array&callable intersections in MissingTypehintCheck
- In `MissingTypehintCheck::getIterableTypesWithMissingValueTypehint()`, the special-case block for `array&callable` intersection types was skipping the array part entirely, never checking it for missing value types - Added a check for the array inner type's iterable value type before skipping it, so `callable-array`, `callable&array`, and `array&callable(...)` now correctly report `missingType.iterableValue` when the array has no value type - Updated test expectations for `doIntersection()` (gains a third error for the outer array) and `doFoo()` (now reports missing iterable value type) - Added new test case `doBaz()` with explicit `callable&array` parameter - All consumers of `MissingTypehintCheck` (method/function parameters, return types, properties, constants, @var tags, @phpstan-assert) are automatically fixed since they share the same check
1 parent 28f6ffe commit 6148fb6

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/Rules/MissingTypehintCheck.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getIterableTypesWithMissingValueTypehint(Type $type): array
8181
$nonArrayInner = [];
8282
foreach ($type->getTypes() as $innerType) {
8383
if ($innerType->isArray()->yes()) {
84+
$iterableValue = $innerType->getIterableValueType();
85+
if ($iterableValue instanceof MixedType && !$iterableValue->isExplicitMixed()) {
86+
$iterablesWithMissingValueTypehint[] = $innerType;
87+
}
8488
continue;
8589
}
8690
$nonArrayInner[] = $innerType;

tests/PHPStan/Rules/Methods/MissingMethodParameterTypehintRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public function testBug7662(): void
151151
public function testBug14549(): void
152152
{
153153
$this->analyse([__DIR__ . '/data/bug-14549.php'], [
154+
[
155+
'Method Bug14549\Foo::doFoo() has parameter $task with no value type specified in iterable type array.',
156+
12,
157+
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
158+
],
154159
[
155160
'Method Bug14549\Foo::doFoo() has parameter $task with no signature specified for callable.',
156161
12,
@@ -165,6 +170,20 @@ public function testBug14549(): void
165170
46,
166171
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
167172
],
173+
[
174+
'Method Bug14549\Foo::doIntersection() has parameter $array with no value type specified in iterable type array.',
175+
46,
176+
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
177+
],
178+
[
179+
'Method Bug14549\Foo::doBaz() has parameter $task with no value type specified in iterable type array.',
180+
53,
181+
MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP,
182+
],
183+
[
184+
'Method Bug14549\Foo::doBaz() has parameter $task with no signature specified for callable.',
185+
53,
186+
],
168187
]);
169188
}
170189

tests/PHPStan/Rules/Methods/data/bug-14549.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public function doIntersection($array): void
4747
{
4848
}
4949

50+
/**
51+
* @param callable&array $task
52+
*/
53+
public function doBaz(array $task): void
54+
{
55+
}
56+
5057
}
5158

5259

0 commit comments

Comments
 (0)