Skip to content

Commit cea134c

Browse files
committed
Display private properties from parent classes
Right now, when we stringify an object, we do not display the private properties of their parents, because we're only getting the properties of the current object. This commit fixes that behavior, making sure we're displaying all the properties.
1 parent 10e5270 commit cea134c

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

src/Stringifiers/ObjectStringifier.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ public function stringify(mixed $raw, int $depth): ?string
5656
*/
5757
private function getProperties(ReflectionObject $reflectionObject, object $object, int $depth): array
5858
{
59-
$reflectionProperties = $reflectionObject->getProperties();
59+
$reflectionProperties = [];
60+
while ($reflectionObject) {
61+
$reflectionProperties = [
62+
...$reflectionProperties,
63+
...$reflectionObject->getProperties(),
64+
];
65+
$reflectionObject = $reflectionObject->getParentClass();
66+
}
67+
6068
if (count($reflectionProperties) === 0) {
6169
return [];
6270
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
* Copyright (c) Henrique Moody <[email protected]>
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
12+
13+
abstract class ParentWithProperties
14+
{
15+
private string $privateProperty = 'something'; // @phpstan-ignore-line
16+
}

tests/fixtures/WithProperties.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
1212

13-
class WithProperties
13+
class WithProperties extends ParentWithProperties
1414
{
1515
public bool $publicProperty = true;
1616

1717
protected int $protectedProperty = 42;
18-
19-
private string $privateProperty = 'something'; // @phpstan-ignore-line
2018
}

tests/unit/Stringifiers/ObjectStringifierTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPUnit\Framework\Attributes\CoversClass;
1414
use PHPUnit\Framework\Attributes\Test;
1515
use PHPUnit\Framework\TestCase;
16+
use ReflectionClass;
1617
use ReflectionObject;
1718
use Respect\Stringifier\Stringifiers\ObjectStringifier;
1819
use Respect\Stringifier\Test\Double\FakeQuoter;
@@ -22,6 +23,7 @@
2223
use WithProperties;
2324
use WithUninitializedProperties;
2425

26+
use function assert;
2527
use function sprintf;
2628

2729
#[CoversClass(ObjectStringifier::class)]
@@ -71,6 +73,8 @@ public function itShouldStringifyRawValueWhenItIsAnObjectWithProperties(): void
7173
$sut = new ObjectStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_PROPERTIES);
7274

7375
$relection = new ReflectionObject($raw);
76+
$parentReflection = $relection->getParentClass();
77+
assert($parentReflection instanceof ReflectionClass);
7478

7579
$actual = $sut->stringify($raw, self::DEPTH);
7680
$expected = $quoter->quote(
@@ -79,7 +83,10 @@ public function itShouldStringifyRawValueWhenItIsAnObjectWithProperties(): void
7983
$relection->getName(),
8084
$stringifier->stringify($relection->getProperty('publicProperty')->getValue($raw), self::DEPTH + 1),
8185
$stringifier->stringify($relection->getProperty('protectedProperty')->getValue($raw), self::DEPTH + 1),
82-
$stringifier->stringify($relection->getProperty('privateProperty')->getValue($raw), self::DEPTH + 1),
86+
$stringifier->stringify(
87+
$parentReflection->getProperty('privateProperty')->getValue($raw),
88+
self::DEPTH + 1,
89+
),
8390
),
8491
self::DEPTH
8592
);

0 commit comments

Comments
 (0)