Skip to content

Commit ae0ace2

Browse files
committed
Add tests for child class overriding visibility and __get-only
Addresses review: when a child class overrides a protected/private property to public, array_column correctly picks it up when the static type is the child class. Also covers __get-only (no __isset) returning array{}.
1 parent 4252b95 commit ae0ace2

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

tests/PHPStan/Analyser/nsrt/array-column-php82.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,25 @@ public function testNonPublicAsIndex(array $objects): void
273273

274274
}
275275

276+
class ChildWithPublicOverrides extends ObjectWithVisibility
277+
{
278+
public int $prot = 2;
279+
public int $priv = 3;
280+
}
281+
282+
class ArrayColumnVisibilityWithChildOverrideTest
283+
{
284+
285+
/** @param array<int, ChildWithPublicOverrides> $objects */
286+
public function testChildOverride(array $objects): void
287+
{
288+
assertType('list<int>', array_column($objects, 'pub'));
289+
assertType('list<int>', array_column($objects, 'prot'));
290+
assertType('list<int>', array_column($objects, 'priv'));
291+
}
292+
293+
}
294+
276295
class ArrayColumnVisibilityFromInsideTest
277296
{
278297

@@ -311,7 +330,17 @@ public function __isset(string $name): bool
311330
}
312331
}
313332

314-
class ArrayColumnVisibilityWithIssetOnlyTest
333+
class ObjectWithGetOnly
334+
{
335+
private int $priv = 2;
336+
337+
public function __get(string $name): mixed
338+
{
339+
return $this->$name;
340+
}
341+
}
342+
343+
class ArrayColumnVisibilityWithMagicMethodsTest
315344
{
316345

317346
/** @param array<int, ObjectWithIssetOnly> $objects */
@@ -320,6 +349,12 @@ public function testWithIssetOnly(array $objects): void
320349
assertType('array{}', array_column($objects, 'priv'));
321350
}
322351

352+
/** @param array<int, ObjectWithGetOnly> $objects */
353+
public function testWithGetOnly(array $objects): void
354+
{
355+
assertType('array{}', array_column($objects, 'priv'));
356+
}
357+
323358
}
324359

325360
class ObjectWithIsset

tests/PHPStan/Analyser/nsrt/array-column.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ public function testNonPublicAsIndex(array $objects): void
288288

289289
}
290290

291+
class ChildWithPublicOverrides extends ObjectWithVisibility
292+
{
293+
public int $prot = 2;
294+
public int $priv = 3;
295+
}
296+
297+
class ArrayColumnVisibilityWithChildOverrideTest
298+
{
299+
300+
/** @param array<int, ChildWithPublicOverrides> $objects */
301+
public function testChildOverride(array $objects): void
302+
{
303+
assertType('list<int>', array_column($objects, 'pub'));
304+
assertType('list<int>', array_column($objects, 'prot'));
305+
assertType('list<int>', array_column($objects, 'priv'));
306+
}
307+
308+
}
309+
291310
class ArrayColumnVisibilityFromInsideTest
292311
{
293312

@@ -326,7 +345,17 @@ public function __isset(string $name): bool
326345
}
327346
}
328347

329-
class ArrayColumnVisibilityWithIssetOnlyTest
348+
class ObjectWithGetOnly
349+
{
350+
private int $priv = 2;
351+
352+
public function __get(string $name): mixed
353+
{
354+
return $this->$name;
355+
}
356+
}
357+
358+
class ArrayColumnVisibilityWithMagicMethodsTest
330359
{
331360

332361
/** @param array<int, ObjectWithIssetOnly> $objects */
@@ -335,6 +364,12 @@ public function testWithIssetOnly(array $objects): void
335364
assertType('array{}', array_column($objects, 'priv'));
336365
}
337366

367+
/** @param array<int, ObjectWithGetOnly> $objects */
368+
public function testWithGetOnly(array $objects): void
369+
{
370+
assertType('array{}', array_column($objects, 'priv'));
371+
}
372+
338373
}
339374

340375
class ObjectWithIsset

0 commit comments

Comments
 (0)