1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Extracting\Strategies\UrlParameters; |
4
|
|
|
|
5
|
|
|
use Dingo\Api\Http\FormRequest as DingoFormRequest; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest; |
7
|
|
|
use Illuminate\Routing\Route; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Mpociot\ApiDoc\Extracting\ParamHelpers; |
10
|
|
|
use Mpociot\ApiDoc\Extracting\RouteDocBlocker; |
11
|
|
|
use Mpociot\ApiDoc\Extracting\Strategies\Strategy; |
12
|
|
|
use Mpociot\Reflection\DocBlock; |
13
|
|
|
use Mpociot\Reflection\DocBlock\Tag; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
use ReflectionMethod; |
16
|
|
|
|
17
|
|
View Code Duplication |
class GetFromUrlParamTag extends Strategy |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
use ParamHelpers; |
20
|
|
|
|
21
|
|
|
public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = []) |
22
|
|
|
{ |
23
|
|
|
foreach ($method->getParameters() as $param) { |
24
|
|
|
$paramType = $param->getType(); |
25
|
|
|
if ($paramType === null) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$parameterClassName = $paramType->getName(); |
30
|
|
|
|
31
|
|
|
try { |
32
|
|
|
$parameterClass = new ReflectionClass($parameterClassName); |
33
|
|
|
} catch (\ReflectionException $e) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// If there's a FormRequest, we check there for @urlParam tags. |
38
|
|
|
if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class) |
39
|
|
|
|| class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) { |
40
|
|
|
$formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
41
|
|
|
$queryParametersFromDocBlock = $this->getUrlParametersFromDocBlock($formRequestDocBlock->getTags()); |
42
|
|
|
|
43
|
|
|
if (count($queryParametersFromDocBlock)) { |
44
|
|
|
return $queryParametersFromDocBlock; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @var DocBlock $methodDocBlock */ |
50
|
|
|
$methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method']; |
51
|
|
|
|
52
|
|
|
return $this->getUrlParametersFromDocBlock($methodDocBlock->getTags()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getUrlParametersFromDocBlock($tags) |
56
|
|
|
{ |
57
|
|
|
$parameters = collect($tags) |
58
|
|
|
->filter(function ($tag) { |
59
|
|
|
return $tag instanceof Tag && $tag->getName() === 'urlParam'; |
|
|
|
|
60
|
|
|
}) |
61
|
|
|
->mapWithKeys(function (Tag $tag) { |
62
|
|
|
// Format: |
63
|
|
|
// @urlParam <name> <"required" (optional)> <description> |
64
|
|
|
// Examples: |
65
|
|
|
// @urlParam id string required The id of the post. |
66
|
|
|
// @urlParam user_id The ID of the user. |
67
|
|
|
preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
68
|
|
|
$content = preg_replace('/\s?No-example.?/', '', $content); |
69
|
|
|
if (empty($content)) { |
70
|
|
|
// this means only name was supplied |
71
|
|
|
list($name) = preg_split('/\s+/', $tag->getContent()); |
72
|
|
|
$required = false; |
73
|
|
|
$description = ''; |
74
|
|
|
} else { |
75
|
|
|
list($_, $name, $required, $description) = $content; |
|
|
|
|
76
|
|
|
$description = trim($description); |
77
|
|
|
if ($description == 'required' && empty(trim($required))) { |
78
|
|
|
$required = $description; |
79
|
|
|
$description = ''; |
80
|
|
|
} |
81
|
|
|
$required = trim($required) == 'required' ? true : false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
list($description, $value) = $this->parseParamDescription($description, 'string'); |
85
|
|
|
if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) { |
86
|
|
|
$value = Str::contains($description, ['number', 'count', 'page']) |
87
|
|
|
? $this->generateDummyValue('integer') |
88
|
|
|
: $this->generateDummyValue('string'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return [$name => compact('description', 'required', 'value')]; |
92
|
|
|
})->toArray(); |
93
|
|
|
|
94
|
|
|
return $parameters; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.