@@ -29,7 +29,7 @@ public function getDocs(
29
29
bool $ showDelete ,
30
30
bool $ showHead
31
31
): array {
32
- $ methods = array_filter ([
32
+ $ filteredMethods = array_filter ([
33
33
Request::METHOD_GET => $ showGet ,
34
34
Request::METHOD_POST => $ showPost ,
35
35
Request::METHOD_PUT => $ showPut ,
@@ -40,7 +40,10 @@ public function getDocs(
40
40
return $ method ;
41
41
});
42
42
43
- $ docs = $ this ->getControllersInfo (array_keys ($ methods ));
43
+ /** @var string[] $methods */
44
+ $ methods = array_keys ($ filteredMethods );
45
+
46
+ $ docs = $ this ->getControllersInfo ($ methods );
44
47
$ docs = $ this ->appendRequestRules ($ docs );
45
48
46
49
return array_filter ($ docs );
@@ -58,14 +61,10 @@ public function splitByMethods(array $docs): array
58
61
$ splitDocs = [];
59
62
60
63
foreach ($ docs as $ doc ) {
61
- if (count ($ doc ->getMethods ()) === 1 ) {
62
- $ splitDocs [] = $ doc ;
63
- continue ;
64
- }
65
-
66
64
foreach ($ doc ->getMethods () as $ method ) {
67
65
$ cloned = $ doc ->clone ();
68
66
$ cloned ->setMethods ([$ method ]);
67
+ $ cloned ->setHttpMethod ($ method );
69
68
$ splitDocs [] = $ cloned ;
70
69
}
71
70
}
@@ -106,65 +105,6 @@ public function sortDocs(array $docs, ?string $sortBy = 'default'): array
106
105
return $ sorted ->values ()->all ();
107
106
}
108
107
109
- /**
110
- * @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
111
- * @return \Rakutentech\LaravelRequestDocs\Doc[]
112
- */
113
- public function filterByMethods (array $ docs , bool $ get , bool $ post , bool $ put , bool $ patch , bool $ delete , bool $ head ): array
114
- {
115
- $ filtered = [];
116
-
117
- foreach ($ docs as $ doc ) {
118
- if ($ get && in_array ('GET ' , $ doc ->getMethods ())) {
119
- $ clone = clone $ doc ;
120
- $ clone ->setMethods (['GET ' ]);
121
- $ filtered [] = $ clone ;
122
- }
123
- }
124
-
125
- foreach ($ docs as $ doc ) {
126
- if ($ post && in_array ('POST ' , $ doc ->getMethods ())) {
127
- $ clone = clone $ doc ;
128
- $ clone ->setMethods (['POST ' ]);
129
- $ filtered [] = $ clone ;
130
- }
131
- }
132
-
133
- foreach ($ docs as $ doc ) {
134
- if ($ put && in_array ('PUT ' , $ doc ->getMethods ())) {
135
- $ clone = clone $ doc ;
136
- $ clone ->setMethods (['PUT ' ]);
137
- $ filtered [] = $ clone ;
138
- }
139
- }
140
-
141
- foreach ($ docs as $ doc ) {
142
- if ($ patch && in_array ('PATCH ' , $ doc ->getMethods ())) {
143
- $ clone = clone $ doc ;
144
- $ clone ->setMethods (['PATCH ' ]);
145
- $ filtered [] = $ clone ;
146
- }
147
- }
148
-
149
- foreach ($ docs as $ doc ) {
150
- if ($ delete && in_array ('DELETE ' , $ doc ->getMethods ())) {
151
- $ clone = clone $ doc ;
152
- $ clone ->setMethods (['DELETE ' ]);
153
- $ filtered [] = $ clone ;
154
- }
155
- }
156
-
157
- foreach ($ docs as $ doc ) {
158
- if ($ head && in_array ('HEAD ' , $ doc ->getMethods ())) {
159
- $ clone = clone $ doc ;
160
- $ clone ->setMethods (['HEAD ' ]);
161
- $ filtered [] = $ clone ;
162
- }
163
- }
164
-
165
- return $ filtered ;
166
- }
167
-
168
108
/**
169
109
* @param \Rakutentech\LaravelRequestDocs\Doc[] $docs
170
110
* @return \Rakutentech\LaravelRequestDocs\Doc[]
@@ -216,15 +156,12 @@ public function getControllersInfo(array $onlyMethods): array
216
156
}
217
157
}
218
158
219
- $ httpMethod = $ route ->methods [ 0 ] ;
159
+ $ routeMethods = array_intersect ( $ route ->methods , $ onlyMethods ) ;
220
160
221
- if (! in_array ( $ httpMethod , $ onlyMethods )) {
161
+ if (empty ( $ routeMethods )) {
222
162
continue ;
223
163
}
224
164
225
- // Exclude from `$route->methods` which is not in `$onlyMethods`.
226
- $ routeMethods = array_intersect ($ route ->methods , $ onlyMethods );
227
-
228
165
$ controllerName = '' ;
229
166
$ controllerFullPath = '' ;
230
167
$ method = '' ;
@@ -237,21 +174,14 @@ public function getControllersInfo(array $onlyMethods): array
237
174
$ controllerName = (new ReflectionClass ($ controllerFullPath ))->getShortName ();
238
175
}
239
176
240
- foreach ($ docs as $ doc ) {
241
- if ($ doc ->getUri () === $ route ->uri && $ doc ->getHttpMethod () === $ httpMethod ) {
242
- // is duplicate
243
- continue 2 ;
244
- }
245
- }
246
-
247
177
$ doc = new Doc (
248
178
$ route ->uri ,
249
179
$ routeMethods ,
250
180
config ('request-docs.hide_meta_data ' ) ? [] : $ route ->middleware (),
251
181
config ('request-docs.hide_meta_data ' ) ? '' : $ controllerName ,
252
182
config ('request-docs.hide_meta_data ' ) ? '' : $ controllerFullPath ,
253
183
config ('request-docs.hide_meta_data ' ) ? '' : $ method ,
254
- $ httpMethod ,
184
+ '' ,
255
185
[],
256
186
'' ,
257
187
);
@@ -303,10 +233,6 @@ public function appendRequestRules(array $controllersInfo): array
303
233
$ controllerInfo ->mergeRules ($ this ->flattenRules ($ requestObject ->$ requestMethod ()));
304
234
} catch (Throwable $ e ) {
305
235
$ controllerInfo ->mergeRules ($ this ->rulesByRegex ($ requestClassName , $ requestMethod ));
306
-
307
- if (config ('request-docs.debug ' )) {
308
- throw $ e ;
309
- }
310
236
}
311
237
}
312
238
} catch (Throwable $ th ) {
@@ -389,7 +315,7 @@ public function rulesByRegex(string $requestClassName, string $methodName): arra
389
315
// check if line is a comment
390
316
$ trimmed = trim ($ lines [$ i ]);
391
317
if (Str::startsWith ($ trimmed , '// ' ) || Str::startsWith ($ trimmed , '# ' )) {
392
- continue ;
318
+ continue ; // @codeCoverageIgnore
393
319
}
394
320
// check if => in string, only pick up rules that are coded on single line
395
321
if (Str::contains ($ lines [$ i ], '=> ' )) {
0 commit comments