deprecate non-string constraint violation codes
This commit is contained in:
parent
3644b70d42
commit
e217729066
@ -90,5 +90,8 @@ TwigBridge
|
|||||||
Validator
|
Validator
|
||||||
---------
|
---------
|
||||||
|
|
||||||
|
* 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()`.
|
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
|
||||||
Pass it as the first argument instead.
|
Pass it as the first argument instead.
|
||||||
|
@ -454,6 +454,8 @@ TwigBridge
|
|||||||
Validator
|
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()`
|
* 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 `checkMX` and `checkHost` options of the `Email` constraint were removed
|
||||||
* The `Email::__construct()` 'strict' property has been removed. Use 'mode'=>"strict" instead.
|
* The `Email::__construct()` 'strict' property has been removed. Use 'mode'=>"strict" instead.
|
||||||
|
@ -4,6 +4,9 @@ CHANGELOG
|
|||||||
4.4.0
|
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.
|
* 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
|
* added the `compared_value_path` parameter in violations when using any
|
||||||
comparison constraint with the `propertyPath` option.
|
comparison constraint with the `propertyPath` option.
|
||||||
|
@ -56,6 +56,10 @@ class ConstraintViolation implements ConstraintViolationInterface
|
|||||||
$message = '';
|
$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->message = $message;
|
||||||
$this->messageTemplate = $messageTemplate;
|
$this->messageTemplate = $messageTemplate;
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
|
@ -65,49 +65,49 @@ class FileValidator extends ConstraintValidator
|
|||||||
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
|
$this->context->buildViolation($constraint->uploadIniSizeErrorMessage)
|
||||||
->setParameter('{{ limit }}', $limitAsString)
|
->setParameter('{{ limit }}', $limitAsString)
|
||||||
->setParameter('{{ suffix }}', $suffix)
|
->setParameter('{{ suffix }}', $suffix)
|
||||||
->setCode(UPLOAD_ERR_INI_SIZE)
|
->setCode((string) UPLOAD_ERR_INI_SIZE)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
|
$this->context->buildViolation($constraint->uploadFormSizeErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_FORM_SIZE)
|
->setCode((string) UPLOAD_ERR_FORM_SIZE)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_PARTIAL:
|
case UPLOAD_ERR_PARTIAL:
|
||||||
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
|
$this->context->buildViolation($constraint->uploadPartialErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_PARTIAL)
|
->setCode((string) UPLOAD_ERR_PARTIAL)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_NO_FILE:
|
case UPLOAD_ERR_NO_FILE:
|
||||||
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
|
$this->context->buildViolation($constraint->uploadNoFileErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_NO_FILE)
|
->setCode((string) UPLOAD_ERR_NO_FILE)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_NO_TMP_DIR:
|
case UPLOAD_ERR_NO_TMP_DIR:
|
||||||
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
|
$this->context->buildViolation($constraint->uploadNoTmpDirErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_NO_TMP_DIR)
|
->setCode((string) UPLOAD_ERR_NO_TMP_DIR)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_CANT_WRITE:
|
case UPLOAD_ERR_CANT_WRITE:
|
||||||
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
|
$this->context->buildViolation($constraint->uploadCantWriteErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_CANT_WRITE)
|
->setCode((string) UPLOAD_ERR_CANT_WRITE)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
case UPLOAD_ERR_EXTENSION:
|
case UPLOAD_ERR_EXTENSION:
|
||||||
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
|
$this->context->buildViolation($constraint->uploadExtensionErrorMessage)
|
||||||
->setCode(UPLOAD_ERR_EXTENSION)
|
->setCode((string) UPLOAD_ERR_EXTENSION)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
$this->context->buildViolation($constraint->uploadErrorMessage)
|
$this->context->buildViolation($constraint->uploadErrorMessage)
|
||||||
->setCode($value->getError())
|
->setCode((string) $value->getError())
|
||||||
->addViolation();
|
->addViolation();
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -64,7 +64,7 @@ EOF;
|
|||||||
'some_value',
|
'some_value',
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
0
|
'0'
|
||||||
);
|
);
|
||||||
|
|
||||||
$expected = <<<'EOF'
|
$expected = <<<'EOF'
|
||||||
@ -108,4 +108,24 @@ EOF;
|
|||||||
|
|
||||||
$this->assertSame($expected, (string) $violation);
|
$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 @@ abstract class FileValidatorTest extends ConstraintValidatorTestCase
|
|||||||
public function uploadedFileErrorProvider()
|
public function uploadedFileErrorProvider()
|
||||||
{
|
{
|
||||||
$tests = [
|
$tests = [
|
||||||
[UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
|
[(string) UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'],
|
||||||
[UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
|
[(string) UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'],
|
||||||
[UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
|
[(string) UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'],
|
||||||
[UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
|
[(string) UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'],
|
||||||
[UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
|
[(string) UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'],
|
||||||
[UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
|
[(string) UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'],
|
||||||
];
|
];
|
||||||
|
|
||||||
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
|
if (class_exists('Symfony\Component\HttpFoundation\File\UploadedFile')) {
|
||||||
// when no maxSize is specified on constraint, it should use the ini value
|
// 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,
|
'{{ limit }}' => UploadedFile::getMaxFilesize() / 1048576,
|
||||||
'{{ suffix }}' => 'MiB',
|
'{{ suffix }}' => 'MiB',
|
||||||
]];
|
]];
|
||||||
|
|
||||||
// it should use the smaller limitation (maxSize option in this case)
|
// 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,
|
'{{ limit }}' => 1,
|
||||||
'{{ suffix }}' => 'bytes',
|
'{{ suffix }}' => 'bytes',
|
||||||
], '1'];
|
], '1'];
|
||||||
@ -464,14 +464,14 @@ abstract class FileValidatorTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
// it correctly parses the maxSize option and not only uses simple string comparison
|
// it correctly parses the maxSize option and not only uses simple string comparison
|
||||||
// 1000M should be bigger than the ini value
|
// 1000M should be bigger than the ini value
|
||||||
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
|
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
|
||||||
'{{ limit }}' => $limit,
|
'{{ limit }}' => $limit,
|
||||||
'{{ suffix }}' => $suffix,
|
'{{ suffix }}' => $suffix,
|
||||||
], '1000M'];
|
], '1000M'];
|
||||||
|
|
||||||
// it correctly parses the maxSize option and not only uses simple string comparison
|
// it correctly parses the maxSize option and not only uses simple string comparison
|
||||||
// 1000M should be bigger than the ini value
|
// 1000M should be bigger than the ini value
|
||||||
$tests[] = [UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
|
$tests[] = [(string) UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', [
|
||||||
'{{ limit }}' => '0.1',
|
'{{ limit }}' => '0.1',
|
||||||
'{{ suffix }}' => 'MB',
|
'{{ suffix }}' => 'MB',
|
||||||
], '100K'];
|
], '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 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
|
|||||||
*/
|
*/
|
||||||
public function setCode($code)
|
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;
|
$this->code = $code;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
Reference in New Issue
Block a user