Skip to content

Commit fe67424

Browse files
committed
deprecate handling options in the base Constraint class
1 parent 5e9ea6e commit fe67424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+655
-179
lines changed

UPGRADE-7.4.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,121 @@ HttpClient
2222
----------
2323

2424
* Deprecate using amphp/http-client < 5
25+
26+
Validator
27+
---------
28+
29+
* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint
30+
class instead.
31+
32+
Before:
33+
34+
```php
35+
class CustomConstraint extends Constraint
36+
{
37+
public $option1;
38+
public $option2;
39+
40+
public function __construct(?array $options = null)
41+
{
42+
parent::__construct($options);
43+
}
44+
}
45+
```
46+
47+
After:
48+
49+
```php
50+
class CustomConstraint extends Constraint
51+
{
52+
public $option1;
53+
public $option2;
54+
55+
public function __construct($option1 = null, $option2 = null)
56+
{
57+
parent::__construct();
58+
59+
$this->option1 = $option1;
60+
$this->option2 = $option2;
61+
}
62+
}
63+
```
64+
65+
* Deprecate the `getRequiredOptions()` method of the base `Constraint` class. Use mandatory constructor arguments instead.
66+
67+
Before:
68+
69+
```php
70+
class CustomConstraint extends Constraint
71+
{
72+
public $option1;
73+
public $option2;
74+
75+
public function __construct(?array $options = null)
76+
{
77+
parent::__construct($options);
78+
}
79+
80+
public function getRequiredOptions()
81+
{
82+
return ['option1'];
83+
}
84+
}
85+
```
86+
87+
After:
88+
89+
```php
90+
class CustomConstraint extends Constraint
91+
{
92+
public $option1;
93+
public $option2;
94+
95+
public function __construct($option1, $option2 = null)
96+
{
97+
parent::__construct();
98+
99+
$this->option1 = $option1;
100+
$this->option2 = $option2;
101+
}
102+
}
103+
```
104+
* Deprecate the `normalizeOptions()` and `getDefaultOption()` methods of the base `Constraint` class without replacements.
105+
Overriding them in child constraint will not have any effects starting with Symfony 8.0.
106+
* Deprecate passing an array of options to the `Composite` constraint class. Initialize the properties referenced with `getNestedConstraints()`
107+
in child classes before calling the constructor of `Composite`.
108+
109+
Before:
110+
111+
```php
112+
class CustomCompositeConstraint extends Composite
113+
{
114+
public array $constraints = [];
115+
116+
public function __construct(?array $options = null)
117+
{
118+
parent::__construct($options);
119+
}
120+
121+
protected function getCompositeOption(): string
122+
{
123+
return 'constraints';
124+
}
125+
}
126+
```
127+
128+
After:
129+
130+
```php
131+
class CustomCompositeConstraint extends Composite
132+
{
133+
public array $constraints = [];
134+
135+
public function __construct(array $constraints)
136+
{
137+
$this->constraints = $constraints;
138+
139+
parent::__construct();
140+
}
141+
}
142+
```

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,21 @@ public function __construct(
6666
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
6767

6868
$options = array_merge($fields, $options ?? []);
69+
$fields = null;
6970
} else {
7071
if (\is_array($options)) {
7172
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
73+
74+
$options['fields'] = $fields;
75+
$fields = null;
7276
} else {
73-
$options = [];
77+
$options = null;
7478
}
75-
76-
$options['fields'] = $fields;
7779
}
7880

7981
parent::__construct($options, $groups, $payload);
8082

83+
$this->fields = $fields ?? $this->fields;
8184
$this->message = $message ?? $this->message;
8285
$this->service = $service ?? $this->service;
8386
$this->em = $em ?? $this->em;

src/Symfony/Bridge/Doctrine/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"symfony/translation": "^6.4|^7.0|^8.0",
4242
"symfony/type-info": "^7.1|^8.0",
4343
"symfony/uid": "^6.4|^7.0|^8.0",
44-
"symfony/validator": "^6.4|^7.0|^8.0",
44+
"symfony/validator": "^7.4|^8.0",
4545
"symfony/var-dumper": "^6.4|^7.0|^8.0",
4646
"doctrine/collections": "^1.8|^2.0",
4747
"doctrine/data-fixtures": "^1.1|^2",
@@ -64,7 +64,7 @@
6464
"symfony/property-info": "<6.4",
6565
"symfony/security-bundle": "<6.4",
6666
"symfony/security-core": "<6.4",
67-
"symfony/validator": "<6.4"
67+
"symfony/validator": "<7.4"
6868
},
6969
"autoload": {
7070
"psr-4": { "Symfony\\Bridge\\Doctrine\\": "" },

src/Symfony/Component/Security/Core/Tests/Validator/Constraints/UserPasswordTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public function testValidatedByService(UserPassword $constraint)
3535

3636
public static function provideServiceValidatedConstraints(): iterable
3737
{
38-
yield 'Doctrine style' => [new UserPassword(['service' => 'my_service'])];
39-
4038
yield 'named arguments' => [new UserPassword(service: 'my_service')];
4139

4240
$metadata = new ClassMetadata(UserPasswordDummy::class);
@@ -45,6 +43,14 @@ public static function provideServiceValidatedConstraints(): iterable
4543
yield 'attribute' => [$metadata->properties['b']->constraints[0]];
4644
}
4745

46+
/**
47+
* @group legacy
48+
*/
49+
public function testValidatedByServiceDoctrineStyle()
50+
{
51+
self::assertSame('my_service', (new UserPassword(['service' => 'my_service']))->validatedBy());
52+
}
53+
4854
public function testAttributes()
4955
{
5056
$metadata = new ClassMetadata(UserPasswordDummy::class);

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,124 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint
8+
class instead.
9+
10+
Before:
11+
12+
```php
13+
class CustomConstraint extends Constraint
14+
{
15+
public $option1;
16+
public $option2;
17+
18+
public function __construct(?array $options = null)
19+
{
20+
parent::__construct($options);
21+
}
22+
}
23+
```
24+
25+
After:
26+
27+
```php
28+
class CustomConstraint extends Constraint
29+
{
30+
public $option1;
31+
public $option2;
32+
33+
public function __construct($option1 = null, $option2 = null)
34+
{
35+
parent::__construct();
36+
37+
$this->option1 = $option1;
38+
$this->option2 = $option2;
39+
}
40+
}
41+
```
42+
43+
* Deprecate the `getRequiredOptions()` method of the base `Constraint` class. Use mandatory constructor arguments instead.
44+
45+
Before:
46+
47+
```php
48+
class CustomConstraint extends Constraint
49+
{
50+
public $option1;
51+
public $option2;
52+
53+
public function __construct(?array $options = null)
54+
{
55+
parent::__construct($options);
56+
}
57+
58+
public function getRequiredOptions()
59+
{
60+
return ['option1'];
61+
}
62+
}
63+
```
64+
65+
After:
66+
67+
```php
68+
class CustomConstraint extends Constraint
69+
{
70+
public $option1;
71+
public $option2;
72+
73+
public function __construct($option1, $option2 = null)
74+
{
75+
parent::__construct();
76+
77+
$this->option1 = $option1;
78+
$this->option2 = $option2;
79+
}
80+
}
81+
```
82+
* Deprecate the `normalizeOptions()` and `getDefaultOption()` methods of the base `Constraint` class without replacements.
83+
Overriding them in child constraint will not have any effects starting with Symfony 8.0.
84+
* Deprecate passing an array of options to the `Composite` constraint class. Initialize the properties referenced with `getNestedConstraints()`
85+
in child classes before calling the constructor of `Composite`.
86+
87+
Before:
88+
89+
```php
90+
class CustomCompositeConstraint extends Composite
91+
{
92+
public array $constraints = [];
93+
94+
public function __construct(?array $options = null)
95+
{
96+
parent::__construct($options);
97+
}
98+
99+
protected function getCompositeOption(): string
100+
{
101+
return 'constraints';
102+
}
103+
}
104+
```
105+
106+
After:
107+
108+
```php
109+
class CustomCompositeConstraint extends Composite
110+
{
111+
public array $constraints = [];
112+
113+
public function __construct(array $constraints)
114+
{
115+
$this->constraints = $constraints;
116+
117+
parent::__construct();
118+
}
119+
}
120+
```
121+
4122
7.3
5123
---
6124

src/Symfony/Component/Validator/Constraint.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ public function __construct(mixed $options = null, ?array $groups = null, mixed
110110
{
111111
unset($this->groups); // enable lazy initialization
112112

113+
if (null === $options && (\func_num_args() > 0 || (new \ReflectionMethod($this, 'getRequiredOptions'))->getDeclaringClass()->getName() === self::class)) {
114+
if (null !== $groups) {
115+
$this->groups = $groups;
116+
}
117+
$this->payload = $payload;
118+
119+
return;
120+
}
121+
122+
trigger_deprecation('symfony/validator', '7.4', 'Support for evaluating options in the base Constraint class is deprecated. Initialize properties in the constructor of %s instead.', static::class);
123+
113124
$options = $this->normalizeOptions($options);
114125
if (null !== $groups) {
115126
$options['groups'] = $groups;
@@ -122,6 +133,8 @@ public function __construct(mixed $options = null, ?array $groups = null, mixed
122133
}
123134

124135
/**
136+
* @deprecated since Symfony 7.4
137+
*
125138
* @return array<string, mixed>
126139
*/
127140
protected function normalizeOptions(mixed $options): array
@@ -241,6 +254,8 @@ public function addImplicitGroupName(string $group): void
241254
*
242255
* Override this method to define a default option.
243256
*
257+
* @deprecated since Symfony 7.4
258+
*
244259
* @see __construct()
245260
*/
246261
public function getDefaultOption(): ?string
@@ -255,6 +270,8 @@ public function getDefaultOption(): ?string
255270
*
256271
* @return string[]
257272
*
273+
* @deprecated since Symfony 7.4
274+
*
258275
* @see __construct()
259276
*/
260277
public function getRequiredOptions(): array

src/Symfony/Component/Validator/Constraints/AbstractComparison.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ public function __construct(mixed $value = null, ?string $propertyPath = null, ?
3939
} elseif (null !== $value) {
4040
if (\is_array($options)) {
4141
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);
42-
} else {
43-
$options = [];
44-
}
4542

46-
$options['value'] = $value;
43+
$options['value'] = $value;
44+
}
4745
}
4846

4947
parent::__construct($options, $groups, $payload);
5048

5149
$this->message = $message ?? $this->message;
50+
$this->value = $value ?? $this->value;
5251
$this->propertyPath = $propertyPath ?? $this->propertyPath;
5352

5453
if (null === $this->value && null === $this->propertyPath) {
@@ -64,6 +63,9 @@ public function __construct(mixed $value = null, ?string $propertyPath = null, ?
6463
}
6564
}
6665

66+
/**
67+
* @deprecated since Symfony 7.4
68+
*/
6769
public function getDefaultOption(): ?string
6870
{
6971
return 'value';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy