From 6a347759d7d5d5fb735ac4879bdc31a6662b49a4 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 6 Jan 2024 13:29:55 +0100 Subject: [PATCH] use constructor property promotion in Charset and MacAddress constraints --- .../Validator/Constraints/Charset.php | 16 +++++++------- .../Constraints/CharsetValidator.php | 2 +- .../Validator/Constraints/MacAddress.php | 18 ++++------------ .../Tests/Constraints/CharsetTest.php | 4 ++-- .../Constraints/CharsetValidatorTest.php | 3 ++- .../Tests/Constraints/MacAddressTest.php | 21 +++---------------- .../Constraints/MacAddressValidatorTest.php | 8 ++----- 7 files changed, 21 insertions(+), 51 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Charset.php b/src/Symfony/Component/Validator/Constraints/Charset.php index 37346b92551c2..23d493fb62362 100644 --- a/src/Symfony/Component/Validator/Constraints/Charset.php +++ b/src/Symfony/Component/Validator/Constraints/Charset.php @@ -26,15 +26,13 @@ final class Charset extends Constraint self::BAD_ENCODING_ERROR => 'BAD_ENCODING_ERROR', ]; - public array|string $encodings = []; - public string $message = 'The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.'; - - public function __construct(array|string $encodings = null, string $message = null, array $groups = null, mixed $payload = null, array $options = null) - { - parent::__construct($options, $groups, $payload); - - $this->message = $message ?? $this->message; - $this->encodings = (array) ($encodings ?? $this->encodings); + public function __construct( + public array|string $encodings = [], + public string $message = 'The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.', + array $groups = null, + mixed $payload = null, + ) { + parent::__construct(null, $groups, $payload); if ([] === $this->encodings) { throw new ConstraintDefinitionException(sprintf('The "%s" constraint requires at least one encoding.', static::class)); diff --git a/src/Symfony/Component/Validator/Constraints/CharsetValidator.php b/src/Symfony/Component/Validator/Constraints/CharsetValidator.php index fd0adccfe0cbd..3e5de79d2eb48 100644 --- a/src/Symfony/Component/Validator/Constraints/CharsetValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CharsetValidator.php @@ -35,7 +35,7 @@ public function validate(mixed $value, Constraint $constraint): void throw new UnexpectedValueException($value, 'string'); } - if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), $constraint->encodings, true)) { + if (!\in_array($detected = mb_detect_encoding($value, $constraint->encodings, true), (array) $constraint->encodings, true)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ detected }}', $detected) ->setParameter('{{ encodings }}', implode(', ', $constraint->encodings)) diff --git a/src/Symfony/Component/Validator/Constraints/MacAddress.php b/src/Symfony/Component/Validator/Constraints/MacAddress.php index fda431d792235..474f1d96d41ef 100644 --- a/src/Symfony/Component/Validator/Constraints/MacAddress.php +++ b/src/Symfony/Component/Validator/Constraints/MacAddress.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraint; -use Symfony\Component\Validator\Exception\InvalidArgumentException; /** * Validates that a value is a valid MAC address. @@ -28,25 +27,16 @@ class MacAddress extends Constraint self::INVALID_MAC_ERROR => 'INVALID_MAC_ERROR', ]; - public string $message = 'This is not a valid MAC address.'; - - /** @var callable|null */ - public $normalizer; + public ?\Closure $normalizer; public function __construct( - array $options = null, - string $message = null, + public string $message = 'This value is not a valid MAC address.', callable $normalizer = null, array $groups = null, mixed $payload = null, ) { - parent::__construct($options, $groups, $payload); - - $this->message = $message ?? $this->message; - $this->normalizer = $normalizer ?? $this->normalizer; + parent::__construct(null, $groups, $payload); - if (null !== $this->normalizer && !\is_callable($this->normalizer)) { - throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', get_debug_type($this->normalizer))); - } + $this->normalizer = null !== $normalizer ? $normalizer(...) : null; } } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CharsetTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CharsetTest.php index 893066645a94c..1b23a2ea7911d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CharsetTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CharsetTest.php @@ -23,7 +23,7 @@ public function testSingleEncodingCanBeSet() { $encoding = new Charset('UTF-8'); - $this->assertSame(['UTF-8'], $encoding->encodings); + $this->assertSame('UTF-8', $encoding->encodings); } public function testMultipleEncodingCanBeSet() @@ -48,7 +48,7 @@ public function testAttributes() $this->assertTrue($loader->loadClassMetadata($metadata)); [$aConstraint] = $metadata->properties['a']->getConstraints(); - $this->assertSame(['UTF-8'], $aConstraint->encodings); + $this->assertSame('UTF-8', $aConstraint->encodings); [$bConstraint] = $metadata->properties['b']->getConstraints(); $this->assertSame(['ASCII', 'UTF-8'], $bConstraint->encodings); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php index 4d33080d081be..76470b9370dee 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/CharsetValidatorTest.php @@ -27,7 +27,7 @@ protected function createValidator(): CharsetValidator /** * @dataProvider provideValidValues */ - public function testEncodingIsValid(string|\Stringable $value, array $encodings) + public function testEncodingIsValid(string|\Stringable $value, array|string $encodings) { $this->validator->validate($value, new Charset(encodings: $encodings)); @@ -63,6 +63,7 @@ public static function provideValidValues() { yield ['my ascii string', ['ASCII']]; yield ['my ascii string', ['UTF-8']]; + yield ['my ascii string', 'UTF-8']; yield ['my ascii string', ['ASCII', 'UTF-8']]; yield ['my ûtf 8', ['ASCII', 'UTF-8']]; yield ['my ûtf 8', ['UTF-8']]; diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressTest.php index d39180bc09f74..0669f3ed8f87d 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraints\MacAddress; -use Symfony\Component\Validator\Exception\InvalidArgumentException; use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\Loader\AttributeLoader; @@ -24,23 +23,9 @@ class MacAddressTest extends TestCase { public function testNormalizerCanBeSet() { - $mac = new MacAddress(['normalizer' => 'trim']); + $mac = new MacAddress(normalizer: 'trim'); - $this->assertEquals('trim', $mac->normalizer); - } - - public function testInvalidNormalizerThrowsException() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).'); - new MacAddress(['normalizer' => 'Unknown Callable']); - } - - public function testInvalidNormalizerObjectThrowsException() - { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).'); - new MacAddress(['normalizer' => new \stdClass()]); + $this->assertEquals(trim(...), $mac->normalizer); } public function testAttributes() @@ -51,7 +36,7 @@ public function testAttributes() [$aConstraint] = $metadata->properties['a']->getConstraints(); self::assertSame('myMessage', $aConstraint->message); - self::assertSame('trim', $aConstraint->normalizer); + self::assertEquals(trim(...), $aConstraint->normalizer); self::assertSame(['Default', 'MacAddressDummy'], $aConstraint->groups); [$bConstraint] = $metadata->properties['b']->getConstraints(); diff --git a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php index 0c56bcd23d210..2552d4bc9ba86 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/MacAddressValidatorTest.php @@ -73,9 +73,7 @@ public static function getValidMacs(): array */ public function testValidMacsWithWhitespaces($mac) { - $this->validator->validate($mac, new MacAddress([ - 'normalizer' => 'trim', - ])); + $this->validator->validate($mac, new MacAddress(normalizer: 'trim')); $this->assertNoViolation(); } @@ -97,9 +95,7 @@ public static function getValidMacsWithWhitespaces(): array */ public function testInvalidMacs($mac) { - $constraint = new MacAddress([ - 'message' => 'myMessage', - ]); + $constraint = new MacAddress('myMessage'); $this->validator->validate($mac, $constraint);