Skip to content

Commit 319837e

Browse files
Merge pull request rakutentech#21 from Mello21century/bug-trowable-request-fix
Fix in exception in form request classes if the php failed to get rules
2 parents 807980f + 70d67c6 commit 319837e

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/LaravelRequestDocs.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getDocs()
3737
return array_filter($docs);
3838
}
3939

40-
public function sortDocs(array $docs, $sortBy = 'default'): Array
40+
public function sortDocs(array $docs, $sortBy = 'default'): array
4141
{
4242
if ($sortBy === 'route_names') {
4343
sort($docs);
@@ -72,7 +72,7 @@ public function sortDocs(array $docs, $sortBy = 'default'): Array
7272
return $sorted;
7373
}
7474

75-
public function getControllersInfo(): Array
75+
public function getControllersInfo(): array
7676
{
7777
$controllersInfo = [];
7878
$routes = collect(Route::getRoutes());
@@ -105,7 +105,7 @@ public function getControllersInfo(): Array
105105
return $controllersInfo;
106106
}
107107

108-
public function appendRequestRules(Array $controllersInfo)
108+
public function appendRequestRules(array $controllersInfo)
109109
{
110110
foreach ($controllersInfo as $index => $controllerInfo) {
111111
$controller = $controllerInfo['controller_full_path'];
@@ -125,7 +125,11 @@ public function appendRequestRules(Array $controllersInfo)
125125
//throw $th;
126126
}
127127
if ($requestClass instanceof FormRequest) {
128-
$controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules());
128+
try {
129+
$controllersInfo[$index]['rules'] = $this->flattenRules($requestClass->rules());
130+
} catch (\ErrorException $th) {
131+
$controllerInfo[$index]['rules'] = $this->rulesByRegex($requestClassName);
132+
}
129133
$controllersInfo[$index]['docBlock'] = $this->lrdDocComment($reflectionMethod->getDocComment());
130134
}
131135
}
@@ -184,4 +188,34 @@ public function flattenRules($mixedRules)
184188

185189
return $rules;
186190
}
191+
192+
public function rulesByRegex($requestClassName)
193+
{
194+
$data = new ReflectionMethod($requestClassName, 'rules');
195+
$lines = file($data->getFileName());
196+
$rules = [];
197+
for ($i = $data->getStartLine() - 1; $i <= $data->getEndLine() - 1; $i++) {
198+
preg_match_all("/(?:'|\").*?(?:'|\")/", $lines[$i], $matches);
199+
$rules[] = $matches;
200+
}
201+
202+
$rules = collect($rules)
203+
->filter(function ($item) {
204+
return count($item[0]) > 0;
205+
})
206+
->transform(function ($item) {
207+
$fieldName = Str::of($item[0][0])->replace(['"', "'"], '');
208+
$definedFieldRules = collect(array_slice($item[0], 1))->transform(function ($rule) {
209+
return Str::of($rule)->replace(['"', "'"], '')->__toString();
210+
})->toArray();
211+
212+
return ['key' => $fieldName, 'rules' => $definedFieldRules];
213+
})
214+
->keyBy('key')
215+
->transform(function ($item) {
216+
return $item['rules'];
217+
})->toArray();
218+
219+
return $rules;
220+
}
187221
}

0 commit comments

Comments
 (0)