|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Strategies\BodyParameters; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
use Illuminate\Routing\Route; |
|
8
|
|
|
use Mpociot\Reflection\DocBlock; |
|
9
|
|
|
use Mpociot\Reflection\DocBlock\Tag; |
|
10
|
|
|
use Mpociot\ApiDoc\Strategies\Strategy; |
|
11
|
|
|
use Mpociot\ApiDoc\Tools\RouteDocBlocker; |
|
12
|
|
|
use Mpociot\ApiDoc\Tools\Traits\ParamHelpers; |
|
13
|
|
|
use Dingo\Api\Http\FormRequest as DingoFormRequest; |
|
14
|
|
|
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest; |
|
15
|
|
|
|
|
16
|
|
|
class GetFromDocBlocks extends Strategy |
|
17
|
|
|
{ |
|
18
|
|
|
use ParamHelpers; |
|
19
|
|
|
|
|
20
|
|
View Code Duplication |
public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
foreach ($method->getParameters() as $param) { |
|
23
|
|
|
$paramType = $param->getType(); |
|
24
|
|
|
if ($paramType === null) { |
|
25
|
|
|
continue; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$parameterClassName = version_compare(phpversion(), '7.1.0', '<') |
|
29
|
|
|
? $paramType->__toString() |
|
30
|
|
|
: $paramType->getName(); |
|
31
|
|
|
|
|
32
|
|
|
try { |
|
33
|
|
|
$parameterClass = new ReflectionClass($parameterClassName); |
|
34
|
|
|
} catch (\ReflectionException $e) { |
|
35
|
|
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// If there's a FormRequest, we check there for @bodyParam tags. |
|
39
|
|
|
if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class) |
|
40
|
|
|
|| class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) { |
|
41
|
|
|
$formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
|
42
|
|
|
$bodyParametersFromDocBlock = $this->getBodyParametersFromDocBlock($formRequestDocBlock->getTags()); |
|
43
|
|
|
|
|
44
|
|
|
if (count($bodyParametersFromDocBlock)) { |
|
45
|
|
|
return $bodyParametersFromDocBlock; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; |
|
51
|
|
|
|
|
52
|
|
|
return $this->getBodyParametersFromDocBlock($methodDocBlock->getTags()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getBodyParametersFromDocBlock($tags) |
|
56
|
|
|
{ |
|
57
|
|
|
$parameters = collect($tags) |
|
58
|
|
|
->filter(function ($tag) { |
|
59
|
|
|
return $tag instanceof Tag && $tag->getName() === 'bodyParam'; |
|
|
|
|
|
|
60
|
|
|
}) |
|
61
|
|
|
->mapWithKeys(function ($tag) { |
|
62
|
|
|
preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
|
63
|
|
|
$content = preg_replace('/\s?No-example.?/', '', $content); |
|
64
|
|
View Code Duplication |
if (empty($content)) { |
|
|
|
|
|
|
65
|
|
|
// this means only name and type were supplied |
|
66
|
|
|
list($name, $type) = preg_split('/\s+/', $tag->getContent()); |
|
67
|
|
|
$required = false; |
|
68
|
|
|
$description = ''; |
|
69
|
|
|
} else { |
|
70
|
|
|
list($_, $name, $type, $required, $description) = $content; |
|
|
|
|
|
|
71
|
|
|
$description = trim($description); |
|
72
|
|
|
if ($description == 'required' && empty(trim($required))) { |
|
73
|
|
|
$required = $description; |
|
74
|
|
|
$description = ''; |
|
75
|
|
|
} |
|
76
|
|
|
$required = trim($required) == 'required' ? true : false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$type = $this->normalizeParameterType($type); |
|
80
|
|
|
list($description, $example) = $this->parseParamDescription($description, $type); |
|
81
|
|
|
$value = is_null($example) && ! $this->shouldExcludeExample($tag) ? $this->generateDummyValue($type) : $example; |
|
82
|
|
|
|
|
83
|
|
|
return [$name => compact('type', 'description', 'required', 'value')]; |
|
84
|
|
|
})->toArray(); |
|
85
|
|
|
|
|
86
|
|
|
return $parameters; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.