Skip to content

Commit b24fbd2

Browse files
committed
Refactors constraints to use named arguments
Adopts named arguments for Symfony Validator constraint instantiation within field providers and their corresponding tests. This improves code readability and explicitness by clearly labeling each argument's purpose, making the constraint configuration easier to understand.
1 parent ce186e3 commit b24fbd2

20 files changed

+64
-88
lines changed

src/Validator/Constraints/Email.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@ class Email extends _Email
2121
public bool $checkDNS = false;
2222

2323
public function __construct(
24-
?array $options = null,
2524
?string $message = null,
2625
?string $mode = null,
2726
?callable $normalizer = null,
2827
?array $groups = null,
2928
mixed $payload = null,
29+
?string $tldMessage = null,
30+
?string $dnsMessage = null,
31+
?bool $checkDNS = null,
3032
) {
31-
parent::__construct($options, $message, $mode, $normalizer, $groups, $payload);
33+
parent::__construct(message: $message, mode: $mode, normalizer: $normalizer, groups: $groups, payload: $payload);
3234
$this->mode ??= 'strict';
35+
if (null !== $tldMessage) {
36+
$this->tldMessage = $tldMessage;
37+
}
38+
if (null !== $dnsMessage) {
39+
$this->dnsMessage = $dnsMessage;
40+
}
41+
if (null !== $checkDNS) {
42+
$this->checkDNS = $checkDNS;
43+
}
3344
}
3445
}

src/Validator/Constraints/EntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getConstraints(string $class, string $field): array
131131
}
132132
} elseif (($fieldMapping['type'] & ClassMetadataInfo::TO_MANY) !== 0) {
133133
// ToMany
134-
$constraints[] = new All([
134+
$constraints[] = new All(constraints: [
135135
new Type($fieldMapping['targetEntity']),
136136
]);
137137
} else {

src/Validator/Constraints/FloatScale.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,12 @@ class FloatScale extends Constraint
1818

1919
public int $scale;
2020

21-
/**
22-
* @param int|array<mixed> $scale
23-
* @param array<mixed> $options
24-
*/
2521
public function __construct(
26-
$scale,
22+
int $scale,
2723
?array $groups = null,
2824
mixed $payload = null,
29-
array $options = []
3025
) {
31-
if (\is_array($scale)) {
32-
$options = array_merge($scale, $options);
33-
} elseif (null !== $scale) {
34-
$options['value'] = $scale;
35-
}
36-
37-
parent::__construct($options, $groups, $payload);
38-
}
39-
40-
public function getDefaultOption(): string
41-
{
42-
return 'scale';
43-
}
44-
45-
public function getRequiredOptions(): array
46-
{
47-
return ['scale'];
26+
$this->scale = $scale;
27+
parent::__construct(groups: $groups, payload: $payload);
4828
}
4929
}

src/Validator/Constraints/Money.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@ class Money extends Constraint
1616

1717
public float $min = 0.0;
1818
public float $max = self::MAX;
19+
20+
public function __construct(
21+
float $min = 0.0,
22+
float $max = self::MAX,
23+
?array $groups = null,
24+
mixed $payload = null,
25+
) {
26+
parent::__construct(groups: $groups, payload: $payload);
27+
$this->min = $min;
28+
$this->max = $max;
29+
}
1930
}

src/Validator/Constraints/Postal.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,13 @@ class Postal extends Constraint
1616

1717
public string $countryPropertyPath;
1818

19-
/**
20-
* @param string|array<mixed> $countryPropertyPath
21-
* @param array<mixed> $options
22-
*/
2319
public function __construct(
24-
$countryPropertyPath,
20+
string $countryPropertyPath,
2521
?array $groups = null,
2622
mixed $payload = null,
27-
array $options = []
2823
) {
29-
if (\is_array($countryPropertyPath)) {
30-
$options = array_merge($countryPropertyPath, $options);
31-
} elseif (null !== $countryPropertyPath) {
32-
$options['value'] = $countryPropertyPath;
33-
}
34-
35-
parent::__construct($options, $groups, $payload);
36-
}
37-
38-
public function getDefaultOption(): string
39-
{
40-
return 'countryPropertyPath';
41-
}
42-
43-
public function getRequiredOptions(): array
44-
{
45-
return ['countryPropertyPath'];
24+
$this->countryPropertyPath = $countryPropertyPath;
25+
parent::__construct(groups: $groups, payload: $payload);
4626
}
4727

4828
public string $message = 'The value {{ value }} is not a valid postal code.';

src/Validator/ConstraintsSetProvider/Field/EmailProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function getConstraints(array $fieldMapping): array
1919
{
2020
return [
2121
new Email(),
22-
new Length(['max' => ($fieldMapping['length'] ?? 255)]),
22+
new Length(max: $fieldMapping['length'] ?? 255),
2323
];
2424
}
2525
}

src/Validator/ConstraintsSetProvider/Field/IpProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function supports(string $type): bool
1717
public function getConstraints(array $fieldMapping): array
1818
{
1919
return [
20-
new Ip(['version' => 'all']),
20+
new Ip(version: 'all'),
2121
];
2222
}
2323
}

src/Validator/ConstraintsSetProvider/Field/LocaleProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function supports(string $type): bool
1717
public function getConstraints(array $fieldMapping): array
1818
{
1919
return [
20-
new Locale(['canonicalize' => true]),
20+
new Locale(canonicalize: true),
2121
];
2222
}
2323
}

src/Validator/ConstraintsSetProvider/Field/StringProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public function supports(string $type): bool
1717

1818
public function getConstraints(array $fieldMapping): array
1919
{
20-
$constraints = [new Length(['max' => $fieldMapping['length'] ?? 255])];
20+
$constraints = [new Length(max: $fieldMapping['length'] ?? 255)];
2121

2222
if (isset($fieldMapping['nullable']) && true === $fieldMapping['nullable']) {
23-
$constraints[] = new NotBlank(['allowNull' => true]);
23+
$constraints[] = new NotBlank(allowNull: true);
2424
}
2525

2626
return $constraints;

src/Validator/ConstraintsSetProvider/Field/TextProvider.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public function supports(string $type): bool
1717
public function getConstraints(array $fieldMapping): array
1818
{
1919
return [
20-
new Length([
21-
'max' => $fieldMapping['length'] ?? 65535,
22-
'charset' => '8bit',
23-
]),
20+
new Length(max: $fieldMapping['length'] ?? 65535, charset: '8bit'),
2421
];
2522
}
2623
}

0 commit comments

Comments
 (0)