Skip to content

[Validator] Deprecate unused arg in ExpressionValidator #32041

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
Jun 14, 2019
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: 6 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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