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 = []) |
||
| 90 | |||
| 91 | View Code Duplication | protected function getBodyParameters(ReflectionMethod $method, array $tags) |
|
| 92 | { |
||
| 93 | foreach ($method->getParameters() as $param) { |
||
| 94 | $paramType = $param->getType(); |
||
| 95 | if ($paramType === null) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | $parameterClassName = version_compare(phpversion(), '7.1.0', '<') |
||
| 100 | ? $paramType->__toString() |
||
| 101 | : $paramType->getName(); |
||
| 102 | |||
| 103 | try { |
||
| 104 | $parameterClass = new ReflectionClass($parameterClassName); |
||
| 105 | } catch (\ReflectionException $e) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | 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)) { |
||
| 110 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
||
| 111 | $bodyParametersFromDocBlock = $this->getBodyParametersFromDocBlock($formRequestDocBlock->getTags()); |
||
| 112 | |||
| 113 | if (count($bodyParametersFromDocBlock)) { |
||
| 114 | return $bodyParametersFromDocBlock; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->getBodyParametersFromDocBlock($tags); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param array $tags |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | protected function getBodyParametersFromDocBlock(array $tags) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param ReflectionMethod $method |
||
| 162 | * @param array $tags |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | View Code Duplication | protected function getQueryParameters(ReflectionMethod $method, array $tags) |
|
| 167 | { |
||
| 168 | foreach ($method->getParameters() as $param) { |
||
| 169 | $paramType = $param->getType(); |
||
| 170 | if ($paramType === null) { |
||
| 171 | continue; |
||
| 172 | } |
||
| 173 | |||
| 174 | $parameterClassName = version_compare(phpversion(), '7.1.0', '<') |
||
| 175 | ? $paramType->__toString() |
||
| 176 | : $paramType->getName(); |
||
| 177 | |||
| 178 | try { |
||
| 179 | $parameterClass = new ReflectionClass($parameterClassName); |
||
| 180 | } catch (\ReflectionException $e) { |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | 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)) { |
||
| 185 | $formRequestDocBlock = new DocBlock($parameterClass->getDocComment()); |
||
| 186 | $queryParametersFromDocBlock = $this->getQueryParametersFromDocBlock($formRequestDocBlock->getTags()); |
||
| 187 | |||
| 188 | if (count($queryParametersFromDocBlock)) { |
||
| 189 | return $queryParametersFromDocBlock; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this->getQueryParametersFromDocBlock($tags); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param array $tags |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | protected function getQueryParametersFromDocBlock(array $tags) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $tags |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | protected function getAuthStatusFromDocBlock(array $tags) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param ReflectionMethod $method |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | protected function parseDocBlock(ReflectionMethod $method) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param ReflectionClass $controller |
||
| 272 | * @param ReflectionMethod $method |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | protected function getRouteGroup(ReflectionClass $controller, ReflectionMethod $method) |
||
| 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.