16
16
use ReflectionParameter ;
17
17
use ReflectionUnionType ;
18
18
19
+ use function array_map ;
19
20
use function base_path ;
20
21
use function class_exists ;
21
22
use function collect ;
@@ -33,12 +34,23 @@ class GenerateHelperCommand extends Command
33
34
34
35
protected $ description = 'Generates correct PHPDocs for Http facade macros ' ;
35
36
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
+
36
48
public function handle (): void
37
49
{
38
50
$ this ->sections ()->map (function (array $ macros , string $ section ) {
39
51
intro ($ section );
40
52
41
- return $ this ->macros ($ macros );
53
+ return $ this ->macros ($ macros, $ section );
42
54
})
43
55
->tap (fn () => outro ('storing ' ))
44
56
->tap (fn () => $ this ->cleanUp ())
@@ -53,13 +65,13 @@ protected function cleanUp(): void
53
65
$ this ->components ->task ('clean up ' , fn () => Directory::ensureDelete ($ this ->directory ()));
54
66
}
55
67
56
- protected function macros (array $ macros ): Collection
68
+ protected function macros (array $ macros, string $ section ): Collection
57
69
{
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 ) {
59
71
$ name = $ this ->resolveName ($ macro , $ name );
60
72
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 ());
63
75
});
64
76
65
77
return $ result ;
@@ -83,37 +95,30 @@ protected function compileBlocks(string $section, Collection $blocks): string
83
95
);
84
96
}
85
97
86
- protected function prepare (string $ name , array $ functions ): array
98
+ protected function prepare (string $ section , string $ name , Closure $ callback ): array
87
99
{
88
- return $ this ->docBlock ($ name , $ this ->docBlockParameters ($ functions ));
100
+ return $ this ->docBlock ($ section , $ name , $ this ->docBlockParameters ($ callback ));
89
101
}
90
102
91
- protected function docBlock (string $ name , string $ parameters ): array
103
+ protected function docBlock (string $ section , string $ name , string $ parameters ): array
92
104
{
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 ]);
97
106
}
98
107
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
105
109
{
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 ' ;
108
113
109
- $ result .= ' $ ' . $ parameter ->getName ();
114
+ $ result .= ' $ ' . $ parameter ->getName ();
110
115
111
- if ($ parameter ->isDefaultValueAvailable ()) {
112
- $ result .= ' = ' . var_export ($ parameter ->getDefaultValue (), true );
113
- }
116
+ if ($ parameter ->isDefaultValueAvailable ()) {
117
+ $ result .= ' = ' . var_export ($ parameter ->getDefaultValue (), true );
118
+ }
114
119
115
- return $ result ;
116
- })->implode (', ' );
120
+ return $ result ;
121
+ })->implode (', ' );
117
122
}
118
123
119
124
protected function compactTypes (ReflectionNamedType |ReflectionUnionType $ type ): string
0 commit comments