Skip to content

Commit 372c74a

Browse files
Never rely on dynamic properties
1 parent 912720b commit 372c74a

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ CHANGELOG
9696
3.2.0
9797
-----
9898

99-
* deprecated `Tests\Constraints\AbstractContraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
99+
* deprecated `Tests\Constraints\AbstractConstraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
100100
* added support for PHP constants in YAML configuration files
101101

102102
3.1.0

Constraint.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
*
2626
* Constraint instances are immutable and serializable.
2727
*
28-
* @property string[] $groups The groups that the constraint belongs to
29-
*
3028
* @author Bernhard Schussek <[email protected]>
3129
*/
3230
abstract class Constraint
@@ -58,6 +56,13 @@ abstract class Constraint
5856
*/
5957
public $payload;
6058

59+
/**
60+
* The groups that the constraint belongs to.
61+
*
62+
* @var string[]
63+
*/
64+
public $groups;
65+
6166
/**
6267
* Returns the name of the given error code.
6368
*
@@ -105,14 +110,13 @@ public static function getErrorName($errorCode)
105110
*/
106111
public function __construct($options = null)
107112
{
113+
unset($this->groups); // enable lazy initialization
114+
108115
$defaultOption = $this->getDefaultOption();
109116
$invalidOptions = [];
110117
$missingOptions = array_flip((array) $this->getRequiredOptions());
111118
$knownOptions = get_class_vars(static::class);
112119

113-
// The "groups" option is added to the object lazily
114-
$knownOptions['groups'] = true;
115-
116120
if (\is_array($options) && isset($options['value']) && !property_exists($this, 'value')) {
117121
if (null === $defaultOption) {
118122
throw new ConstraintDefinitionException(sprintf('No default option is configured for constraint "%s".', static::class));

Constraints/Composite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct($options = null)
7979
}
8080
}
8181

82-
if (!property_exists($this, 'groups')) {
82+
if (!isset(((array) $this)['groups'])) {
8383
$mergedGroups = [];
8484

8585
foreach ($nestedConstraints as $constraint) {
@@ -96,7 +96,7 @@ public function __construct($options = null)
9696
}
9797

9898
foreach ($nestedConstraints as $constraint) {
99-
if (property_exists($constraint, 'groups')) {
99+
if (isset(((array) $constraint)['groups'])) {
100100
$excessGroups = array_diff($constraint->groups, $this->groups);
101101

102102
if (\count($excessGroups) > 0) {
@@ -141,7 +141,7 @@ abstract protected function getCompositeOption();
141141
*
142142
* @return Constraint[]
143143
*/
144-
public function getNestedContraints()
144+
public function getNestedConstraints()
145145
{
146146
/* @var Constraint[] $nestedConstraints */
147147
return $this->{$this->getCompositeOption()};

Mapping/ClassMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ private function checkConstraint(Constraint $constraint)
490490
}
491491

492492
if ($constraint instanceof Composite) {
493-
foreach ($constraint->getNestedContraints() as $nestedContraint) {
494-
$this->checkConstraint($nestedContraint);
493+
foreach ($constraint->getNestedConstraints() as $nestedConstraint) {
494+
$this->checkConstraint($nestedConstraint);
495495
}
496496
}
497497
}

Mapping/MemberMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ private function checkConstraint(Constraint $constraint)
188188
}
189189

190190
if ($constraint instanceof Composite) {
191-
foreach ($constraint->getNestedContraints() as $nestedContraint) {
192-
$this->checkConstraint($nestedContraint);
191+
foreach ($constraint->getNestedConstraints() as $nestedConstraint) {
192+
$this->checkConstraint($nestedConstraint);
193193
}
194194
}
195195
}

0 commit comments

Comments
 (0)