Skip to content

Commit 8d7d783

Browse files
Check if property is initialized before getting its value
This commit also removed the use of "setAccessible", since it's not neccessary after PHP 8.1. Co-authored-by: Henrique Moody <[email protected]>
1 parent 6e3ed94 commit 8d7d783

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixes:
1717
- `KeySet` now reports which extra keys are causing the rule to fail.
1818
- Ensure empty strings are never a valid currency code
1919
- Do not hide messages on EachException
20+
- Dot not throw exception when validating an uninitialized property
2021

2122
Changes:
2223

library/Rules/Attribute.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use function property_exists;
1818

1919
/**
20-
* Validates an object attribute, event private ones.
20+
* Validates an object attribute, even private ones.
2121
*
2222
* @author Alexandre Gomes Gaigalas <[email protected]>
2323
* @author Emmerson Siqueira <[email protected]>
@@ -38,7 +38,9 @@ public function __construct(string $reference, ?Validatable $rule = null, bool $
3838
public function getReferenceValue($input)
3939
{
4040
$propertyMirror = new ReflectionProperty($input, (string) $this->getReference());
41-
$propertyMirror->setAccessible(true);
41+
if ($propertyMirror->isInitialized($input) === false) {
42+
return null;
43+
}
4244

4345
return $propertyMirror->getValue($input);
4446
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Respect\Validation\Test\Stubs;
11+
12+
final class WithUninitialized
13+
{
14+
public string $initialized = 'foo';
15+
16+
public string $uninitialized;
17+
}

tests/unit/Rules/AttributeTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Respect\Validation\Test\RuleTestCase;
1313
use Respect\Validation\Test\Stubs\WithProperties;
14+
use Respect\Validation\Test\Stubs\WithUninitialized;
1415

1516
/**
1617
* @group rule
@@ -47,6 +48,10 @@ public static function providerForValidInput(): array
4748
new Attribute('public', new AlwaysValid()),
4849
new WithProperties(),
4950
],
51+
'attribute is present but uninitialized' => [
52+
new Attribute('uninitialized'),
53+
new WithUninitialized(),
54+
],
5055
'non mandatory attribute is not present' => [
5156
new Attribute('nonexistent', null, false),
5257
new WithProperties(),
@@ -55,6 +60,10 @@ public static function providerForValidInput(): array
5560
new Attribute('nonexistent', new AlwaysValid(), false),
5661
new WithProperties(),
5762
],
63+
'attribute is present but uninitialized with extra validator' => [
64+
new Attribute('uninitialized', new AlwaysValid()),
65+
new WithUninitialized(),
66+
],
5867
];
5968
}
6069

0 commit comments

Comments
 (0)