Skip to content

Commit 68d1b3f

Browse files
committed
feature #32041 [Validator] Deprecate unused arg in ExpressionValidator (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
2 parents 50c62d7 + 0c0978c commit 68d1b3f

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

UPGRADE-4.4.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ TwigBridge
8585

8686
* Deprecated to pass `$rootDir` and `$fileLinkFormatter` as 5th and 6th argument respectively to the
8787
`DebugCommand::__construct()` method, swap the variables position.
88+
89+
Validator
90+
---------
91+
92+
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
93+
Pass it as the first argument instead.

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ TwigBridge
453453
Validator
454454
--------
455455

456+
* An `ExpressionLanguage` instance or null must be passed as the first argument of `ExpressionValidator::__construct()`
456457
* The `checkMX` and `checkHost` options of the `Email` constraint were removed
457458
* The `Email::__construct()` 'strict' property has been removed. Use 'mode'=>"strict" instead.
458459
* Calling `EmailValidator::__construct()` method with a boolean parameter has been removed, use `EmailValidator("strict")` instead.

src/Symfony/Component/Validator/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`

src/Symfony/Component/Validator/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

src/Symfony/Component/Validator/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)