Skip to content

Commit 2ac4503

Browse files
committed
feature symfony#45484 Make constraint violation interfaces stringable (HypeMC)
This PR was merged into the 6.1 branch. Discussion ---------- Make constraint violation interfaces stringable | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - Currently the `ConstraintViolationInterface` & `ConstraintViolationListInterface` don't have a `__toString()` method even though their only implementations do. Since the `validate` method returns a `ConstraintViolationListInterface` trying to cast the errors into a string doesn't sit well with static analysis tools. Here I've used an [example from the docs](https://symfony.com/doc/current/validation.html#using-the-validator-service): ![Screen](https://user-images.githubusercontent.com/2445045/154810607-4c95ee66-9d21-4456-b857-3cfa7a8f1b80.png) This PR adds a `__toString()` method to the interfaces via an `@method` tag. In Symfony 7 the `Stringable` interface can be implemented instead. Commits ------- 4cead0c Make the ConstraintViolationInterface & ConstraintViolationListInterface stringable
2 parents f6911c7 + 4cead0c commit 2ac4503

File tree

6 files changed

+9
-2
lines changed

6 files changed

+9
-2
lines changed

UPGRADE-6.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ Validator
3333

3434
* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead
3535
* Deprecate constraint `ExpressionLanguageSyntax`, use `ExpressionSyntax` instead
36+
* Implementing the `ConstraintViolationInterface` or `ConstraintViolationListInterface`
37+
without implementing the `__toString()` method is deprecated

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead
88
* Deprecate constraint `ExpressionLanguageSyntax`, use `ExpressionSyntax` instead
9+
* Add method `__toString()` to `ConstraintViolationInterface` & `ConstraintViolationListInterface`
910

1011
6.0
1112
---

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(string|\Stringable $message, ?string $messageTemplat
6464
}
6565

6666
/**
67-
* Converts the violation into a string for debugging purposes.
67+
* {@inheritdoc}
6868
*/
6969
public function __toString(): string
7070
{

src/Symfony/Component/Validator/ConstraintViolationInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
* element is still the person, but the property path is "address.street".
3131
*
3232
* @author Bernhard Schussek <[email protected]>
33+
*
34+
* @method string __toString() Converts the violation into a string for debugging purposes. Not implementing it is deprecated since Symfony 6.1.
3335
*/
3436
interface ConstraintViolationInterface
3537
{

src/Symfony/Component/Validator/ConstraintViolationList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function createFromMessage(string $message): self
4646
}
4747

4848
/**
49-
* Converts the violation into a string for debugging purposes.
49+
* {@inheritdoc}
5050
*/
5151
public function __toString(): string
5252
{

src/Symfony/Component/Validator/ConstraintViolationListInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* @extends \ArrayAccess<int, ConstraintViolationInterface>
2020
* @extends \Traversable<int, ConstraintViolationInterface>
21+
*
22+
* @method string __toString() Converts the violation into a string for debugging purposes. Not implementing it is deprecated since Symfony 6.1.
2123
*/
2224
interface ConstraintViolationListInterface extends \Traversable, \Countable, \ArrayAccess
2325
{

0 commit comments

Comments
 (0)