8000 minor #53444 [Validator] use constructor property promotion in Charse… · symfony/symfony@aa8e74b · GitHub
[go: up one dir, main page]

Skip to content

Commit aa8e74b

Browse files
minor #53444 [Validator] use constructor property promotion in Charset and MacAddress constraints (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Validator] use constructor property promotion in Charset and MacAddress constraints | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 6a34775 use constructor property promotion in Charset and MacAddress constraints
2 parents e4027ef + 6a34775 commit aa8e74b

File tree

7 files changed

+21
-51
lines changed

7 files changed

+21
-51
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ final class Charset extends Constraint
2626
self::BAD_ENCODING_ERROR => 'BAD_ENCODING_ERROR',
2727
];
2828

29-
public array|string $encodings = [];
30-
public string $message = 'The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.';
31-
32-
public function __construct(array|string $encodings = null, string $message = null, array $groups = null, mixed $payload = null, array $options = null)
33-
{
34-
parent::__construct($options, $groups, $payload);
35-
36-
$this->message = $message ?? $this->message;
37-
$this->encodings = (array) ($encodings ?? $this->encodings);
29+
public function __construct(
30+
public array|string $encodings = [],
31+
public string $message = 'The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.',
32+
array $groups = null,
33+
mixed $payload = null,
34+
) {
35+
parent::__construct(null, $groups, $payload);
3836

3937
if ([] === $this->encodings) {
4038
throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires at least one encoding.', static::class));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function validate(mixed $value, Constraint $constraint): void
3535
throw new UnexpectedValueException($value, 'string');
3636
}
3737

38-
if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), $constraint->encodings, true)) {
38+
if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), (array) $constraint->encodings, true)) {
3939
$this->context->buildViolation($constraint->message)
4040
->setParameter('{{ detected }}', $detected)
4141
->setParameter('{{ encodings }}', implode(', ', $constraint->encodings))

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1615

1716
/**
1817
* Validates that a value is a valid MAC address.
@@ -28,25 +27,16 @@ class MacAddress extends Constraint
2827
self::INVALID_MAC_ERROR => 'INVALID_MAC_ERROR',
2928
];
3029

31-
public string $message = 'This is not a valid MAC address.';
32-
33-
/** @var callable|null */
34-
public $normalizer;
30+
public ?\Closure $normalizer;
3531

3632
public function __construct(
37-
array $options = null,
38-
string $message = null,
33+
public string $message = 'This value is not a valid MAC address.',
3934
callable $normalizer = null,
4035
array $groups = null,
4136
mixed $payload = null,
4237
) {
43-
parent::__construct($options, $groups, $payload);
44-
45-
$this->message = $message ?? $this->message;
46-
$this->normalizer = $normalizer ?? $this->normalizer;
38+
parent::__construct(null, $groups, $payload);
4739

48-
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
49-
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer)));
50-
}
40+
$this->normalizer = null !== $normalizer ? $normalizer(...) : null;
5141
}
5242
}

src/Symfony/Component/Validator/Tests/Constraints/CharsetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testSingleEncodingCanBeSet()
2323
{
2424
$encoding = new Charset('UTF-8');
2525

26-
$this->assertSame(['UTF-8'], $encoding->encodings);
26+
$this->assertSame('UTF-8', $encoding->encodings);
2727
}
2828

2929
public function testMultipleEncodingCanBeSet()
@@ -48,7 +48,7 @@ public function testAttributes()
4848
$this->assertTrue($loader->loadClassMetadata($metadata));
4949

5050
[$aConstraint] = $metadata->properties['a']->getConstraints();
51-
$this->assertSame(['UTF-8'], $aConstraint->encodings);
51+
$this->assertSame('UTF-8', $aConstraint->encodings);
5252

5353
[$bConstraint] = $metadata->properties['b']->getConstraints();
5454
$this->assertSame(['ASCII', 'UTF-8'], $bConstraint->encodings);

src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function createValidator(): CharsetValidator
2727
/**
2828
* @dataProvider provideValidValues
2929
*/
30-
public function testEncodingIsValid(string|\Stringable $value, array $encodings)
30+
public function testEncodingIsValid(string|\Stringable $value, array|string $encodings)
3131
{
3232
$this->validator->validate($value, new Charset(encodings: $encodings));
3333

@@ -63,6 +63,7 @@ public static function provideValidValues()
6363
{
6464
yield ['my ascii string', ['ASCII']];
6565
yield ['my ascii string', ['UTF-8']];
66+
yield ['my ascii string', 'UTF-8'];
6667
yield ['my ascii string', ['ASCII', 'UTF-8']];
6768
yield ['my ûtf 8', ['ASCII', 'UTF-8']];
6869
yield ['my ûtf 8', ['UTF-8']];

src/Symfony/Component/Validator/Tests/Constraints/MacAddressTest.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\MacAddress;
16-
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1716
use Symfony\Component\Validator\Mapping\ClassMetadata;
1817
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
1918

@@ -24,23 +23,9 @@ class MacAddressTest extends TestCase
2423
{
2524
public function testNormalizerCanBeSet()
2625
{
27-
$mac = new MacAddress(['normalizer' => 'trim']);
26+
$mac = new MacAddress(normalizer: 'trim');
2827

29-
$this->assertEquals('trim', $mac->normalizer);
30-
}
31-
32-
public function testInvalidNormalizerThrowsException()
33-
{
34-
$this->expectException(InvalidArgumentException::class);
35-
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).');
36-
new MacAddress(['normalizer' => 'Unknown Callable']);
37-
}
38-
39-
public function testInvalidNormalizerObjectThrowsException()
40-
{
41-
$this->expectException(InvalidArgumentException::class);
42-
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
43-
new MacAddress(['normalizer' => new \stdClass()]);
28+
$this->assertEquals(trim(...), $mac->normalizer);
4429
}
4530

4631
public function testAttributes()
@@ -51,7 +36,7 @@ public function testAttributes()
5136

5237
[$aConstraint] = $metadata->properties['a']->getConstraints();
5338
self::assertSame('myMessage', $aConstraint->message);
54-
self::assertSame('trim', $aConstraint->normalizer);
39+
self::assertEquals(trim(...), $aConstraint->normalizer);
5540
self::assertSame(['Default', 'MacAddressDummy'], $aConstraint->groups);
5641

5742
[$bConstraint] = $metadata->properties['b']->getConstraints();

src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public static function getValidMacs(): array
7373
*/
7474
public function testValidMacsWithWhitespaces($mac)
7575
{
76-
$this->validator->validate($mac, new MacAddress([
77-
'normalizer' => 'trim',
78-
]));
76+
$this->validator->validate($mac, new MacAddress(normalizer: 'trim'));
7977

8078
$this->assertNoViolation();
8179
}
@@ -97,9 +95,7 @@ public static function getValidMacsWithWhitespaces(): array
9795
*/
9896
public function testInvalidMacs($mac)
9997
{
100-
$constraint = new MacAddress([
101-
'message' => 'myMessage',
102-
]);
98+
$constraint = new MacAddress('myMessage');
10399

104100
$this->validator->validate($mac, $constraint);
105101

0 commit comments

Comments
 (0)
0