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 Addon 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 Addon, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Addon |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param string $path |
||
| 14 | * |
||
| 15 | * @return static |
||
| 16 | */ |
||
| 17 | 5 | public static function create($path) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $path |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | 5 | protected static function loadConfig($path, $name) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $name; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $path; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \Illuminate\Contracts\Config\Repository |
||
| 70 | */ |
||
| 71 | protected $config; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \Illuminate\Contracts\Foundation\Application |
||
| 75 | */ |
||
| 76 | protected $app; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $name |
||
| 80 | * @param string $path |
||
| 81 | * @param \Illuminate\Contracts\Config\Repository $config |
||
| 82 | */ |
||
| 83 | 14 | public function __construct($name, $path, Repository $config) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * get name. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 5 | public function name() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * get fullpath. |
||
| 102 | * |
||
| 103 | * @param string $path |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 7 | public function path($path = null) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * get relative path. |
||
| 118 | * |
||
| 119 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | 2 | public function relativePath(Application $app) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * get version. |
||
| 130 | * |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | 7 | public function version() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * get PHP namespace. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 7 | public function phpNamespace() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * get config value. |
||
| 150 | * |
||
| 151 | * @param string $key |
||
| 152 | * @param mixed $default |
||
| 153 | * |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | 11 | public function config($key, $default = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Get a lang resource name |
||
| 163 | * |
||
| 164 | * @param string $resource |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 1 | public function transName($resource) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Translate the given message. |
||
| 175 | * |
||
| 176 | * @param string $id |
||
|
|
|||
| 177 | * @param array $parameters |
||
| 178 | * @param string $domain |
||
| 179 | * @param string $locale |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | 1 | View Code Duplication | public function trans() |
| 189 | |||
| 190 | /** |
||
| 191 | * Translates the given message based on a count. |
||
| 192 | * |
||
| 193 | * @param string $id |
||
| 194 | * @param int $number |
||
| 195 | * @param array $parameters |
||
| 196 | * @param string $domain |
||
| 197 | * @param string $locale |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | 1 | View Code Duplication | public function transChoice() |
| 207 | |||
| 208 | /** |
||
| 209 | * Get a view resource name |
||
| 210 | * |
||
| 211 | * @param string $resource |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function viewName($resource) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $view |
||
| 222 | * @param array $data |
||
| 223 | * @param array $mergeData |
||
| 224 | * |
||
| 225 | * @return \Illuminate\View\View |
||
| 226 | */ |
||
| 227 | public function view($view, $data = [], $mergeData = []) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get a spec resource name |
||
| 234 | * |
||
| 235 | * @param string $resource |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function specName($resource) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get spec. |
||
| 246 | * |
||
| 247 | * @param string $path |
||
| 248 | * |
||
| 249 | * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
||
| 250 | */ |
||
| 251 | public function spec($path) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * register addon. |
||
| 258 | * |
||
| 259 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 260 | */ |
||
| 261 | 4 | public function register(Application $app) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * register addon version 4. |
||
| 275 | * |
||
| 276 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 277 | */ |
||
| 278 | 1 | private function registerV4(Application $app) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * register addon version 5. |
||
| 304 | * |
||
| 305 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 306 | */ |
||
| 307 | 3 | private function registerV5(Application $app) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * boot addon. |
||
| 320 | * |
||
| 321 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 322 | */ |
||
| 323 | 3 | public function boot(Application $app) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * boot addon version 4. |
||
| 337 | * |
||
| 338 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 339 | */ |
||
| 340 | 1 | private function bootV4(Application $app) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * boot addon version 5. |
||
| 364 | * |
||
| 365 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 366 | */ |
||
| 367 | 2 | private function bootV5(Application $app) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * load addon initial script files. |
||
| 384 | * |
||
| 385 | * @param array $files |
||
| 386 | */ |
||
| 387 | 3 | private function loadFiles(array $files) |
|
| 399 | } |
||
| 400 |
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.