Skip to content

Commit 56af625

Browse files
committed
SmartArray, SmartArrayHtml: point @implements at the real interface
The foreach type hints annotated Iterator, but the classes implement IteratorAggregate, so only PhpStorm (which is lenient about the interface name) could use them. Now they annotate IteratorAggregate, which PHPStan and Psalm read too. Still one @implements line per object type rather than a union: PhpStorm keeps only one object member per generic union, and the HTML mode needs both SmartArrayHtml and SmartString to autocomplete. Verified in PhpStorm 2025.3.1; the scalar union collapses to mixed in hovers, which is cosmetic. Also collapses the raw side's five scalar lines into one.
1 parent 4e1f788 commit 56af625

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

src/SmartArray.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Itools\SmartArray;
55

66
use InvalidArgumentException;
7+
use IteratorAggregate;
78
use Itools\SmartString\SmartString;
89
use JetBrains\PhpStorm\Deprecated;
910

@@ -21,13 +22,10 @@
2122
* mode narrows the return type (raw values here, SmartStrings in SmartArrayHtml);
2223
* only new(), asRaw(), and asHtml() have per-class behavior.
2324
*
24-
* PhpStorm 2025.3.1: Repeated "@implements" needed - union types in Iterator generics don't work reliably for foreach inference
25-
* @implements \Iterator<mixed, SmartArray>
26-
* @implements \Iterator<mixed, string>
27-
* @implements \Iterator<mixed, int>
28-
* @implements \Iterator<mixed, float>
29-
* @implements \Iterator<mixed, bool>
30-
* @implements \Iterator<mixed, null>
25+
* PhpStorm: repeated single-type @implements lines - it keeps only one object
26+
* member per generic union, so foreach over a union loses the second type
27+
* @implements IteratorAggregate<mixed, SmartArray>
28+
* @implements IteratorAggregate<mixed, string|int|float|bool|null>
3129
*/
3230
class SmartArray extends SmartArrayBase
3331
{

src/SmartArrayHtml.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Itools\SmartArray;
44

55
use InvalidArgumentException;
6-
use Iterator;
6+
use IteratorAggregate;
77
use Itools\SmartString\SmartString;
88
use JetBrains\PhpStorm\Deprecated;
99

@@ -21,9 +21,10 @@
2121
* mode narrows the return type (SmartStrings here, raw values in SmartArray);
2222
* only new(), asRaw(), and asHtml() have per-class behavior.
2323
*
24-
* PhpStorm 2025.3.1: Repeated "@implements" needed - union types in Iterator generics don't work reliably for foreach inference
25-
* @implements Iterator<mixed, SmartString>
26-
* @implements Iterator<mixed, SmartArrayHtml>
24+
* PhpStorm: repeated single-type @implements lines - it keeps only one object
25+
* member per generic union, so foreach over a union loses the second type
26+
* @implements IteratorAggregate<mixed, SmartArrayHtml>
27+
* @implements IteratorAggregate<mixed, SmartString>
2728
*/
2829
class SmartArrayHtml extends SmartArrayBase
2930
{

0 commit comments

Comments
 (0)