Skip to content

[Validator] Remove deprecated code #41392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ Validator
After:

```php
$builder->enableAnnotationMapping(true)
$builder
->enableAnnotationMapping()
->setDoctrineAnnotationReader($reader);
```

Expand All @@ -399,7 +400,8 @@ Validator
After:

```php
$builder->enableAnnotationMapping(true)
$builder
->enableAnnotationMapping()
->addDefaultDoctrineAnnotationReader();
```

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGELOG
=========

6.0
---

* Remove the `allowEmptyString` option from the `Length` constraint
* Remove the `NumberConstraintTrait` trait
* `ValidatorBuilder::enableAnnotationMapping()` does not accept a Doctrine annotation reader anymore
* `ValidatorBuilder::enableAnnotationMapping()` won't automatically setup a Doctrine annotation reader anymore

5.3
---
* Add the `normalizer` option to the `Unique` constraint
Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Component/Validator/Constraints/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Length extends Constraint
public $min;
public $charset = 'UTF-8';
public $normalizer;
public $allowEmptyString = false;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -95,9 +94,5 @@ public function __construct(
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)));
}

if (isset($options['allowEmptyString'])) {
trigger_deprecation('symfony/validator', '5.2', sprintf('The "allowEmptyString" option of the "%s" constraint is deprecated.', self::class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, Length::class);
}

if (null === $value || ('' === $value && $constraint->allowEmptyString)) {
if (null === $value) {
return;
}

Expand Down

This file was deleted.

10 changes: 2 additions & 8 deletions src/Symfony/Component/Validator/Constraints/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,8 @@ public function __construct(
throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "minPropertyPath" or "maxPropertyPath" option.', static::class));
}

if (null !== $this->min && null !== $this->max) {
$this->deprecatedMinMessageSet = isset($options['minMessage']) || null !== $minMessage;
$this->deprecatedMaxMessageSet = isset($options['maxMessage']) || null !== $maxMessage;

// BC layer, should throw a ConstraintDefinitionException in 6.0
if ($this->deprecatedMinMessageSet || $this->deprecatedMaxMessageSet) {
trigger_deprecation('symfony/validator', '4.4', '"minMessage" and "maxMessage" are deprecated when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
}
if (null !== $this->min && null !== $this->max && ($minMessage || $maxMessage)) {
throw new ConstraintDefinitionException(sprintf('The "%s" constraint can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.', static::class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,12 @@
*/
class AddAutoMappingConfigurationPass implements CompilerPassInterface
{
private $validatorBuilderService;
private $tag;

public function __construct(string $validatorBuilderService = 'validator.builder', string $tag = 'validator.auto_mapper')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/validator', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}

$this->validatorBuilderService = $validatorBuilderService;
$this->tag = $tag;
}

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('validator.auto_mapping') || !$container->hasDefinition($this->validatorBuilderService)) {
if (!$container->hasParameter('validator.auto_mapping') || !$container->hasDefinition('validator.builder')) {
return;
}

Expand All @@ -60,8 +47,8 @@ public function process(ContainerBuilder $container)
}
}

$validatorBuilder = $container->getDefinition($this->validatorBuilderService);
foreach ($container->findTaggedServiceIds($this->tag) as $id => $tags) {
$validatorBuilder = $container->getDefinition('validator.builder');
foreach ($container->findTaggedServiceIds('validator.auto_mapper') as $id => $tags) {
$regexp = $this->getRegexp(array_merge($globalNamespaces, $servicesToNamespaces[$id] ?? []));
$validatorBuilder->addMethodCall('addLoader', [new Reference($id)]);
$container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,14 @@
*/
class AddConstraintValidatorsPass implements CompilerPassInterface
{
private $validatorFactoryServiceId;
private $constraintValidatorTag;

public function __construct(string $validatorFactoryServiceId = 'validator.validator_factory', string $constraintValidatorTag = 'validator.constraint_validator')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/validator', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}

$this->validatorFactoryServiceId = $validatorFactoryServiceId;
$this->constraintValidatorTag = $constraintValidatorTag;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->validatorFactoryServiceId)) {
if (!$container->hasDefinition('validator.validator_factory')) {
return;
}

$validators = [];
foreach ($container->findTaggedServiceIds($this->constraintValidatorTag, true) as $id => $attributes) {
foreach ($container->findTaggedServiceIds('validator.constraint_validator', true) as $id => $attributes) {
$definition = $container->getDefinition($id);

if (isset($attributes[0]['alias'])) {
Expand All @@ -53,7 +40,7 @@ public function process(ContainerBuilder $container)
}

$container
->getDefinition($this->validatorFactoryServiceId)
->getDefinition('validator.validator_factory')
->replaceArgument(0, ServiceLocatorTagPass::register($container, $validators))
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,17 @@
*/
class AddValidatorInitializersPass implements CompilerPassInterface
{
private $builderService;
private $initializerTag;

public function __construct(string $builderService = 'validator.builder', string $initializerTag = 'validator.initializer')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/validator', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}

$this->builderService = $builderService;
$this->initializerTag = $initializerTag;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->builderService)) {
if (!$container->hasDefinition('validator.builder')) {
return;
}

$initializers = [];
foreach ($container->findTaggedServiceIds($this->initializerTag, true) as $id => $attributes) {
foreach ($container->findTaggedServiceIds('validator.initializer', true) as $id => $attributes) {
$initializers[] = new Reference($id);
}

$container->getDefinition($this->builderService)->addMethodCall('addObjectInitializers', [$initializers]);
$container->getDefinition('validator.builder')->addMethodCall('addObjectInitializers', [$initializers]);
}
}
22 changes: 0 additions & 22 deletions src/Symfony/Component/Validator/Tests/Constraints/LengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Validator\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand All @@ -23,8 +22,6 @@
*/
class LengthTest extends TestCase
{
use ExpectDeprecationTrait;

public function testNormalizerCanBeSet()
{
$length = new Length(['min' => 0, 'max' => 10, 'normalizer' => 'trim']);
Expand All @@ -46,25 +43,6 @@ public function testInvalidNormalizerObjectThrowsException()
new Length(['min' => 0, 'max' => 10, 'normalizer' => new \stdClass()]);
}

/**
* @group legacy
* @dataProvider allowEmptyStringOptionData
*/
public function testDeprecatedAllowEmptyStringOption(bool $value)
{
$this->expectDeprecation('Since symfony/validator 5.2: The "allowEmptyString" option of the "Symfony\Component\Validator\Constraints\Length" constraint is deprecated.');

new Length(['allowEmptyString' => $value, 'max' => 5]);
}

public function allowEmptyStringOptionData()
{
return [
[true],
[false],
];
}

public function testConstraintDefaultOption()
{
$constraint = new Length(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public function testNullIsValid()
$this->assertNoViolation();
}

/**
* @group legacy
*/
public function testAllowEmptyString()
{
$this->validator->validate('', new Length(['value' => 6, 'allowEmptyString' => true]));

$this->assertNoViolation();
}

public function testEmptyStringIsInvalid()
{
$this->validator->validate('', new Length([
Expand Down
44 changes: 4 additions & 40 deletions src/Symfony/Component/Validator/Tests/Constraints/RangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
namespace Symfony\Component\Validator\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\MissingOptionsException;

class RangeTest extends TestCase
{
use ExpectDeprecationTrait;

public function testThrowsConstraintExceptionIfBothMinLimitAndPropertyPath()
{
$this->expectException(ConstraintDefinitionException::class);
Expand Down Expand Up @@ -59,43 +56,10 @@ public function testThrowsNoDefaultOptionConfiguredException()
new Range('value');
}

public function provideDeprecationTriggeredIfMinMaxAndMinMessageOrMaxMessageSet(): array
{
return [
[['min' => 1, 'max' => 10, 'minMessage' => 'my_min_message'], true, false],
[['min' => 1, 'max' => 10, 'maxMessage' => 'my_max_message'], false, true],
[['min' => 1, 'max' => 10, 'minMessage' => 'my_min_message', 'maxMessage' => 'my_max_message'], true, true],
];
}

/**
* @group legacy
* @dataProvider provideDeprecationTriggeredIfMinMaxAndMinMessageOrMaxMessageSet
*/
public function testDeprecationTriggeredIfMinMaxAndMinMessageOrMaxMessageSet(array $options, bool $expectedDeprecatedMinMessageSet, bool $expectedDeprecatedMaxMessageSet)
{
$this->expectDeprecation('Since symfony/validator 4.4: "minMessage" and "maxMessage" are deprecated when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');

$sut = new Range($options);
$this->assertEquals($expectedDeprecatedMinMessageSet, $sut->deprecatedMinMessageSet);
$this->assertEquals($expectedDeprecatedMaxMessageSet, $sut->deprecatedMaxMessageSet);
}

public function provideDeprecationNotTriggeredIfNotMinMaxOrNotMinMessageNorMaxMessageSet(): array
{
return [
[['min' => 1, 'minMessage' => 'my_min_message', 'maxMessage' => 'my_max_message']],
[['max' => 10, 'minMessage' => 'my_min_message', 'maxMessage' => 'my_max_message']],
[['min' => 1, 'max' => 10, 'notInRangeMessage' => 'my_message']],
];
}

/**
* @doesNotPerformAssertions
* @dataProvider provideDeprecationNotTriggeredIfNotMinMaxOrNotMinMessageNorMaxMessageSet
*/
public function testDeprecationNotTriggeredIfNotMinMaxOrNotMinMessageNorMaxMessageSet(array $options)
public function testThrowsConstraintDefinitionExceptionIfBothMinAndMaxAndMinMessageOrMaxMessage()
{
new Range($options);
$this->expectException(\Symfony\Component\Validator\Exception\ConstraintDefinitionException::class);
$this->expectExceptionMessage('can not use "minMessage" and "maxMessage" when the "min" and "max" options are both set. Use "notInRangeMessage" instead.');
eval('new \Symfony\Component\Validator\Constraints\Range(min: "min", max: "max", minMessage: "minMessage", maxMessage: "maxMessage");');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,8 @@ public function provideMessageIfMinAndMaxSet(): array
[
['minMessage' => 'min_message'],
0,
'min_message',
Range::TOO_LOW_ERROR,
$notInRangeMessage,
Range::NOT_IN_RANGE_ERROR,
],
[
['maxMessage' => 'max_message'],
Expand All @@ -1055,8 +1055,8 @@ public function provideMessageIfMinAndMaxSet(): array
[
['maxMessage' => 'max_message'],
15,
'max_message',
Range::TOO_HIGH_ERROR,
$notInRangeMessage,
Range::NOT_IN_RANGE_ERROR,
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ValidValidatorTest extends TestCase
public function testPropertyPathsArePassedToNestedContexts()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader()->getValidator();
$validator = $validatorBuilder->enableAnnotationMapping()->addDefaultDoctrineAnnotationReader()->getValidator();

$violations = $validator->validate(new Foo(), null, ['nested']);

Expand All @@ -23,7 +23,7 @@ public function testPropertyPathsArePassedToNestedContexts()
public function testNullValues()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader()->getValidator();
$validator = $validatorBuilder->enableAnnotationMapping()->addDefaultDoctrineAnnotationReader()->getValidator();

$foo = new Foo();
$foo->fooBar = null;
Expand Down
Loading