Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Generator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Generator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Generator |
||
| 14 | { |
||
| 15 | use ParamHelpers; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string The seed to be used with Faker. |
||
| 19 | * Useful when you want to always have the same fake output. |
||
| 20 | */ |
||
| 21 | private $fakerSeed = null; |
||
| 22 | |||
| 23 | public function __construct(string $fakerSeed = null) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param Route $route |
||
| 30 | * |
||
| 31 | * @return mixed |
||
| 32 | */ |
||
| 33 | public function getUri(Route $route) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Route $route |
||
| 40 | * |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | public function getMethods(Route $route) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param \Illuminate\Routing\Route $route |
||
| 50 | * @param array $apply Rules to apply when generating documentation for this route |
||
|
|
|||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public function processRoute(Route $route, array $rulesToApply = []) |
||
| 55 | { |
||
| 56 | $action = $route->getAction()["uses"]; |
||
| 57 | list($class, $method) = is_array($action) ? $action : explode('@', $action); |
||
| 58 | $controller = new ReflectionClass($class); |
||
| 59 | $method = $controller->getMethod($method); |
||
| 60 | |||
| 61 | $routeGroup = $this->getRouteGroup($controller, $method); |
||
| 62 | $docBlock = $this->parseDocBlock($method); |
||
| 63 | $bodyParameters = $this->getBodyParameters($method, $docBlock['tags']); |
||
| 64 | $queryParameters = $this->getQueryParameters($method, $docBlock['tags']); |
||
| 65 | $content = ResponseResolver::getResponse($route, $docBlock['tags'], [ |
||
| 66 | 'rules' => $rulesToApply, |
||
| 67 | 'body' => $bodyParameters, |
||
| 68 | 'query' => $queryParameters, |
||
| 69 | ]); |
||
| 70 | |||
| 71 | $parsedRoute = [ |
||
| 72 | 'id' => md5($this->getUri($route).':'.implode($this->getMethods($route))), |
||
| 73 | 'group' => $routeGroup, |
||
| 74 | 'title' => $docBlock['short'], |
||
| 75 | 'description' => $docBlock['long'], |
||
| 76 | 'methods' => $this->getMethods($route), |
||
| 77 | 'uri' => $this->getUri($route), |
||
| 78 | 'boundUri' => Utils::getFullUrl($route, $rulesToApply['bindings'] ?? ($rulesToApply['response_calls']['bindings'] ?? [])), |
||
| 79 | 'queryParameters' => $queryParameters, |
||
| 80 | 'bodyParameters' => $bodyParameters, |
||
| 81 | 'cleanBodyParameters' => $this->cleanParams($bodyParameters), |
||
| 82 | 'cleanQueryParameters' => $this->cleanParams($queryParameters), |
||
| 83 | 'authenticated' => $this->getAuthStatusFromDocBlock($docBlock['tags']), |
||
| 84 | 'response' => $content, |
||
| 85 | 'showresponse' => ! empty($content), |
||
| 86 | ]; |
||
| 87 | $parsedRoute['headers'] = $rulesToApply['headers'] ?? []; |
||
| 88 | |||
| 89 | return $parsedRoute; |
||
| 90 | } |
||
| 91 | |||
| 92 | View Code Duplication | protected function getBodyParameters(ReflectionMethod $method, array $tags) |
|
| 93 | { |
||
| 94 | foreach ($method->getParameters() as $param) { |
||
| 95 | $paramType = $param->getType(); |
||
| 96 | if ($paramType === null) { |
||
| 97 | continue; |
||
| 98 | } |
||
| 99 | |||
| 100 | $parameterClassName = version_compare(phpversion(), '7.1.0', '<') |
||
| 101 | ? $paramType->__toString() |
||
| 102 | : $paramType->getName(); |
||
| 103 | |||
| 104 | try { |
||
| 105 | $parameterClass = new ReflectionClass($parameterClassName); |
||
| 106 | } catch (\ReflectionException $e) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | if (class_exists('\Illuminate\Foundation\Http\FormRequest') && $parameterClass->isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class) || class_exists('\Dingo\Api\Http\FormRequest') && $parameterClass->isSubclassOf(\Dingo\Api\Http\FormRequest::class)) { |
||
| 111 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
||
| 112 | $bodyParametersFromDocBlock = $this->getBodyParametersFromDocBlock($formRequestDocBlock->getTags()); |
||
| 113 | |||
| 114 | if (count($bodyParametersFromDocBlock)) { |
||
| 115 | return $bodyParametersFromDocBlock; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | return $this->getBodyParametersFromDocBlock($tags); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $tags |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | protected function getBodyParametersFromDocBlock(array $tags) |
||
| 129 | { |
||
| 130 | $parameters = collect($tags) |
||
| 131 | ->filter(function ($tag) { |
||
| 132 | return $tag instanceof Tag && $tag->getName() === 'bodyParam'; |
||
| 133 | }) |
||
| 134 | ->mapWithKeys(function ($tag) { |
||
| 135 | preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
||
| 136 | View Code Duplication | if (empty($content)) { |
|
| 137 | // this means only name and type were supplied |
||
| 138 | list($name, $type) = preg_split('/\s+/', $tag->getContent()); |
||
| 139 | $required = false; |
||
| 140 | $description = ''; |
||
| 141 | } else { |
||
| 142 | list($_, $name, $type, $required, $description) = $content; |
||
| 143 | $description = trim($description); |
||
| 144 | if ($description == 'required' && empty(trim($required))) { |
||
| 145 | $required = $description; |
||
| 146 | $description = ''; |
||
| 147 | } |
||
| 148 | $required = trim($required) == 'required' ? true : false; |
||
| 149 | } |
||
| 150 | |||
| 151 | $type = $this->normalizeParameterType($type); |
||
| 152 | list($description, $example) = $this->parseDescription($description, $type); |
||
| 153 | $value = is_null($example) ? $this->generateDummyValue($type) : $example; |
||
| 154 | |||
| 155 | return [$name => compact('type', 'description', 'required', 'value')]; |
||
| 156 | })->toArray(); |
||
| 157 | |||
| 158 | return $parameters; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param ReflectionMethod $method |
||
| 163 | * @param array $tags |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | View Code Duplication | protected function getQueryParameters(ReflectionMethod $method, array $tags) |
|
| 168 | { |
||
| 169 | foreach ($method->getParameters() as $param) { |
||
| 170 | $paramType = $param->getType(); |
||
| 171 | if ($paramType === null) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | $parameterClassName = version_compare(phpversion(), '7.1.0', '<') |
||
| 176 | ? $paramType->__toString() |
||
| 177 | : $paramType->getName(); |
||
| 178 | |||
| 179 | try { |
||
| 180 | $parameterClass = new ReflectionClass($parameterClassName); |
||
| 181 | } catch (\ReflectionException $e) { |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | if (class_exists('\Illuminate\Foundation\Http\FormRequest') && $parameterClass->isSubclassOf(\Illuminate\Foundation\Http\FormRequest::class) || class_exists('\Dingo\Api\Http\FormRequest') && $parameterClass->isSubclassOf(\Dingo\Api\Http\FormRequest::class)) { |
||
| 186 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
||
| 187 | $queryParametersFromDocBlock = $this->getQueryParametersFromDocBlock($formRequestDocBlock->getTags()); |
||
| 188 | |||
| 189 | if (count($queryParametersFromDocBlock)) { |
||
| 190 | return $queryParametersFromDocBlock; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | return $this->getQueryParametersFromDocBlock($tags); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param array $tags |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | protected function getQueryParametersFromDocBlock(array $tags) |
||
| 204 | { |
||
| 205 | $parameters = collect($tags) |
||
| 206 | ->filter(function ($tag) { |
||
| 207 | return $tag instanceof Tag && $tag->getName() === 'queryParam'; |
||
| 208 | }) |
||
| 209 | ->mapWithKeys(function ($tag) { |
||
| 210 | preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content); |
||
| 211 | View Code Duplication | if (empty($content)) { |
|
| 212 | // this means only name was supplied |
||
| 213 | list($name) = preg_split('/\s+/', $tag->getContent()); |
||
| 214 | $required = false; |
||
| 215 | $description = ''; |
||
| 216 | } else { |
||
| 217 | list($_, $name, $required, $description) = $content; |
||
| 218 | $description = trim($description); |
||
| 219 | if ($description == 'required' && empty(trim($required))) { |
||
| 220 | $required = $description; |
||
| 221 | $description = ''; |
||
| 222 | } |
||
| 223 | $required = trim($required) == 'required' ? true : false; |
||
| 224 | } |
||
| 225 | |||
| 226 | list($description, $value) = $this->parseDescription($description, 'string'); |
||
| 227 | if (is_null($value)) { |
||
| 228 | $value = str_contains($description, ['number', 'count', 'page']) |
||
| 229 | ? $this->generateDummyValue('integer') |
||
| 230 | : $this->generateDummyValue('string'); |
||
| 231 | } |
||
| 232 | |||
| 233 | return [$name => compact('description', 'required', 'value')]; |
||
| 234 | })->toArray(); |
||
| 235 | |||
| 236 | return $parameters; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param array $tags |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | protected function getAuthStatusFromDocBlock(array $tags) |
||
| 245 | { |
||
| 246 | $authTag = collect($tags) |
||
| 247 | ->first(function ($tag) { |
||
| 248 | return $tag instanceof Tag && strtolower($tag->getName()) === 'authenticated'; |
||
| 249 | }); |
||
| 250 | |||
| 251 | return (bool) $authTag; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param ReflectionMethod $method |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected function parseDocBlock(ReflectionMethod $method) |
||
| 260 | { |
||
| 261 | $comment = $method->getDocComment(); |
||
| 262 | $phpdoc = new DocBlock($comment); |
||
| 263 | |||
| 264 | return [ |
||
| 265 | 'short' => $phpdoc->getShortDescription(), |
||
| 266 | 'long' => $phpdoc->getLongDescription()->getContents(), |
||
| 267 | 'tags' => $phpdoc->getTags(), |
||
| 268 | ]; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param ReflectionClass $controller |
||
| 273 | * @param ReflectionMethod $method |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | protected function getRouteGroup(ReflectionClass $controller, ReflectionMethod $method) |
||
| 278 | { |
||
| 279 | // @group tag on the method overrides that on the controller |
||
| 280 | $docBlockComment = $method->getDocComment(); |
||
| 281 | View Code Duplication | if ($docBlockComment) { |
|
| 282 | $phpdoc = new DocBlock($docBlockComment); |
||
| 283 | foreach ($phpdoc->getTags() as $tag) { |
||
| 284 | if ($tag->getName() === 'group') { |
||
| 285 | return $tag->getContent(); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | $docBlockComment = $controller->getDocComment(); |
||
| 291 | View Code Duplication | if ($docBlockComment) { |
|
| 292 | $phpdoc = new DocBlock($docBlockComment); |
||
| 293 | foreach ($phpdoc->getTags() as $tag) { |
||
| 294 | if ($tag->getName() === 'group') { |
||
| 295 | return $tag->getContent(); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return config('apidoc.default_group', 'general'); |
||
| 301 | } |
||
| 302 | |||
| 303 | private function normalizeParameterType($type) |
||
| 313 | |||
| 314 | private function generateDummyValue(string $type) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Allows users to specify an example for the parameter by writing 'Example: the-example', |
||
| 351 | * to be used in example requests and response calls. |
||
| 352 | * |
||
| 353 | * @param string $description |
||
| 354 | * @param string $type The type of the parameter. Used to cast the example provided, if any. |
||
| 355 | * |
||
| 356 | * @return array The description and included example. |
||
| 357 | */ |
||
| 358 | private function parseDescription(string $description, string $type) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Cast a value from a string to a specified type. |
||
| 373 | * |
||
| 374 | * @param string $value |
||
| 375 | * @param string $type |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | private function castToType(string $value, string $type) |
||
| 400 | } |
||
| 401 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.