12
12
use ApiClients \Tools \OpenApiClientGenerator \Generator \Client \Routers ;
13
13
use ApiClients \Tools \OpenApiClientGenerator \Generator \Client \Routers \RouterClass ;
14
14
use ApiClients \Tools \OpenApiClientGenerator \Generator \Helper \Operation ;
15
+ use ApiClients \Tools \OpenApiClientGenerator \Generator \Helper \Types ;
15
16
use ApiClients \Tools \OpenApiClientGenerator \PrivatePromotedPropertyAsParam ;
16
17
use ApiClients \Tools \OpenApiClientGenerator \Representation ;
17
18
use ApiClients \Tools \OpenApiClientGenerator \Utils ;
25
26
use PhpParser \Node ;
26
27
use PhpParser \Node \Arg ;
27
28
use PhpParser \Node \Expr ;
29
+ use PhpParser \Node \Name ;
30
+ use PhpParser \Node \UnionType ;
28
31
use React \EventLoop \Loop ;
29
32
use React \Http \Browser ;
30
33
use ReflectionClass ;
@@ -326,7 +329,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
326
329
array_map (
327
330
'trim ' ,
328
331
array_unique (
329
- $ returnTypes ,
332
+ [...Types:: filterDuplicatesAndIncompatibleRawTypes (... $ returnTypes)] ,
330
333
),
331
334
),
332
335
);
@@ -336,7 +339,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
336
339
'trim ' ,
337
340
array_filter (
338
341
array_unique (
339
- $ returnTypes ,
342
+ [...Types:: filterDuplicatesAndIncompatibleRawTypes (... $ returnTypes)] ,
340
343
),
341
344
static fn (string $ type ): bool => $ type !== 'void ' ,
342
345
),
@@ -494,7 +497,6 @@ public static function generate(Configuration $configuration, string $pathPrefix
494
497
],
495
498
),
496
499
),
497
- // ...($returnTypesUnfilterred === 'void' ? [new Node\Stmt\Return_()] : []),
498
500
],
499
501
];
500
502
}
@@ -553,11 +555,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
553
555
$ left = '' ;
554
556
$ right = '' ;
555
557
for ($ i = 0 ; $ i < $ count ; $ i ++) {
556
- $ returnType = implode ('| ' , [
557
- ...($ operations [$ i ]->matchMethod === 'STREAM ' ? ['iterable<string> ' ] : []),
558
- ...array_unique ($ operations [$ i ]->returnType ),
559
- ]);
560
- $ returnType = ($ operations [$ i ]->matchMethod === 'LIST ' ? 'iterable< ' . $ returnType . '> ' : $ returnType );
558
+ $ returnType = Operation::getDocBlockResultTypeFromOperation ($ operations [$ i ]);
561
559
if ($ i !== $ lastItem ) {
562
560
$ left .= '($call is Operation \\' . $ operations [$ i ]->classNameSanitized ->relative . '::OPERATION_MATCH ? ' . $ returnType . ' : ' ;
563
561
} else {
@@ -572,7 +570,22 @@ public static function generate(Configuration $configuration, string $pathPrefix
572
570
' */ ' ,
573
571
'// phpcs:enable ' ,
574
572
])),
575
- )->addParam ((new Param ('call ' ))->setType ('string ' ))->addParam ((new Param ('params ' ))->setType ('array ' )->setDefault ([]))->addStmt (
573
+ )->addParam ((new Param ('call ' ))->setType ('string ' ))->addParam ((new Param ('params ' ))->setType ('array ' )->setDefault ([]))->setReturnType (
574
+ new UnionType (
575
+ array_map (
576
+ static fn (string $ type ): Name => new Name ($ type ),
577
+ array_unique (
578
+ [
579
+ ...Types::filterDuplicatesAndIncompatibleRawTypes (...(static function (array $ operations ): iterable {
580
+ foreach ($ operations as $ operation ) {
581
+ yield from explode ('| ' , Operation::getResultTypeFromOperation ($ operation ));
582
+ }
583
+ })($ operations )),
584
+ ],
585
+ ),
586
+ ),
587
+ ),
588
+ )->addStmt (
576
589
new Node \Expr \Assign (
577
590
new Node \Expr \Array_ ([
578
591
new Node \Expr \ArrayItem (
@@ -743,6 +756,19 @@ private static function traverseOperationPaths(array $operations, array &$operat
743
756
return $ operations ;
744
757
}
745
758
759
+ /** @param array<Representation\Path> $paths */
760
+ private static function operationsInThisThree (array $ paths , int $ level , Routers $ routers ): iterable
761
+ {
762
+ foreach ($ paths as $ path ) {
763
+ yield from $ path ['operations ' ];
764
+ yield from self ::operationsInThisThree (
765
+ $ path ['paths ' ], /** @phpstan-ignore-line */
766
+ $ level + 1 ,
767
+ $ routers ,
768
+ );
769
+ }
770
+ }
771
+
746
772
/**
747
773
* @param array<Representation\Operation> $operations
748
774
* @param array<Representation\Path> $paths
@@ -760,7 +786,20 @@ private static function traverseOperations(array $operations, array $paths, int
760
786
$ nonArgumentPathChunks [] = new Node \Expr \ArrayItem (new Node \Scalar \String_ ($ pathChunk ));
761
787
}
762
788
789
+ $ opsInTree = [
790
+ ...self ::operationsInThisThree (
791
+ $ paths ,
792
+ $ level ,
793
+ $ routers ,
794
+ ),
795
+ ];
796
+
763
797
$ ifs = [];
798
+
799
+ // if (count($opsIntree) === 13) {
800
+ // $operations = [...$operations, ...$opsIntree];
801
+ // }
802
+
764
803
foreach ($ operations as $ operation ) {
765
804
$ ifs [] = [
766
805
new Node \Expr \BinaryOp \Equal (
@@ -774,6 +813,7 @@ private static function traverseOperations(array $operations, array $paths, int
774
813
];
775
814
}
776
815
816
+ // if (count($opsIntree) > 13) {
777
817
foreach ($ paths as $ pathChunk => $ path ) {
778
818
$ ifs [] = [
779
819
new Node \Expr \BinaryOp \Equal (
@@ -784,14 +824,16 @@ private static function traverseOperations(array $operations, array $paths, int
784
824
new Node \Scalar \String_ ($ pathChunk ),
785
825
),
786
826
self ::traverseOperations (
787
- $ path ['operations ' ], /** @phpstan-ignore-line */
827
+ $ path ['operations ' ], /** @phpstan-ignore-line */
788
828
$ path ['paths ' ], /** @phpstan-ignore-line */
789
829
$ level + 1 ,
790
830
$ routers ,
791
831
),
792
832
];
793
833
}
794
834
835
+ // }
836
+
795
837
if (count ($ ifs ) === 0 ) {
796
838
return [];
797
839
}
@@ -816,7 +858,17 @@ private static function traverseOperations(array $operations, array $paths, int
816
858
/** @return array<Node\Stmt> */
817
859
private static function callOperation (Routers $ routers , Representation \Operation $ operation , Representation \Path $ path ): array
818
860
{
819
- $ returnType = Operation::getResultTypeFromOperation ($ operation );
861
+ $ returnType = implode (
862
+ '| ' ,
863
+ [
864
+ ...Types::filterDuplicatesAndIncompatibleRawTypes (
865
+ ...explode (
866
+ '| ' ,
867
+ Operation::getResultTypeFromOperation ($ operation ),
868
+ ),
869
+ ),
870
+ ],
871
+ );
820
872
$ router = $ routers ->add (
821
873
$ operation ->method ,
822
874
$ operation ->group ,
0 commit comments