Skip to content

[3.0] [Validator] deprecations cleanup #15708

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

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
Expand All @@ -37,15 +36,11 @@ public static function getSubscribedEvents()
}

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ViolationMapperInterface $violationMapper
* @param ValidatorInterface $validator
* @param ViolationMapperInterface $violationMapper
*/
public function __construct($validator, ViolationMapperInterface $violationMapper)
public function __construct(ValidatorInterface $validator, ViolationMapperInterface $violationMapper)
{
if (!$validator instanceof ValidatorInterface && !$validator instanceof LegacyValidatorInterface) {
throw new \InvalidArgumentException('Validator must be instance of Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
}

$this->validator = $validator;
$this->violationMapper = $violationMapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -35,14 +34,10 @@ class FormTypeValidatorExtension extends BaseValidatorExtension
private $violationMapper;

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ValidatorInterface $validator
*/
public function __construct($validator)
public function __construct(ValidatorInterface $validator)
{
if (!$validator instanceof ValidatorInterface && !$validator instanceof LegacyValidatorInterface) {
throw new \InvalidArgumentException('Validator must be instance of Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
}

$this->validator = $validator;
$this->violationMapper = new ViolationMapper();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;

/**
* Extension supporting the Symfony Validator component in forms.
Expand All @@ -26,31 +25,26 @@
*/
class ValidatorExtension extends AbstractExtension
{
/**
* @var ValidatorInterface
*/
private $validator;

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ValidatorInterface $validator
*
* @throws UnexpectedTypeException If $validator is invalid
*/
public function __construct($validator)
public function __construct(ValidatorInterface $validator)
{
// 2.5 API
if ($validator instanceof ValidatorInterface) {
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');
// 2.4 API
} elseif ($validator instanceof LegacyValidatorInterface) {
$metadata = $validator->getMetadataFactory()->getMetadataFor('Symfony\Component\Form\Form');
} else {
throw new UnexpectedTypeException($validator, 'Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
}
/* @var ClassMetadata $metadata */
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');

// Register the form constraints in the validator programmatically.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are missing a deprecation warning here btw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What needs to be done?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a PR should be opened to add the deprecation warning in the 2.7 branch

// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml

/* @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());

Expand All @@ -59,13 +53,7 @@ public function __construct($validator)

public function loadTypeGuesser()
{
// 2.5 API
if ($this->validator instanceof ValidatorInterface) {
return new ValidatorTypeGuesser($this->validator);
}

// 2.4 API
return new ValidatorTypeGuesser($this->validator->getMetadataFactory());
return new ValidatorTypeGuesser($this->validator);
}

protected function loadTypeExtensions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;

class ValidatorTypeGuesser implements FormTypeGuesserInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function setUp()
{
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
$this->violationMapper = $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface');
$this->listener = new ValidationListener($this->validator, $this->violationMapper);
$this->message = 'Message';
Expand Down Expand Up @@ -182,31 +182,9 @@ public function testValidateWithEmptyViolationList()

public function testValidatorInterfaceSinceSymfony25()
{
// Mock of ValidatorInterface since apiVersion 2.5
$validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');

$listener = new ValidationListener($validator, $this->violationMapper);
$this->assertAttributeSame($validator, 'validator', $listener);
}

/**
* @group legacy
*/
public function testValidatorInterfaceUntilSymfony24()
{
// Mock of ValidatorInterface until apiVersion 2.4
$validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');

$listener = new ValidationListener($validator, $this->violationMapper);
$this->assertAttributeSame($validator, 'validator', $listener);
}

/**
* @group legacy
* @expectedException \InvalidArgumentException
*/
public function testInvalidValidatorInterface()
{
new ValidationListener(null, $this->violationMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,12 @@ public function testValidConstraint()

public function testValidatorInterfaceSinceSymfony25()
{
// Mock of ValidatorInterface since apiVersion 2.5
$validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');

$formTypeValidatorExtension = new FormTypeValidatorExtension($validator);
$this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension);
}

public function testValidatorInterfaceUntilSymfony24()
{
// Mock of ValidatorInterface until apiVersion 2.4
$validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');

$formTypeValidatorExtension = new FormTypeValidatorExtension($validator);
$this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidValidatorInterface()
{
new FormTypeValidatorExtension(null);
}

protected function createForm(array $options = array())
{
return $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, $options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ abstract class TypeTestCase extends BaseTypeTestCase

protected function setUp()
{
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
$this->validator->expects($this->once())->method('getMetadataFactory')->will($this->returnValue($metadataFactory));
$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$metadataFactory->expects($this->once())->method('getMetadataFor')->will($this->returnValue($metadata));
$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();

$metadata->expects($this->once())
->method('addConstraint')
->willReturn(null);

$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
$this->validator->expects($this->once())
->method('getMetadataFor')
->willReturn($metadata);

parent::setUp();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function test2Dot5ValidationApi()
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\RecursiveValidator')
->disableOriginalConstructor()
->getMock();

$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();
Expand All @@ -38,8 +39,7 @@ public function test2Dot5ValidationApi()
->method('addPropertyConstraint')
->with('children', $this->isInstanceOf('Symfony\Component\Validator\Constraints\Valid'));

$validator
->expects($this->never())
$validator->expects($this->never())
->method('getMetadataFactory');

$extension = new ValidatorExtension($validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ValidatorTypeGuesserTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->metadata = new ClassMetadata(self::TEST_CLASS);
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface');
$this->metadataFactory->expects($this->any())
->method('getMetadataFor')
->with(self::TEST_CLASS)
Expand Down
49 changes: 1 addition & 48 deletions src/Symfony/Component/Validator/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

namespace Symfony\Component\Validator;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the empty line between the namespace and use statements must be kept

use Symfony\Component\Validator\Context\ExecutionContextInterface as ExecutionContextInterface2Dot5;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
use Symfony\Component\Validator\Violation\LegacyConstraintViolationBuilder;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* Base class for constraint validators.
Expand Down Expand Up @@ -52,51 +50,6 @@ public function initialize(ExecutionContextInterface $context)
$this->context = $context;
}

/**
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
* supports the 2.4 context API.
*
* @param string $message The violation message
* @param array $parameters The message parameters
*
* @return ConstraintViolationBuilderInterface The violation builder
*
* @deprecated since version 2.5, to be removed in 3.0.
*/
protected function buildViolation($message, array $parameters = array())
{
@trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);

if ($this->context instanceof ExecutionContextInterface2Dot5) {
return $this->context->buildViolation($message, $parameters);
}

return new LegacyConstraintViolationBuilder($this->context, $message, $parameters);
}

/**
* Wrapper for {@link ExecutionContextInterface::buildViolation} that
* supports the 2.4 context API.
*
* @param ExecutionContextInterface $context The context to use
* @param string $message The violation message
* @param array $parameters The message parameters
*
* @return ConstraintViolationBuilderInterface The violation builder
*
* @deprecated since version 2.5, to be removed in 3.0.
*/
protected function buildViolationInContext(ExecutionContextInterface $context, $message, array $parameters = array())
{
@trigger_error('The '.__METHOD__.' is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);

if ($context instanceof ExecutionContextInterface2Dot5) {
return $context->buildViolation($message, $parameters);
}

return new LegacyConstraintViolationBuilder($context, $message, $parameters);
}

/**
* Returns a string representation of the type of the value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator;

use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
Expand Down
26 changes: 0 additions & 26 deletions src/Symfony/Component/Validator/ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,6 @@ public function getMessageTemplate()
return $this->messageTemplate;
}

/**
* {@inheritdoc}
*
* @deprecated since version 2.7, to be removed in 3.0.
* Use getParameters() instead
*/
public function getMessageParameters()
{
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getParameters() method instead.', E_USER_DEPRECATED);

return $this->parameters;
}

/**
* Alias of {@link getMessageParameters()}.
*/
Expand All @@ -160,19 +147,6 @@ public function getParameters()
return $this->parameters;
}

/**
* {@inheritdoc}
*
* @deprecated since version 2.7, to be removed in 3.0.
* Use getPlural() instead
*/
public function getMessagePluralization()
{
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.7, to be removed in 3.0. Use the ConstraintViolation::getPlural() method instead.', E_USER_DEPRECATED);

return $this->plural;
}

/**
* Alias of {@link getMessagePluralization()}.
*/
Expand Down
34 changes: 0 additions & 34 deletions src/Symfony/Component/Validator/ConstraintViolationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,6 @@ public function getMessage();
*/
public function getMessageTemplate();

/**
* Returns the parameters to be inserted into the raw violation message.
*
* @return array A possibly empty list of parameters indexed by the names
* that appear in the message template.
*
* @see getMessageTemplate()
*
* @api
*
* @deprecated since version 2.7, to be replaced by getParameters() in 3.0.
*/
public function getMessageParameters();

/**
* Returns a number for pluralizing the violation message.
*
* For example, the message template could have different translation based
* on a parameter "choices":
*
* <ul>
* <li>Please select exactly one entry. (choices=1)</li>
* <li>Please select two entries. (choices=2)</li>
* </ul>
*
* This method returns the value of the parameter for choosing the right
* pluralization form (in this case "choices").
*
* @return int|null The number to use to pluralize of the message.
*
* @deprecated since version 2.7, to be replaced by getPlural() in 3.0.
*/
public function getMessagePluralization();

/**
* Returns the root element of the validation.
*
Expand Down
Loading