1616use ReflectionParameter ;
1717use ReflectionUnionType ;
1818
19+ use function array_map ;
1920use function base_path ;
2021use function class_exists ;
2122use function collect ;
@@ -33,12 +34,23 @@ class GenerateHelperCommand extends Command
3334
3435 protected $ description = 'Generates correct PHPDocs for Http facade macros ' ;
3536
37+ protected array $ docBlocks = [
38+ 'request ' => [
39+ ' * @method $this %s(%s) ' ,
40+ ' * @method static $this %s(%s) ' ,
41+ ],
42+ 'response ' => [
43+ ' * @method mixed %s(%s) ' ,
44+ ' * @method static mixed %s(%s) ' ,
45+ ],
46+ ];
47+
3648 public function handle (): void
3749 {
3850 $ this ->sections ()->map (function (array $ macros , string $ section ) {
3951 intro ($ section );
4052
41- return $ this ->macros ($ macros );
53+ return $ this ->macros ($ macros, $ section );
4254 })
4355 ->tap (fn () => outro ('storing ' ))
4456 ->tap (fn () => $ this ->cleanUp ())
@@ -53,13 +65,13 @@ protected function cleanUp(): void
5365 $ this ->components ->task ('clean up ' , fn () => Directory::ensureDelete ($ this ->directory ()));
5466 }
5567
56- protected function macros (array $ macros ): Collection
68+ protected function macros (array $ macros, string $ section ): Collection
5769 {
58- return collect ($ macros )->map (function (Macro |string $ macro , int |string $ name ) {
70+ return collect ($ macros )->map (function (Macro |string $ macro , int |string $ name ) use ( $ section ) {
5971 $ name = $ this ->resolveName ($ macro , $ name );
6072
61- $ this ->components ->task ($ name , function () use ($ macro , $ name , &$ result ) {
62- $ result = $ this ->prepare ($ name , $ this -> reflectionCallback ( $ macro ::callback ())-> getParameters ());
73+ $ this ->components ->task ($ name , function () use ($ macro , $ name , $ section , &$ result ) {
74+ $ result = $ this ->prepare ($ section , $ name , $ macro ::callback ());
6375 });
6476
6577 return $ result ;
@@ -83,37 +95,30 @@ protected function compileBlocks(string $section, Collection $blocks): string
8395 );
8496 }
8597
86- protected function prepare (string $ name , array $ functions ): array
98+ protected function prepare (string $ section , string $ name , Closure $ callback ): array
8799 {
88- return $ this ->docBlock ($ name , $ this ->docBlockParameters ($ functions ));
100+ return $ this ->docBlock ($ section , $ name , $ this ->docBlockParameters ($ callback ));
89101 }
90102
91- protected function docBlock (string $ name , string $ parameters ): array
103+ protected function docBlock (string $ section , string $ name , string $ parameters ): array
92104 {
93- return [
94- sprintf (' * @method $this %s(%s) ' , $ name , $ parameters ),
95- sprintf (' * @method static $this %s(%s) ' , $ name , $ parameters ),
96- ];
105+ return array_map (fn (string $ template ) => sprintf ($ template , $ name , $ parameters ), $ this ->docBlocks [$ section ]);
97106 }
98107
99- /**
100- * @param array<ReflectionParameter> $functions
101- *
102- * @return Collection
103- */
104- protected function docBlockParameters (array $ functions ): string
108+ protected function docBlockParameters (Closure $ callback ): string
105109 {
106- return collect ($ functions )->map (function (ReflectionParameter $ parameter ) {
107- $ result = $ parameter ->hasType () ? $ this ->compactTypes ($ parameter ->getType ()) : 'mixed ' ;
110+ return collect ($ this ->reflectionCallback ($ callback )->getParameters ())
111+ ->map (function (ReflectionParameter $ parameter ) {
112+ $ result = $ parameter ->hasType () ? $ this ->compactTypes ($ parameter ->getType ()) : 'mixed ' ;
108113
109- $ result .= ' $ ' . $ parameter ->getName ();
114+ $ result .= ' $ ' . $ parameter ->getName ();
110115
111- if ($ parameter ->isDefaultValueAvailable ()) {
112- $ result .= ' = ' . var_export ($ parameter ->getDefaultValue (), true );
113- }
116+ if ($ parameter ->isDefaultValueAvailable ()) {
117+ $ result .= ' = ' . var_export ($ parameter ->getDefaultValue (), true );
118+ }
114119
115- return $ result ;
116- })->implode (', ' );
120+ return $ result ;
121+ })->implode (', ' );
117122 }
118123
119124 protected function compactTypes (ReflectionNamedType |ReflectionUnionType $ type ): string
0 commit comments