diff --git a/src/Assert.php b/src/Assert.php index 31c861b9..bc9f3afd 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -406,9 +406,8 @@ public static function isInitialized(mixed $value, string $property, string|call { Assert::object($value); - $reflectionProperty = new ReflectionProperty($value, $property); - - if (!$reflectionProperty->isInitialized($value)) { + if (!\property_exists($value, $property) + || !(new ReflectionProperty($value, $property))->isInitialized($value)) { $message = self::resolveMessage($message); static::reportInvalidArgument(\sprintf( $message ?: 'Expected property %s to be initialized.', diff --git a/tests/AssertTest.php b/tests/AssertTest.php index f0ec064d..14ea6b9c 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -165,6 +165,10 @@ public static function getTests(): array public mixed $a; public mixed $b = true; }, 'b'], true], + ['isInitialized', [new class { + public mixed $b = true; + }, 'a'], false], + ['isInitialized', [new ArrayObject(), 'storage'], false], ['isCallable', ['strlen'], true], ['isCallable', [[self::class, 'getTests']], true], ['isCallable', [function () {}], true],