Skip to content

Commit

Permalink
Check if property is initialized before getting its value
Browse files Browse the repository at this point in the history
This commit also removed the use of "setAccessible", since it's not
neccessary after PHP 8.1.

Co-authored-by: Henrique Moody <[email protected]>
  • Loading branch information
gtrbunny and henriquemoody committed Jan 27, 2024
1 parent 6e3ed94 commit 8d7d783
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixes:
- `KeySet` now reports which extra keys are causing the rule to fail.
- Ensure empty strings are never a valid currency code
- Do not hide messages on EachException
- Dot not throw exception when validating an uninitialized property

Changes:

Expand Down
6 changes: 4 additions & 2 deletions library/Rules/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use function property_exists;

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

return $propertyMirror->getValue($input);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/library/Stubs/WithUninitialized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Test\Stubs;

final class WithUninitialized
{
public string $initialized = 'foo';

public string $uninitialized;
}
9 changes: 9 additions & 0 deletions tests/unit/Rules/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Respect\Validation\Test\RuleTestCase;
use Respect\Validation\Test\Stubs\WithProperties;
use Respect\Validation\Test\Stubs\WithUninitialized;

/**
* @group rule
Expand Down Expand Up @@ -47,6 +48,10 @@ public static function providerForValidInput(): array
new Attribute('public', new AlwaysValid()),
new WithProperties(),
],
'attribute is present but uninitialized' => [
new Attribute('uninitialized'),
new WithUninitialized(),
],
'non mandatory attribute is not present' => [
new Attribute('nonexistent', null, false),
new WithProperties(),
Expand All @@ -55,6 +60,10 @@ public static function providerForValidInput(): array
new Attribute('nonexistent', new AlwaysValid(), false),
new WithProperties(),
],
'attribute is present but uninitialized with extra validator' => [
new Attribute('uninitialized', new AlwaysValid()),
new WithUninitialized(),
],
];
}

Expand Down

0 comments on commit 8d7d783

Please sign in to comment.