Skip to content

Commit 9051e36

Browse files
phpstan-botclaude
authored andcommitted
Add custom generic class test to demonstrate fix is not WeakMap-specific
The fix applies to any generic class whose constructor does not reference its template types (e.g. WeakMap, SplObjectStorage, or custom classes with no-arg constructors). Added CustomGenericCache test cases to prove this. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8439cfe commit 9051e36

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/PHPStan/Analyser/nsrt/bug-11844.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,65 @@ public static function init(): void
107107
{
108108
if (self::$storage === null) {
109109
self::$storage = new \SplObjectStorage();
110+
assertType('SplObjectStorage<object, string>', self::$storage);
111+
}
112+
}
113+
}
114+
115+
/**
116+
* Custom generic class whose constructor does NOT reference template types.
117+
* This proves the fix is general, not WeakMap-specific.
118+
*
119+
* @template TKey of string
120+
* @template TValue
121+
*/
122+
class CustomGenericCache
123+
{
124+
/** @var array<TKey, TValue> */
125+
private array $data = [];
126+
127+
public function __construct()
128+
{
129+
}
130+
131+
/**
132+
* @param TKey $key
133+
* @param TValue $value
134+
*/
135+
public function set(string $key, mixed $value): void
136+
{
137+
$this->data[$key] = $value;
138+
}
139+
}
140+
141+
class CustomGenericPropertyCase
142+
{
143+
/**
144+
* @var CustomGenericCache<string, int>|null
145+
*/
146+
private ?CustomGenericCache $cache = null;
147+
148+
public function init(): void
149+
{
150+
if ($this->cache === null) {
151+
$this->cache = new CustomGenericCache();
152+
assertType('Bug11844\CustomGenericCache<string, int>', $this->cache);
153+
}
154+
}
155+
}
156+
157+
class StaticCustomGenericPropertyCase
158+
{
159+
/**
160+
* @var CustomGenericCache<string, int>|null
161+
*/
162+
private static ?CustomGenericCache $cache = null;
163+
164+
public static function init(): void
165+
{
166+
if (self::$cache === null) {
167+
self::$cache = new CustomGenericCache();
168+
assertType('Bug11844\CustomGenericCache<string, int>', self::$cache);
110169
}
111170
}
112171
}

0 commit comments

Comments
 (0)