Skip to content

Commit 4a2e2ef

Browse files
committed
Regression test
Closes phpstan/phpstan#6158
1 parent fc5be6a commit 4a2e2ef

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,9 @@ public function testBug5095(): void
8888
]);
8989
}
9090

91+
public function testBug6158(): void
92+
{
93+
$this->analyse([__DIR__ . '/data/bug-6158.php'], []);
94+
}
95+
9196
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Bug6158;
4+
5+
/**
6+
* @template TKey of array-key
7+
* @template T
8+
* @implements ArrayAccess<TKey|null, T>
9+
*/
10+
class Collection implements \ArrayAccess {
11+
12+
/** @var array<TKey|null, T> */
13+
private array $values;
14+
15+
/**
16+
* @param TKey $offset
17+
*/
18+
final public function offsetExists(mixed $offset): bool
19+
{
20+
return array_key_exists($offset, $this->values);
21+
}
22+
23+
/**
24+
* @param TKey $offset
25+
*
26+
* @return T
27+
*/
28+
final public function offsetGet(mixed $offset): mixed
29+
{
30+
return $this->values[$offset];
31+
}
32+
33+
/**
34+
* @param TKey|null $offset
35+
* @param T $value
36+
*/
37+
final public function offsetSet($offset, $value): void
38+
{
39+
$this->values[$offset] = $value;
40+
}
41+
42+
/**
43+
* @param TKey $offset
44+
*/
45+
final public function offsetUnset($offset): void
46+
{
47+
unset($this->values[$offset]);
48+
}
49+
50+
/** @return T|null */
51+
final public function randValue(): mixed
52+
{
53+
if ($this->values === []) {
54+
return null;
55+
}
56+
57+
return $this[array_rand($this->values)];
58+
}
59+
}
60+
61+
final class User {
62+
63+
public UserCollection $users;
64+
65+
public function __construct()
66+
{
67+
$this->users = new UserCollection();
68+
}
69+
70+
public function randValue(): ?User
71+
{
72+
return $this->rand();
73+
}
74+
75+
private function rand(): ?User
76+
{
77+
return $this->users->randValue();
78+
}
79+
80+
}
81+
82+
/**
83+
* @extends Collection<array-key, User>
84+
*/
85+
class UserCollection extends Collection
86+
{
87+
}

0 commit comments

Comments
 (0)