Skip to content

Commit fc4700f

Browse files
committed
bug #45696 Make FormErrorIterator generic (VincentLanglet)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Make FormErrorIterator generic | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes/no | New feature? | yes/no | Deprecations? | no | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> I target 5.4, since it's a follow up of #45322 which introduced some errors in my code. It allows to typehint the `Form::getErrors` method to return `FormErrorIterator<FormError>` most of the time (which is possible by psalm/phpstan plugins with ReturnTypeProviders). Commits ------- 08ddac5 Make FormErrorIterator generic
2 parents b7e878c + 08ddac5 commit fc4700f

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/Symfony/Component/Form/FormErrorIterator.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
*
3030
* @author Bernhard Schussek <[email protected]>
3131
*
32-
* @implements \ArrayAccess<int, FormError|FormErrorIterator>
33-
* @implements \RecursiveIterator<int, FormError|FormErrorIterator>
34-
* @implements \SeekableIterator<int, FormError|FormErrorIterator>
32+
* @template T of FormError|FormErrorIterator
33+
*
34+
* @implements \ArrayAccess<int, T>
35+
* @implements \RecursiveIterator<int, T>
36+
* @implements \SeekableIterator<int, T>
3537
*/
3638
class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \ArrayAccess, \Countable
3739
{
@@ -41,10 +43,14 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
4143
public const INDENTATION = ' ';
4244

4345
private $form;
46+
47+
/**
48+
* @var list<T>
49+
*/
4450
private $errors;
4551

4652
/**
47-
* @param list<FormError|self> $errors
53+
* @param list<T> $errors
4854
*
4955
* @throws InvalidArgumentException If the errors are invalid
5056
*/
@@ -74,7 +80,7 @@ public function __toString()
7480
$string .= 'ERROR: '.$error->getMessage()."\n";
7581
} else {
7682
/* @var self $error */
77-
$string .= $error->form->getName().":\n";
83+
$string .= $error->getForm()->getName().":\n";
7884
$string .= self::indent((string) $error);
7985
}
8086
}
@@ -95,7 +101,7 @@ public function getForm()
95101
/**
96102
* Returns the current element of the iterator.
97103
*
98-
* @return FormError|self An error or an iterator containing nested errors
104+
* @return T An error or an iterator containing nested errors
99105
*/
100106
#[\ReturnTypeWillChange]
101107
public function current()
@@ -164,7 +170,7 @@ public function offsetExists($position)
164170
*
165171
* @param int $position The position
166172
*
167-
* @return FormError|FormErrorIterator
173+
* @return T
168174
*
169175
* @throws OutOfBoundsException If the given position does not exist
170176
*/
@@ -227,7 +233,10 @@ public function getChildren()
227233
// throw new LogicException(sprintf('The current element is not iterable. Use "%s" to get the current element.', self::class.'::current()'));
228234
}
229235

230-
return current($this->errors);
236+
/** @var self $children */
237+
$children = current($this->errors);
238+
239+
return $children;
231240
}
232241

233242
/**

0 commit comments

Comments
 (0)