File tree Expand file tree Collapse file tree 4 files changed +47
-15
lines changed Expand file tree Collapse file tree 4 files changed +47
-15
lines changed Original file line number Diff line number Diff line change @@ -696,22 +696,8 @@ static function (string $variance): TemplateTypeVariance {
696696return new ErrorType ();
697697} elseif ($ mainTypeName === 'value-of ' ) {
698698if (count ($ genericTypes ) === 1 ) { // value-of<ValueType>
699- $ genericType = $ genericTypes [0 ];
700- if ($ genericType ->isEnum ()->yes ()) {
701- $ valueTypes = [];
702- foreach ($ genericType ->getEnumCases () as $ enumCase ) {
703- $ valueType = $ enumCase ->getBackingValueType ();
704- if ($ valueType === null ) {
705- continue ;
706- }
707-
708- $ valueTypes [] = $ valueType ;
709- }
710-
711- return TypeCombinator::union (...$ valueTypes );
712- }
699+ $ type = new ValueOfType ($ genericTypes [0 ]);
713700
714- $ type = new ValueOfType ($ genericType );
715701return $ type ->isResolvable () ? $ type ->resolve () : $ type ;
716702}
717703
Original file line number Diff line number Diff line change @@ -49,6 +49,20 @@ public function isResolvable(): bool
4949
5050protected function getResult (): Type
5151{
52+ if ($ this ->type ->isEnum ()->yes ()) {
53+ $ valueTypes = [];
54+ foreach ($ this ->type ->getEnumCases () as $ enumCase ) {
55+ $ valueType = $ enumCase ->getBackingValueType ();
56+ if ($ valueType === null ) {
57+ continue ;
58+ }
59+
60+ $ valueTypes [] = $ valueType ;
61+ }
62+
63+ return TypeCombinator::union (...$ valueTypes );
64+ }
65+
5266return $ this ->type ->getIterableValueType ();
5367}
5468
Original file line number Diff line number Diff line change @@ -334,6 +334,11 @@ public function dataFileAsserts(): iterable
334334yield from $ this ->gatherAssertTypes (__DIR__ . '/data/array-sum.php ' );
335335yield from $ this ->gatherAssertTypes (__DIR__ . '/data/array-plus.php ' );
336336yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-4573.php ' );
337+
338+ if (PHP_VERSION_ID >= 80100 ) {
339+ yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-9881.php ' );
340+ }
341+
337342yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-4577.php ' );
338343yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-4579.php ' );
339344yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-3321.php ' );
Original file line number Diff line number Diff line change 1+ <?php // lint >= 8.1
2+
3+ namespace Bug9881 ;
4+
5+ use BackedEnum ;
6+ use function PHPStan \Testing \assertType ;
7+
8+ class HelloWorld
9+ {
10+ /**
11+ * @template B of BackedEnum
12+ * @param B[] $enums
13+ * @return value-of<B>[]
14+ */
15+ public static function arrayEnumToStrings (array $ enums ): array
16+ {
17+ return array_map (static fn (BackedEnum $ code ): string |int => $ code ->value , $ enums );
18+ }
19+ }
20+
21+ enum Test: string {
22+ case DA = 'da ' ;
23+ }
24+
25+ function (Test ...$ da ): void {
26+ assertType ('array< \'da \'> ' , HelloWorld::arrayEnumToStrings ($ da ));
27+ };
You can’t perform that action at this time.
0 commit comments