Skip to content

Commit

Permalink
feature #32041 [Validator] Deprecate unused arg in ExpressionValidato…
Browse files Browse the repository at this point in the history
…r (ogizanagi)

This PR was merged into the 4.4 branch.

Discussion
----------

[Validator] Deprecate unused arg in ExpressionValidator

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | N/A   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A <!-- required for new features -->

Time to deprecate this unused first argument?

Commits
-------

0c0978c [Validator] Deprecate unused arg in ExpressionValidator
  • Loading branch information
fabpot committed Jun 14, 2019
2 parents 50c62d7 + 0c0978c commit 68d1b3f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions UPGRADE-4.4.md
Expand Up @@ -85,3 +85,9 @@ TwigBridge

* Deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
`DebugCommand::__construct()` method, swap the variables position.

Validator
---------

* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
Pass it as the first argument instead.
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Expand Up @@ -453,6 +453,7 @@ TwigBridge
Validator
--------

* An `ExpressionLanguage` instance or null must be passed as the first argument of `ExpressionValidator::__construct()`
* The `checkMX` and `checkHost` options of the `Email` constraint were removed
* The `Email::__construct()` 'strict' property has been removed. Use 'mode'=>"strict" instead.
* Calling `EmailValidator::__construct()` method with a boolean parameter has been removed, use `EmailValidator("strict")` instead.
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`. Pass it as the first argument instead.
* added the `compared_value_path` parameter in violations when using any
comparison constraint with the `propertyPath` option.
* added support for checking an array of types in `TypeValidator`
Expand Down
Expand Up @@ -25,8 +25,19 @@ class ExpressionValidator extends ConstraintValidator
{
private $expressionLanguage;

public function __construct($propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
public function __construct(/*ExpressionLanguage */$expressionLanguage = null)
{
if (!$expressionLanguage instanceof ExpressionLanguage) {
if (null !== $expressionLanguage) {
@trigger_error(sprintf('The "%s" first argument must be an instance of "%s" or null since 4.4. "%s" given', __METHOD__, ExpressionLanguage::class, \is_object($expressionLanguage) ? \get_class($expressionLanguage) : \gettype($expressionLanguage)), E_USER_DEPRECATED);
}

if (\func_num_args() > 1 && func_get_arg(1) instanceof ExpressionLanguage) {
@trigger_error(sprintf('The "%s" instance should be passed as "%s" first argument instead of second argument since 4.4.', ExpressionLanguage::class, __METHOD__), E_USER_DEPRECATED);
$expressionLanguage = func_get_arg(1);
}
}

$this->expressionLanguage = $expressionLanguage;
}

Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\ExpressionValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
Expand Down Expand Up @@ -253,6 +254,34 @@ public function testExpressionLanguageUsage()
'expression' => 'false',
]);

$expressionLanguage = $this->getMockBuilder(ExpressionLanguage::class)->getMock();

$used = false;

$expressionLanguage->method('evaluate')
->willReturnCallback(function () use (&$used) {
$used = true;

return true;
});

$validator = new ExpressionValidator($expressionLanguage);
$validator->initialize($this->createContext());
$validator->validate(null, $constraint);

$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
}

/**
* @group legacy
* @expectedDeprecation The "Symfony\Component\ExpressionLanguage\ExpressionLanguage" instance should be passed as "Symfony\Component\Validator\Constraints\ExpressionValidator::__construct" first argument instead of second argument since 4.4.
*/
public function testLegacyExpressionLanguageUsage()
{
$constraint = new Expression([
'expression' => 'false',
]);

$expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();

$used = false;
Expand All @@ -271,6 +300,15 @@ public function testExpressionLanguageUsage()
$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
}

/**
* @group legacy
* @expectedDeprecation The "Symfony\Component\Validator\Constraints\ExpressionValidator::__construct" first argument must be an instance of "Symfony\Component\ExpressionLanguage\ExpressionLanguage" or null since 4.4. "string" given
*/
public function testConstructorInvalidType()
{
new ExpressionValidator('foo');
}

public function testPassingCustomValues()
{
$constraint = new Expression([
Expand Down

0 comments on commit 68d1b3f

Please sign in to comment.