Skip to content
Permalink
Browse files

feature #32265 [Validator] deprecate non-string constraint violation …

…codes (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Validator] deprecate non-string constraint violation codes

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #32066 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

e217729 deprecate non-string constraint violation codes
  • Loading branch information...
fabpot committed Jul 3, 2019
2 parents 4e32643 + e217729 commit 6abaa8c5a2470fcdb86330f4ab0cd2d3042b5090
@@ -106,6 +106,10 @@ Validator
---------

* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
* Deprecated using anything else than a `string` as the code of a `ConstraintViolation`, a `string` type-hint will
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
method in 5.0.
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
Pass it as the first argument instead.
* The `Length` constraint expects the `allowEmptyString` option to be defined
when the `min` option is used.
@@ -455,6 +455,8 @@ TwigBridge
Validator
--------

* Removed support for non-string codes of a `ConstraintViolation`. A `string` type-hint was added to the constructor of
the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()` method.
* 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.
@@ -4,6 +4,9 @@ CHANGELOG
4.4.0
-----

* using anything else than a `string` as the code of a `ConstraintViolation` is deprecated, a `string` type-hint will
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
method in 5.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.
@@ -56,6 +56,10 @@ public function __construct(?string $message, ?string $messageTemplate, array $p
$message = '';
}
if (null !== $code && !\is_string($code)) {
@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);
}
$this->message = $message;
$this->messageTemplate = $messageTemplate;
$this->parameters = $parameters;
@@ -65,49 +65,49 @@ public function validate($value, Constraint $constraint)
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setCode(UPLOAD_ERR_INI_SIZE)
->setCode((string) UPLOAD_ERR_INI_SIZE)
->addViolation();
return;
case UPLOAD_ERR_FORM_SIZE:
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
->setCode(UPLOAD_ERR_FORM_SIZE)
->setCode((string) UPLOAD_ERR_FORM_SIZE)
->addViolation();
return;
case UPLOAD_ERR_PARTIAL:
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
->setCode(UPLOAD_ERR_PARTIAL)
->setCode((string) UPLOAD_ERR_PARTIAL)
->addViolation();
return;
case UPLOAD_ERR_NO_FILE:
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
->setCode(UPLOAD_ERR_NO_FILE)
->setCode((string) UPLOAD_ERR_NO_FILE)
->addViolation();
return;
case UPLOAD_ERR_NO_TMP_DIR:
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
->setCode(UPLOAD_ERR_NO_TMP_DIR)
->setCode((string) UPLOAD_ERR_NO_TMP_DIR)
->addViolation();
return;
case UPLOAD_ERR_CANT_WRITE:
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
->setCode(UPLOAD_ERR_CANT_WRITE)
->setCode((string) UPLOAD_ERR_CANT_WRITE)
->addViolation();
return;
case UPLOAD_ERR_EXTENSION:
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
->setCode(UPLOAD_ERR_EXTENSION)
->setCode((string) UPLOAD_ERR_EXTENSION)
->addViolation();
return;
default:
$this->context->buildViolation($constraint->uploadErrorMessage)
->setCode($value->getError())
->setCode((string) $value->getError())
->addViolation();
return;
@@ -64,7 +64,7 @@ public function testToStringHandlesCodes()
'some_value',
null,
null,
0
'0'
);
$expected = <<<'EOF'
@@ -108,4 +108,24 @@ public function testToStringOmitsEmptyCodes()
$this->assertSame($expected, (string) $violation);
}
/**
* @group legacy
* @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.
*/
public function testNonStringCode()
{
$violation = new ConstraintViolation(
'42 cannot be used here',
'this is the message template',
[],
['some_value' => 42],
'some_value',
null,
null,
42
);
self::assertSame(42, $violation->getCode());
}
}
@@ -435,23 +435,23 @@ public function testUploadedFileError($error, $message, array $params = [], $max
public function uploadedFileErrorProvider()
{
$tests = [
[UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
[UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
[UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
[UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
[UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
[UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
[(string) UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
[(string) UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
[(string) UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
[(string) UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
[(string) UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
[(string) UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
];
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
// when no maxSize is specified on constraint, it should use the ini value
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
'{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
'{{ suffix }}' => 'MiB',
]];
// it should use the smaller limitation (maxSize option in this case)
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
'{{ limit }}' => 1,
'{{ suffix }}' => 'bytes',
], '1'];
@@ -464,14 +464,14 @@ public function uploadedFileErrorProvider()
// it correctly parses the maxSize option and not only uses simple string comparison
// 1000M should be bigger than the ini value
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
'{{ limit }}' => $limit,
'{{ suffix }}' => $suffix,
], '1000M'];
// it correctly parses the maxSize option and not only uses simple string comparison
// 1000M should be bigger than the ini value
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
'{{ limit }}' => '0.1',
'{{ suffix }}' => 'MB',
], '100K'];
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Tests\Violation;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
class ConstraintViolationBuilderTest extends TestCase
{
/**
* @group legacy
* @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.
* @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.
*/
public function testNonStringCode()
{
$constraintViolationList = new ConstraintViolationList();
(new ConstraintViolationBuilder($constraintViolationList, new ConstraintA(), 'invalid message', [], null, 'foo', 'baz', new IdentityTranslator()))
->setCode(42)
->addViolation();
self::assertSame(42, $constraintViolationList->get(0)->getCode());
}
}
@@ -132,6 +132,10 @@ public function setPlural($number)
*/
public function setCode($code)
{
if (null !== $code && !\is_string($code)) {
@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);
}
$this->code = $code;
return $this;

0 comments on commit 6abaa8c

Please sign in to comment.
You can’t perform that action at this time.