From cea134c995af031422d7510327e4cd0bb3340562 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Tue, 23 Dec 2025 15:36:48 +0100 Subject: [PATCH] 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. --- src/Stringifiers/ObjectStringifier.php | 10 +++++++++- tests/fixtures/ParentWithProperties.php | 16 ++++++++++++++++ tests/fixtures/WithProperties.php | 4 +--- .../unit/Stringifiers/ObjectStringifierTest.php | 9 ++++++++- 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/ParentWithProperties.php diff --git a/src/Stringifiers/ObjectStringifier.php b/src/Stringifiers/ObjectStringifier.php index d13a3a1..9c278a9 100644 --- a/src/Stringifiers/ObjectStringifier.php +++ b/src/Stringifiers/ObjectStringifier.php @@ -56,7 +56,15 @@ public function stringify(mixed $raw, int $depth): ?string */ private function getProperties(ReflectionObject $reflectionObject, object $object, int $depth): array { - $reflectionProperties = $reflectionObject->getProperties(); + $reflectionProperties = []; + while ($reflectionObject) { + $reflectionProperties = [ + ...$reflectionProperties, + ...$reflectionObject->getProperties(), + ]; + $reflectionObject = $reflectionObject->getParentClass(); + } + if (count($reflectionProperties) === 0) { return []; } diff --git a/tests/fixtures/ParentWithProperties.php b/tests/fixtures/ParentWithProperties.php new file mode 100644 index 0000000..9d69022 --- /dev/null +++ b/tests/fixtures/ParentWithProperties.php @@ -0,0 +1,16 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace + +abstract class ParentWithProperties +{ + private string $privateProperty = 'something'; // @phpstan-ignore-line +} diff --git a/tests/fixtures/WithProperties.php b/tests/fixtures/WithProperties.php index 773d4a9..8d69804 100644 --- a/tests/fixtures/WithProperties.php +++ b/tests/fixtures/WithProperties.php @@ -10,11 +10,9 @@ // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace -class WithProperties +class WithProperties extends ParentWithProperties { public bool $publicProperty = true; protected int $protectedProperty = 42; - - private string $privateProperty = 'something'; // @phpstan-ignore-line } diff --git a/tests/unit/Stringifiers/ObjectStringifierTest.php b/tests/unit/Stringifiers/ObjectStringifierTest.php index f157173..b3df836 100644 --- a/tests/unit/Stringifiers/ObjectStringifierTest.php +++ b/tests/unit/Stringifiers/ObjectStringifierTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use ReflectionClass; use ReflectionObject; use Respect\Stringifier\Stringifiers\ObjectStringifier; use Respect\Stringifier\Test\Double\FakeQuoter; @@ -22,6 +23,7 @@ use WithProperties; use WithUninitializedProperties; +use function assert; use function sprintf; #[CoversClass(ObjectStringifier::class)] @@ -71,6 +73,8 @@ public function itShouldStringifyRawValueWhenItIsAnObjectWithProperties(): void $sut = new ObjectStringifier($stringifier, $quoter, self::MAXIMUM_DEPTH, self::MAXIMUM_NUMBER_OF_PROPERTIES); $relection = new ReflectionObject($raw); + $parentReflection = $relection->getParentClass(); + assert($parentReflection instanceof ReflectionClass); $actual = $sut->stringify($raw, self::DEPTH); $expected = $quoter->quote( @@ -79,7 +83,10 @@ public function itShouldStringifyRawValueWhenItIsAnObjectWithProperties(): void $relection->getName(), $stringifier->stringify($relection->getProperty('publicProperty')->getValue($raw), self::DEPTH + 1), $stringifier->stringify($relection->getProperty('protectedProperty')->getValue($raw), self::DEPTH + 1), - $stringifier->stringify($relection->getProperty('privateProperty')->getValue($raw), self::DEPTH + 1), + $stringifier->stringify( + $parentReflection->getProperty('privateProperty')->getValue($raw), + self::DEPTH + 1, + ), ), self::DEPTH );