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 );