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 AbstractGenerator 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 AbstractGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class AbstractGenerator |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @param $route |
||
| 19 | * |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | abstract public function getUri($route); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param $route |
||
| 26 | * |
||
| 27 | * @return mixed |
||
| 28 | */ |
||
| 29 | abstract public function getMethods($route); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param \Illuminate\Routing\Route $route |
||
| 33 | * @param array $bindings |
||
| 34 | * @param bool $withResponse |
||
| 35 | * |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | abstract public function processRoute($route, $bindings = [], $withResponse = true); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Prepares / Disables route middlewares. |
||
| 42 | * |
||
| 43 | * @param bool $disable |
||
| 44 | * |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | abstract public function prepareMiddleware($disable = false); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the response from the docblock if available. |
||
| 51 | * |
||
| 52 | * @param array $tags |
||
| 53 | * |
||
| 54 | * @return mixed |
||
| 55 | */ |
||
| 56 | protected function getDocblockResponse($tags) |
||
| 57 | { |
||
| 58 | $responseTags = array_filter($tags, function ($tag) { |
||
| 59 | if (! ($tag instanceof Tag)) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | |||
| 63 | return \strtolower($tag->getName()) == 'response'; |
||
| 64 | }); |
||
| 65 | if (empty($responseTags)) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | $responseTag = \array_first($responseTags); |
||
| 69 | |||
| 70 | return \response(\json_encode($responseTag->getContent())); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param array $routeData |
||
| 75 | * @param array $routeAction |
||
| 76 | * @param array $bindings |
||
| 77 | * |
||
| 78 | * @return mixed |
||
| 79 | */ |
||
| 80 | protected function getParameters($routeData, $routeAction, $bindings) |
||
| 81 | { |
||
| 82 | $validator = Validator::make([], $this->getRouteRules($routeAction['uses'], $bindings)); |
||
| 83 | foreach ($validator->getRules() as $attribute => $rules) { |
||
| 84 | $attributeData = [ |
||
| 85 | 'required' => false, |
||
| 86 | 'type' => null, |
||
| 87 | 'default' => '', |
||
| 88 | 'value' => '', |
||
| 89 | 'description' => [], |
||
| 90 | ]; |
||
| 91 | foreach ($rules as $ruleName => $rule) { |
||
| 92 | $this->parseRule($rule, $attribute, $attributeData, $routeData['id']); |
||
| 93 | } |
||
| 94 | $routeData['parameters'][$attribute] = $attributeData; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $routeData; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param $route |
||
| 102 | * @param $bindings |
||
| 103 | * @param $headers |
||
| 104 | * |
||
| 105 | * @return \Illuminate\Http\Response |
||
| 106 | */ |
||
| 107 | protected function getRouteResponse($route, $bindings, $headers = []) |
||
| 108 | { |
||
| 109 | $uri = $this->addRouteModelBindings($route, $bindings); |
||
| 110 | |||
| 111 | $methods = $this->getMethods($route); |
||
| 112 | |||
| 113 | // Split headers into key - value pairs |
||
| 114 | $headers = collect($headers)->map(function ($value) { |
||
| 115 | $split = explode(':', $value); |
||
| 116 | |||
| 117 | return [trim($split[0]) => trim($split[1])]; |
||
| 118 | })->collapse()->toArray(); |
||
| 119 | |||
| 120 | //Changes url with parameters like /users/{user} to /users/1 |
||
| 121 | $uri = preg_replace('/{(.*?)}/', 1, $uri); |
||
| 122 | |||
| 123 | return $this->callRoute(array_shift($methods), $uri, [], [], [], $headers); |
||
|
|
|||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param $route |
||
| 128 | * @param array $bindings |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | protected function addRouteModelBindings($route, $bindings) |
||
| 133 | { |
||
| 134 | $uri = $this->getUri($route); |
||
| 135 | foreach ($bindings as $model => $id) { |
||
| 136 | $uri = str_replace('{'.$model.'}', $id, $uri); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $uri; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param \Illuminate\Routing\Route $route |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function getRouteDescription($route) |
||
| 148 | { |
||
| 149 | list($class, $method) = explode('@', $route); |
||
| 150 | $reflection = new ReflectionClass($class); |
||
| 151 | $reflectionMethod = $reflection->getMethod($method); |
||
| 152 | |||
| 153 | $comment = $reflectionMethod->getDocComment(); |
||
| 154 | $phpdoc = new DocBlock($comment); |
||
| 155 | |||
| 156 | return [ |
||
| 157 | 'short' => $phpdoc->getShortDescription(), |
||
| 158 | 'long' => $phpdoc->getLongDescription()->getContents(), |
||
| 159 | 'tags' => $phpdoc->getTags(), |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $route |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | protected function getRouteGroup($route) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $route |
||
| 187 | * @param array $bindings |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | protected function getRouteRules($route, $bindings) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array $arr |
||
| 222 | * @param string $first |
||
| 223 | * @param string $last |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | protected function fancyImplode($arr, $first, $last) |
||
| 236 | |||
| 237 | protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ') |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $rule |
||
| 255 | * @param string $ruleName |
||
| 256 | * @param array $attributeData |
||
| 257 | * @param int $seed |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | protected function parseRule($rule, $ruleName, &$attributeData, $seed) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Call the given URI and return the Response. |
||
| 449 | * |
||
| 450 | * @param string $method |
||
| 451 | * @param string $uri |
||
| 452 | * @param array $parameters |
||
| 453 | * @param array $cookies |
||
| 454 | * @param array $files |
||
| 455 | * @param array $server |
||
| 456 | * @param string $content |
||
| 457 | * |
||
| 458 | * @return \Illuminate\Http\Response |
||
| 459 | */ |
||
| 460 | abstract public function callRoute($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null); |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Transform headers array to array of $_SERVER vars with HTTP_* format. |
||
| 464 | * |
||
| 465 | * @param array $headers |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | protected function transformHeadersToServerVars(array $headers) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Parse a string based rule. |
||
| 489 | * |
||
| 490 | * @param string $rules |
||
| 491 | * |
||
| 492 | * @return array |
||
| 493 | */ |
||
| 494 | protected function parseStringRule($rules) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Parse a parameter list. |
||
| 512 | * |
||
| 513 | * @param string $rule |
||
| 514 | * @param string $parameter |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | protected function parseParameters($rule, $parameter) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Normalizes a rule so that we can accept short types. |
||
| 529 | * |
||
| 530 | * @param string $rule |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | protected function normalizeRule($rule) |
||
| 545 | } |
||
| 546 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.