Skip to content

Commit 5e973a2

Browse files
committed
array_column: proper support for private properties
1 parent bf9d323 commit 5e973a2

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Type/Php/ArrayColumnHelper.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,15 @@ private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $
147147
continue;
148148
}
149149

150-
$returnTypes[] = $type->getInstanceProperty($propertyName, $scope)->getReadableType();
150+
$property = $type->getInstanceProperty($propertyName, $scope);
151+
if (!$property->isPublic()) {
152+
if (!$type->hasMethod('__isset')->no() && !$type->hasMethod('__get')->no()) {
153+
$returnTypes[] = new MixedType();
154+
}
155+
continue;
156+
}
157+
158+
$returnTypes[] = $property->getReadableType();
151159
}
152160
}
153161

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,69 @@ public function testObjects2(array $array): void
244244
final class Foo
245245
{
246246

247+
public int $publicProperty;
248+
protected int $protectedProperty;
249+
private int $privateProperty;
250+
251+
/** @param array<int, self> $a */
252+
public function doFoo(array $a): void
253+
{
254+
assertType('array{}', array_column($a, 'nodeName'));
255+
assertType('array{}', array_column($a, 'nodeName', 'tagName'));
256+
assertType('list<int>', array_column($a, 'publicProperty'));
257+
assertType('array{}', array_column($a, 'protectedProperty'));
258+
assertType('array{}', array_column($a, 'privateProperty'));
259+
}
260+
261+
}
262+
263+
264+
class FooNotFinal
265+
{
266+
267+
public int $publicProperty;
268+
protected int $protectedProperty;
269+
private int $privateProperty;
270+
271+
/** @param array<int, self> $a */
272+
public function doFoo(array $a): void
273+
{
274+
assertType('list', array_column($a, 'nodeName'));
275+
assertType('array', array_column($a, 'nodeName', 'tagName'));
276+
assertType('list<int>', array_column($a, 'publicProperty'));
277+
assertType('list', array_column($a, 'protectedProperty'));
278+
assertType('list', array_column($a, 'privateProperty'));
279+
}
280+
281+
}
282+
283+
284+
final class Magic
285+
{
286+
287+
public int $publicProperty;
288+
protected int $protectedProperty;
289+
private int $privateProperty;
290+
291+
public function __get($prop)
292+
{
293+
return $this->$prop;
294+
}
295+
296+
public function __isset($prop) : bool
297+
{
298+
return isset($this->$prop);
299+
}
300+
247301
/** @param array<int, self> $a */
248302
public function doFoo(array $a): void
249303
{
250304
assertType('list', array_column($a, 'nodeName'));
251305
assertType('array', array_column($a, 'nodeName', 'tagName'));
306+
assertType('list<int>', array_column($a, 'publicProperty'));
307+
assertType('list', array_column($a, 'protectedProperty'));
308+
assertType('list', array_column($a, 'privateProperty'));
252309
}
253310

254311
}
312+

0 commit comments

Comments
 (0)