Skip to content

Commit b57212c

Browse files
committed
deprecate non-string constraint violation codes
1 parent 3644b70 commit b57212c

File tree

7 files changed

+73
-1
lines changed

7 files changed

+73
-1
lines changed

UPGRADE-4.4.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,8 @@ TwigBridge
9090
Validator
9191
---------
9292

93+
* Deprecated using anything else than a `string` as the code of a `ConstraintViolation`, a `string` type-hint will
94+
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
95+
method in 5.0.
9396
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
9497
Pass it as the first argument instead.

UPGRADE-5.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ TwigBridge
454454
Validator
455455
--------
456456

457+
* Removed support for non-string codes of a `ConstraintViolation`. A `string` type-hint was added to the constructor of
458+
the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()` method.
457459
* An `ExpressionLanguage` instance or null must be passed as the first argument of `ExpressionValidator::__construct()`
458460
* The `checkMX` and `checkHost` options of the `Email` constraint were removed
459461
* The `Email::__construct()` 'strict' property has been removed. Use 'mode'=>"strict" instead.

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* using anything else than a `string` as the code of a `ConstraintViolation` is deprecated, a `string` type-hint will
8+
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
9+
method in 5.0
710
* deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`. Pass it as the first argument instead.
811
* added the `compared_value_path` parameter in violations when using any
912
comparison constraint with the `propertyPath` option.

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public function __construct(?string $message, ?string $messageTemplate, array $p
5656
$message = '';
5757
}
5858

59+
if (null !== $code && !\is_string($code)) {
60+
@trigger_error(sprintf('Not using a string as the error code in %s() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.', __METHOD__), E_USER_DEPRECATED);
61+
}
62+
5963
$this->message = $message;
6064
$this->messageTemplate = $messageTemplate;
6165
$this->parameters = $parameters;

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testToStringHandlesCodes()
6464
'some_value',
6565
null,
6666
null,
67-
0
67+
'0'
6868
);
6969

7070
$expected = <<<'EOF'
@@ -108,4 +108,24 @@ public function testToStringOmitsEmptyCodes()
108108

109109
$this->assertSame($expected, (string) $violation);
110110
}
111+
112+
/**
113+
* @group legacy
114+
* @expectedDeprecation Not using a string as the error code in Symfony\Component\Validator\ConstraintViolation::__construct() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.
115+
*/
116+
public function testNonStringCode()
117+
{
118+
$violation = new ConstraintViolation(
119+
'42 cannot be used here',
120+
'this is the message template',
121+
[],
122+
['some_value' => 42],
123+
'some_value',
124+
null,
125+
null,
126+
42
127+
);
128+
129+
self::assertSame(42, $violation->getCode());
130+
}
111131
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Violation;
13+
14+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
15+
use Symfony\Component\Translation\IdentityTranslator;
16+
use Symfony\Component\Validator\ConstraintViolationList;
17+
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
18+
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
19+
20+
class ConstraintViolationBuilderTest extends TestCase
21+
{
22+
/**
23+
* @group legacy
24+
* @expectedDeprecation Not using a string as the error code in Symfony\Component\Validator\Violation\ConstraintViolationBuilder::setCode() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.
25+
* @expectedDeprecation Not using a string as the error code in Symfony\Component\Validator\ConstraintViolation::__construct() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.
26+
*/
27+
public function testNonStringCode()
28+
{
29+
$constraintViolationList = new ConstraintViolationList();
30+
(new ConstraintViolationBuilder($constraintViolationList, new ConstraintA(), 'invalid message', [], null, 'foo', 'baz', new IdentityTranslator()))
31+
->setCode(42)
32+
->addViolation();
33+
34+
self::assertSame(42, $constraintViolationList->get(0)->getCode());
35+
}
36+
}

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function setPlural($number)
132132
*/
133133
public function setCode($code)
134134
{
135+
if (null !== $code && !\is_string($code)) {
136+
@trigger_error(sprintf('Not using a string as the error code in %s() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.', __METHOD__), E_USER_DEPRECATED);
137+
}
138+
135139
$this->code = $code;
136140

137141
return $this;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy