|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Strategies\QueryParameters; |
|
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 @queryParam 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
|
|
|
$queryParametersFromDocBlock = $this->getqueryParametersFromDocBlock($formRequestDocBlock->getTags()); |
|
43
|
|
|
|
|
44
|
|
|
if (count($queryParametersFromDocBlock)) { |
|
45
|
|
|
return $queryParametersFromDocBlock; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; |
|
51
|
|
|
|
|
52
|
|
|
return $this->getqueryParametersFromDocBlock($methodDocBlock->getTags()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getQueryParametersFromDocBlock($tags) |
|
56
|
|
|
{ |
|
57
|
|
|
$parameters = collect($tags) |
|
58
|
|
|
->filter(function ($tag) { |
|
59
|
|
|
return $tag instanceof Tag && $tag->getName() === 'queryParam'; |
|
|
|
|
|
|
60
|
|
|
}) |
|
61
|
|
|
->mapWithKeys(function ($tag) { |
|
62
|
|
|
preg_match('/(.+?)\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 was supplied |
|
66
|
|
|
list($name) = preg_split('/\s+/', $tag->getContent()); |
|
67
|
|
|
$required = false; |
|
68
|
|
|
$description = ''; |
|
69
|
|
|
} else { |
|
70
|
|
|
list($_, $name, $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
|
|
|
list($description, $value) = $this->parseParamDescription($description, 'string'); |
|
80
|
|
|
if (is_null($value) && ! $this->shouldExcludeExample($tag)) { |
|
81
|
|
|
$value = str_contains($description, ['number', 'count', 'page']) |
|
82
|
|
|
? $this->generateDummyValue('integer') |
|
83
|
|
|
: $this->generateDummyValue('string'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return [$name => compact('description', 'required', 'value')]; |
|
87
|
|
|
})->toArray(); |
|
88
|
|
|
|
|
89
|
|
|
return $parameters; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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.