Skip to content

Commit e3162d8

Browse files
committed
[Validator] Deprecate unused arg in ExpressionValidator
1 parent bab1f3f commit e3162d8

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`. Pass it as the first argument instead.
78
* added the `compared_value_path` parameter in violations when using any
89
comparison constraint with the `propertyPath` option.
910
* added support for checking an array of types in `TypeValidator`

Constraints/ExpressionValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ class ExpressionValidator extends ConstraintValidator
2525
{
2626
private $expressionLanguage;
2727

28-
public function __construct($propertyAccessor = null, ExpressionLanguage $expressionLanguage = null)
28+
public function __construct(/*ExpressionLanguage */$expressionLanguage = null)
2929
{
30+
if (!$expressionLanguage instanceof ExpressionLanguage) {
31+
if (null !== $expressionLanguage) {
32+
@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);
33+
}
34+
35+
if (\func_num_args() > 1 && func_get_arg(1) instanceof ExpressionLanguage) {
36+
@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);
37+
$expressionLanguage = func_get_arg(1);
38+
}
39+
}
40+
3041
$this->expressionLanguage = $expressionLanguage;
3142
}
3243

Tests/Constraints/ExpressionValidatorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1415
use Symfony\Component\Validator\Constraints\Expression;
1516
use Symfony\Component\Validator\Constraints\ExpressionValidator;
1617
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -253,6 +254,34 @@ public function testExpressionLanguageUsage()
253254
'expression' => 'false',
254255
]);
255256

257+
$expressionLanguage = $this->getMockBuilder(ExpressionLanguage::class)->getMock();
258+
259+
$used = false;
260+
261+
$expressionLanguage->method('evaluate')
262+
->willReturnCallback(function () use (&$used) {
263+
$used = true;
264+
265+
return true;
266+
});
267+
268+
$validator = new ExpressionValidator($expressionLanguage);
269+
$validator->initialize($this->createContext());
270+
$validator->validate(null, $constraint);
271+
272+
$this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
273+
}
274+
275+
/**
276+
* @group legacy
277+
* @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.
278+
*/
279+
public function testLegacyExpressionLanguageUsage()
280+
{
281+
$constraint = new Expression([
282+
'expression' => 'false',
283+
]);
284+
256285
$expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
257286

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

303+
/**
304+
* @group legacy
305+
* @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
306+
*/
307+
public function testConstructorInvalidType()
308+
{
309+
new ExpressionValidator('foo');
310+
}
311+
274312
public function testPassingCustomValues()
275313
{
276314
$constraint = new Expression([

0 commit comments

Comments
 (0)