Skip to content

Commit 9660e6b

Browse files
committed
bug #37392 [Validator] fix handling typed properties as constraint options (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [Validator] fix handling typed properties as constraint options | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37387 | License | MIT | Doc PR | Commits ------- 4a66a60 fix handling typed properties as constraint options
2 parents 40152c3 + 4a66a60 commit 9660e6b

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/Symfony/Component/Validator/Constraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function __construct($options = null)
108108
$defaultOption = $this->getDefaultOption();
109109
$invalidOptions = [];
110110
$missingOptions = array_flip((array) $this->getRequiredOptions());
111-
$knownOptions = get_object_vars($this);
111+
$knownOptions = get_class_vars(static::class);
112112

113113
// The "groups" option is added to the object lazily
114114
$knownOptions['groups'] = true;

src/Symfony/Component/Validator/Tests/ConstraintTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\InvalidOptionsException;
1617
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
1718
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
1819
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
1920
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
21+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithStaticProperty;
22+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithTypedProperty;
2023
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
2124
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;
2225

@@ -245,4 +248,25 @@ public function testAnnotationSetUndefinedDefaultOption()
245248
$this->expectExceptionMessage('No default option is configured for constraint "Symfony\Component\Validator\Tests\Fixtures\ConstraintB".');
246249
new ConstraintB(['value' => 1]);
247250
}
251+
252+
public function testStaticPropertiesAreNoOptions()
253+
{
254+
$this->expectException(InvalidOptionsException::class);
255+
256+
new ConstraintWithStaticProperty([
257+
'foo' => 'bar',
258+
]);
259+
}
260+
261+
/**
262+
* @requires PHP 7.4
263+
*/
264+
public function testSetTypedProperty()
265+
{
266+
$constraint = new ConstraintWithTypedProperty([
267+
'foo' => 'bar',
268+
]);
269+
270+
$this->assertSame('bar', $constraint->foo);
271+
}
248272
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Fixtures;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
class ConstraintWithStaticProperty extends Constraint
8+
{
9+
public static $foo;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\Validator\Tests\Fixtures;
4+
5+
use Symfony\Component\Validator\Constraint;
6+
7+
class ConstraintWithTypedProperty extends Constraint
8+
{
9+
public string $foo;
10+
}

0 commit comments

Comments
 (0)