Skip to content

Commit d281214

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Fix checking list return type
1 parent 5ef8716 commit d281214

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/Rules/RuleLevelHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function accepts(Type $acceptingType, Type $acceptedType, bool $strictTyp
111111
$acceptedType->isArray()->yes()
112112
&& $acceptingType->isArray()->yes()
113113
&& !$acceptingType->isIterableAtLeastOnce()->yes()
114+
&& !$acceptingType->isList()->yes()
114115
&& $acceptedType->isConstantArray()->no()
115116
&& $acceptingType->isConstantArray()->no()
116117
) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ public function dataFileAsserts(): iterable
10811081
yield from $this->gatherAssertTypes(__DIR__ . '/data/allowed-subtypes-throwable.php');
10821082
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_values.php');
10831083
yield from $this->gatherAssertTypes(__DIR__ . '/data/array_keys.php');
1084+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-8174.php');
10841085
}
10851086

10861087
/**

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,4 +775,15 @@ public function testBug3499(): void
775775
$this->analyse([__DIR__ . '/data/bug-3499.php'], []);
776776
}
777777

778+
public function testBug8174(): void
779+
{
780+
$this->checkExplicitMixed = true;
781+
$this->analyse([__DIR__ . '/data/bug-8174.php'], [
782+
[
783+
"Method Bug8174\HelloWorld::filterList() should return list<string> but returns array<int<0, max>, '23423'>.",
784+
21,
785+
],
786+
]);
787+
}
788+
778789
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug8174;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param list<string> $list
11+
* @return list<string>
12+
*/
13+
public function filterList(array $list): array {
14+
$filtered = array_filter($list, function ($elem) {
15+
return $elem === '23423';
16+
});
17+
assertType("array<int<0, max>, '23423'>", $filtered); // this is not a list
18+
assertType("list<'23423'>", array_values($filtered)); // this is a list
19+
20+
// why am I allowed to return not a list then?
21+
return $filtered;
22+
}
23+
}

0 commit comments

Comments
 (0)