Skip to content

Commit

Permalink
deprecate the use of option arrays to configure validation constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Apr 26, 2024
1 parent 0f6b3ef commit 0c07d99
Show file tree
Hide file tree
Showing 151 changed files with 2,180 additions and 1,707 deletions.
Expand Up @@ -59,6 +59,8 @@ public function __construct(
array $options = [],
) {
if (\is_array($fields) && \is_string(key($fields))) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($fields, $options);
} elseif (null !== $fields) {
$options['fields'] = $fields;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Expand Up @@ -90,7 +90,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
}

if (true === (self::getFieldMappingValue($mapping, 'unique') ?? false) && !isset($existingUniqueFields[self::getFieldMappingValue($mapping, 'fieldName')])) {
$metadata->addConstraint(new UniqueEntity(['fields' => self::getFieldMappingValue($mapping, 'fieldName')]));
$metadata->addConstraint(new UniqueEntity(fields: self::getFieldMappingValue($mapping, 'fieldName')));
$loaded = true;
}

Expand All @@ -103,7 +103,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'declaredField'), new Valid());
$loaded = true;
} elseif (property_exists($className, self::getFieldMappingValue($mapping, 'fieldName')) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty(self::getFieldMappingValue($mapping, 'fieldName'))->isPrivate())) {
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(['max' => self::getFieldMappingValue($mapping, 'length')]));
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(max: self::getFieldMappingValue($mapping, 'length')));
$loaded = true;
}
} elseif (null === $lengthConstraint->max) {
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\LogicException;
Expand All @@ -28,9 +29,12 @@ abstract class AbstractComparison extends Constraint
public mixed $value = null;
public ?string $propertyPath = null;

#[HasNamedArguments]
public function __construct(mixed $value = null, ?string $propertyPath = null, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = [])
{
if (\is_array($value)) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($value, $options);
} elseif (null !== $value) {
$options['value'] = $value;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Bic.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Intl\Countries;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\LogicException;
Expand Down Expand Up @@ -56,12 +57,17 @@ class Bic extends Constraint
* @param string|null $ibanPropertyPath Property path to the IBAN value when validating objects
* @param string[]|null $groups
*/
#[HasNamedArguments]
public function __construct(?array $options = null, ?string $message = null, ?string $iban = null, ?string $ibanPropertyPath = null, ?string $ibanMessage = null, ?array $groups = null, mixed $payload = null)
{
if (!class_exists(Countries::class)) {
throw new LogicException('The Intl component is required to use the Bic constraint. Try running "composer require symfony/intl".');
}

if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

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

$this->message = $message ?? $this->message;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Blank.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand All @@ -33,8 +34,13 @@ class Blank extends Constraint
* @param array<string,mixed>|null $options
* @param string[]|null $groups
*/
#[HasNamedArguments]
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null)
{
if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

parent::__construct($options ?? [], $groups, $payload);

$this->message = $message ?? $this->message;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Callback.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand All @@ -30,16 +31,21 @@ class Callback extends Constraint
* @param string|string[]|callable|array<string,mixed>|null $callback The callback definition
* @param string[]|null $groups
*/
#[HasNamedArguments]
public function __construct(array|string|callable|null $callback = null, ?array $groups = null, mixed $payload = null, array $options = [])
{
// Invocation through attributes with an array parameter only
if (\is_array($callback) && 1 === \count($callback) && isset($callback['value'])) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$callback = $callback['value'];
}

if (!\is_array($callback) || (!isset($callback['callback']) && !isset($callback['groups']) && !isset($callback['payload']))) {
$options['callback'] = $callback;
} else {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($callback, $options);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/CardScheme.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand Down Expand Up @@ -51,9 +52,12 @@ class CardScheme extends Constraint
* @param string[]|null $groups
* @param array<string,mixed> $options
*/
#[HasNamedArguments]
public function __construct(array|string|null $schemes, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = [])
{
if (\is_array($schemes) && \is_string(key($schemes))) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($schemes, $options);
} else {
$options['value'] = $schemes;
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Cascade.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

Expand All @@ -28,9 +29,12 @@ class Cascade extends Constraint
* @param string[]|string|array<string,mixed>|null $exclude Properties excluded from validation
* @param array<string,mixed>|null $options
*/
#[HasNamedArguments]
public function __construct(array|string|null $exclude = null, ?array $options = null)
{
if (\is_array($exclude) && !array_is_list($exclude)) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($exclude, $options ?? []);
} else {
$this->exclude = array_flip((array) $exclude);
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Choice.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand Down Expand Up @@ -59,6 +60,7 @@ public function getDefaultOption(): ?string
* @param string[]|null $groups
* @param bool|null $match Whether to validate the values are part of the choices or not (defaults to true)
*/
#[HasNamedArguments]
public function __construct(
string|array $options = [],
?array $choices = null,
Expand All @@ -78,7 +80,10 @@ public function __construct(
if (\is_array($options) && $options && array_is_list($options)) {
$choices ??= $options;
$options = [];
} elseif (\is_array($options) && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

if (null !== $choices) {
$options['value'] = $choices;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Cidr.php
Expand Up @@ -84,6 +84,10 @@ public function __construct(
$payload = null,
?callable $normalizer = null,
) {
if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

$this->version = $version ?? $options['version'] ?? $this->version;

if (!\array_key_exists($this->version, self::NET_MAXES)) {
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand Down Expand Up @@ -41,10 +42,13 @@ class Collection extends Composite
* @param bool|null $allowExtraFields Whether to allow additional keys not declared in the configured fields (defaults to false)
* @param bool|null $allowMissingFields Whether to allow the collection to lack some fields declared in the configured fields (defaults to false)
*/
#[HasNamedArguments]
public function __construct(mixed $fields = null, ?array $groups = null, mixed $payload = null, ?bool $allowExtraFields = null, ?bool $allowMissingFields = null, ?string $extraFieldsMessage = null, ?string $missingFieldsMessage = null)
{
if (self::isFieldsOption($fields)) {
$fields = ['fields' => $fields];
} else {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

parent::__construct($fields, $groups, $payload);
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Count.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\MissingOptionsException;

Expand Down Expand Up @@ -50,6 +51,7 @@ class Count extends Constraint
* @param string[]|null $groups
* @param array<mixed,string> $options
*/
#[HasNamedArguments]
public function __construct(
int|array|null $exactly = null,
?int $min = null,
Expand All @@ -64,6 +66,8 @@ public function __construct(
array $options = [],
) {
if (\is_array($exactly)) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($exactly, $options);
$exactly = $options['value'] ?? null;
}
Expand Down
Expand Up @@ -70,10 +70,10 @@ public function validate(mixed $value, Constraint $constraint): void
->getValidator()
->inContext($this->context)
->validate($count, [
new DivisibleBy([
'value' => $constraint->divisibleBy,
'message' => $constraint->divisibleByMessage,
]),
new DivisibleBy(
value: $constraint->divisibleBy,
message: $constraint->divisibleByMessage,
),
], $this->context->getGroup());
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Country.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Countries;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

Expand Down Expand Up @@ -41,6 +42,7 @@ class Country extends Constraint
*
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3#Current_codes
*/
#[HasNamedArguments]
public function __construct(
?array $options = null,
?string $message = null,
Expand All @@ -52,6 +54,10 @@ public function __construct(
throw new LogicException('The Intl component is required to use the Country constraint. Try running "composer require symfony/intl".');
}

if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

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

$this->message = $message ?? $this->message;
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/CssColor.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\InvalidArgumentException;

Expand Down Expand Up @@ -66,13 +67,16 @@ class CssColor extends Constraint
* @param string[]|null $groups
* @param array<string,mixed>|null $options
*/
#[HasNamedArguments]
public function __construct(array|string $formats = [], ?string $message = null, ?array $groups = null, $payload = null, ?array $options = null)
{
$validationModesAsString = implode(', ', self::$validationModes);

if (!$formats) {
$options['value'] = self::$validationModes;
} elseif (\is_array($formats) && \is_string(key($formats))) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($formats, $options ?? []);
} elseif (\is_array($formats)) {
if ([] === array_intersect(self::$validationModes, $formats)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Currency.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Currencies;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\LogicException;

Expand All @@ -38,12 +39,17 @@ class Currency extends Constraint
* @param array<string,mixed>|null $options
* @param string[]|null $groups
*/
#[HasNamedArguments]
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null)
{
if (!class_exists(Currencies::class)) {
throw new LogicException('The Intl component is required to use the Currency constraint. Try running "composer require symfony/intl".');
}

if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

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

$this->message = $message ?? $this->message;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Date.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand All @@ -37,8 +38,13 @@ class Date extends Constraint
* @param array<string,mixed>|null $options
* @param string[]|null $groups
*/
#[HasNamedArguments]
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null)
{
if (null !== $options && [] !== $options) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);
}

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

$this->message = $message ?? $this->message;
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

/**
Expand Down Expand Up @@ -41,9 +42,12 @@ class DateTime extends Constraint
* @param string[]|null $groups
* @param array<string,mixed> $options
*/
#[HasNamedArguments]
public function __construct(string|array|null $format = null, ?string $message = null, ?array $groups = null, mixed $payload = null, array $options = [])
{
if (\is_array($format)) {
trigger_deprecation('symfony/validator', '7.2', 'Passing an array of options to configure the %s constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($format, $options);
} elseif (null !== $format) {
$options['value'] = $format;
Expand Down

0 comments on commit 0c07d99

Please sign in to comment.