Skip to content

Commit 93f509e

Browse files
committed
Regression test
Closes #3151
1 parent 346444a commit 93f509e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,9 @@ public function testBug4603(): void
507507
$this->analyse([__DIR__ . '/data/bug-4603.php'], []);
508508
}
509509

510+
public function testBug3151(): void
511+
{
512+
$this->analyse([__DIR__ . '/data/bug-3151.php'], []);
513+
}
514+
510515
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug3151;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @template T
10+
* @param T[] $haystack
11+
* @param callable(T): bool $matcher
12+
* @return T|null;
13+
*/
14+
function search(array $haystack, callable $matcher)
15+
{
16+
foreach ($haystack as $item) {
17+
if (!$matcher($item)) {
18+
continue;
19+
}
20+
return $item;
21+
}
22+
return null;
23+
}
24+
25+
/**
26+
* @param array<array{type: string, foo: string}> $myArray
27+
* @return array{type: string, foo: string}
28+
*/
29+
function findByType(array $myArray, string $type): array {
30+
$found = $this->search($myArray, function(array $item) use ($type): bool {
31+
return $item['type'] === $type;
32+
});
33+
if ($found === null) {
34+
throw new \LogicException();
35+
}
36+
return $found;
37+
}
38+
39+
}

0 commit comments

Comments
 (0)