You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
Currently Symfony\Component\Validator\ConstraintViolationListInterface does not offer method for accessing iterator. There is getIterator() as a part ConstraintViolationList concrete implementation which also implements IteratorAggregate interface, however Symfony\Component\Messenger\Exception\ValidationFailedException::getViolations() return type points to interface, not implementation. In result, it's "impossible" to access iterator when type-hinting to ConstraintViolationListInterface interface.
How to reproduce
<?php
use Symfony\Component\Messenger\Exception\ValidationFailedException;
use Symfony\Component\Validator\ConstraintViolationInterface;
try {
...
} catch (ValidationFailedException $e) {
dd(array_map(
static fn (ConstraintViolationInterface $violation) => [
$violation->getPropertyPath(),
$violation->getMessage(),
],
// Psalm complains: Method Symfony\Component\Validator\ConstraintViolationListInterface::getIterator does not exist
(array) $e->getViolations()->getIterator()
));
}
Possible Solution
Move implements \IteratorAggregate from ConstraintViolationList to ConstraintViolationListInterface (as extends \IteratorAggregate, obviously). I've tried it locally and Psalm doesn't raise an error.
Additional context
In Symfony app it works (because of implementation), the problem is with static analysis.
The text was updated successfully, but these errors were encountered:
and it works (Rector fixed iterator_to_array($e->getViolations) into [...$e->getViolations]).
Still, would be good to have more user-friendly (explicit) contract for accessing items. Feel free to close it as non-bug if you feel that current contract is correct.
@Wirone i think we can close yes, Symfony doesnt really define templated generic phpdocs currently. Instead it could be handled in plugin/stub files per tool (e.g. phpstan, psalm).
@ro0NL I didn't want to add support for generics in phpDoc, I thought about adding explicit method to ConstraintViolationListInterface for accessing all items as array/iterator. It's not crucial now, as you pointed out how it can be accessed on language level, but for beginners it might not be as obvious 🙂
Symfony version(s) affected: 5.1.5
Description
Currently
Symfony\Component\Validator\ConstraintViolationListInterface
does not offer method for accessing iterator. There isgetIterator()
as a partConstraintViolationList
concrete implementation which also implementsIteratorAggregate
interface, howeverSymfony\Component\Messenger\Exception\ValidationFailedException::getViolations()
return type points to interface, not implementation. In result, it's "impossible" to access iterator when type-hinting toConstraintViolationListInterface
interface.How to reproduce
Possible Solution
Move
implements \IteratorAggregate
fromConstraintViolationList
toConstraintViolationListInterface
(asextends \IteratorAggregate
, obviously). I've tried it locally and Psalm doesn't raise an error.Additional context
In Symfony app it works (because of implementation), the problem is with static analysis.
The text was updated successfully, but these errors were encountered: