Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e217729

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

File tree

9 files changed

+91
-19
lines changed

9 files changed

+91
-19
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/Constraints/FileValidator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,49 +65,49 @@ public function validate($value, Constraint $constraint)
6565
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
6666
->setParameter('{{ limit }}', $limitAsString)
6767
->setParameter('{{ suffix }}', $suffix)
68-
->setCode(UPLOAD_ERR_INI_SIZE)
68+
->setCode((string) UPLOAD_ERR_INI_SIZE)
6969
->addViolation();
7070

7171
return;
7272
case UPLOAD_ERR_FORM_SIZE:
7373
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
74-
->setCode(UPLOAD_ERR_FORM_SIZE)
74+
->setCode((string) UPLOAD_ERR_FORM_SIZE)
7575
->addViolation();
7676

7777
return;
7878
case UPLOAD_ERR_PARTIAL:
7979
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
80-
->setCode(UPLOAD_ERR_PARTIAL)
80+
->setCode((string) UPLOAD_ERR_PARTIAL)
8181
->addViolation();
8282

8383
return;
8484
case UPLOAD_ERR_NO_FILE:
8585
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
86-
->setCode(UPLOAD_ERR_NO_FILE)
86+
->setCode((string) UPLOAD_ERR_NO_FILE)
8787
->addViolation();
8888

8989
return;
9090
case UPLOAD_ERR_NO_TMP_DIR:
9191
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
92-
->setCode(UPLOAD_ERR_NO_TMP_DIR)
92+
->setCode((string) UPLOAD_ERR_NO_TMP_DIR)
9393
->addViolation();
9494

9595
return;
9696
case UPLOAD_ERR_CANT_WRITE:
9797
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
98-
->setCode(UPLOAD_ERR_CANT_WRITE)
98+
->setCode((string) UPLOAD_ERR_CANT_WRITE)
9999
->addViolation();
100100

101101
return;
102102
case UPLOAD_ERR_EXTENSION:
103103
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
104-
->setCode(UPLOAD_ERR_EXTENSION)
104+
->setCode((string) UPLOAD_ERR_EXTENSION)
105105
->addViolation();
106106

107107
return;
108108
default:
109109
$this->context->buildViolation($constraint->uploadErrorMessage)
110-
->setCode($value->getError())
110+
->setCode((string) $value->getError())
111111
->addViolation();
112112

113113
return;

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
}

src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,23 +435,23 @@ public function testUploadedFileError($error, $message, array $params = [], $max
435435
public function uploadedFileErrorProvider()
436436
{
437437
$tests = [
438-
[UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
439-
[UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
440-
[UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
441-
[UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
442-
[UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
443-
[UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
438+
[(string) UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
439+
[(string) UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
440+
[(string) UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
441+
[(string) UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
442+
[(string) UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
443+
[(string) UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
444444
];
445445

446446
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
447447
// when no maxSize is specified on constraint, it should use the ini value
448-
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
448+
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
449449
'{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
450450
'{{ suffix }}' => 'MiB',
451451
]];
452452

453453
// it should use the smaller limitation (maxSize option in this case)
454-
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
454+
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
455455
'{{ limit }}' => 1,
456456
'{{ suffix }}' => 'bytes',
457457
], '1'];
@@ -464,14 +464,14 @@ public function uploadedFileErrorProvider()
464464

465465
// it correctly parses the maxSize option and not only uses simple string comparison
466466
// 1000M should be bigger than the ini value
467-
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
467+
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
468468
'{{ limit }}' => $limit,
469469
'{{ suffix }}' => $suffix,
470470
], '1000M'];
471471

472472
// it correctly parses the maxSize option and not only uses simple string comparison
473473
// 1000M should be bigger than the ini value
474-
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
474+
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
475475
'{{ limit }}' => '0.1',
476476
'{{ suffix }}' => 'MB',
477477
], '100K'];
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 <[email protected]>
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 PHPUnit\Framework\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)