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) |
|
| 18 | { |
||
| 19 | 5 | $pathComponents = explode('/', $path); |
|
| 20 | |||
| 21 | 5 | $name = $pathComponents[count($pathComponents) - 1]; |
|
| 22 | |||
| 23 | 5 | $addonConfig = static::loadConfig($path, $name); |
|
| 24 | |||
| 25 | 4 | $config = ConfigLoader::load($path.'/'.array_get($addonConfig, 'paths.config', 'config')); |
|
| 26 | |||
| 27 | 4 | $config->set('addon', $addonConfig); |
|
| 28 | |||
| 29 | 4 | return new static($name, $path, $config); |
|
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $path |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | 5 | protected static function loadConfig($path, $name) |
|
| 38 | { |
||
| 39 | 5 | if (file_exists($path.'/addon.php')) { |
|
| 40 | 4 | $config = require $path.'/addon.php'; |
|
| 41 | 5 | } elseif (file_exists($path.'/addon.json')) { |
|
| 42 | $config = json_decode(file_get_contents($path.'/addon.json'), true); |
||
| 43 | |||
| 44 | if ($config === null) { |
||
| 45 | throw new RuntimeException("Invalid json format at '$path/addon.json'."); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | // compatible v4 addon |
||
| 49 | 2 | elseif (file_exists($path.'/config/addon.php')) { |
|
| 50 | $config = require $path.'/config/addon.php'; |
||
| 51 | } else { |
||
| 52 | 1 | throw new RuntimeException("No such config file for addon '$name', need 'addon.php' or 'addon.json'."); |
|
| 53 | } |
||
| 54 | |||
| 55 | 4 | return $config; |
|
| 56 | } |
||
| 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) |
|
| 84 | { |
||
| 85 | 14 | $this->name = $name; |
|
| 86 | 14 | $this->path = $path; |
|
| 87 | 14 | $this->config = $config; |
|
| 88 | 14 | } |
|
| 89 | |||
| 90 | /** |
||
| 91 | * get name. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 8 | public function name() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * get fullpath. |
||
| 102 | * |
||
| 103 | * @param string $path |
||
| 104 | * |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | 9 | public function path($path = null) |
|
| 108 | { |
||
| 109 | 9 | if (func_num_args() == 0) { |
|
| 110 | 3 | return $this->path; |
|
| 111 | } else { |
||
| 112 | 7 | return $this->path.'/'.$path; |
|
| 113 | } |
||
| 114 | } |
||
| 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() |
| 183 | { |
||
| 184 | 1 | $args = func_get_args(); |
|
| 185 | 1 | $args[0] = $this->transName($args[0]); |
|
| 186 | |||
| 187 | 1 | return call_user_func_array([$this->app['translator'], 'trans'], $args); |
|
| 188 | } |
||
| 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() |
| 201 | { |
||
| 202 | 1 | $args = func_get_args(); |
|
| 203 | 1 | $args[0] = $this->transName($args[0]); |
|
| 204 | |||
| 205 | 1 | return call_user_func_array([$this->app['translator'], 'transChoice'], $args); |
|
| 206 | } |
||
| 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) |
|
| 262 | { |
||
| 263 | 4 | $this->app = $app; |
|
| 264 | |||
| 265 | 4 | $version = $this->version(); |
|
| 266 | 4 | if ($version == 4) { |
|
| 267 | 1 | $this->registerV4($app); |
|
| 268 | 4 | } elseif ($version == 5) { |
|
| 269 | 3 | $this->registerV5($app); |
|
| 270 | 3 | } else { |
|
| 271 | throw new RuntimeException($version.': Illigal addon version.'); |
||
| 272 | } |
||
| 273 | 4 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * register addon version 4. |
||
| 277 | * |
||
| 278 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 279 | */ |
||
| 280 | 1 | protected function registerV4(Application $app) |
|
| 281 | { |
||
| 282 | 1 | $this->config['paths'] = [ |
|
| 283 | 1 | 'assets' => 'assets', |
|
| 284 | 1 | 'lang' => 'lang', |
|
| 285 | 1 | 'migrations' => 'migrations', |
|
| 286 | 1 | 'seeds' => 'seeds', |
|
| 287 | 1 | 'specs' => 'specs', |
|
| 288 | 1 | 'views' => 'views', |
|
| 289 | ]; |
||
| 290 | |||
| 291 | // regist service providers |
||
| 292 | 1 | $providers = $this->config('addon.providers', []); |
|
| 293 | 1 | foreach ($providers as $provider) { |
|
| 294 | if (!starts_with($provider, '\\')) { |
||
| 295 | $provider = sprintf('%s\%s', $this->phpNamespace(), $provider); |
||
| 296 | } |
||
| 297 | |||
| 298 | $app->register($provider); |
||
| 299 | 1 | } |
|
| 300 | 1 | } |
|
| 301 | |||
| 302 | /** |
||
| 303 | * register addon version 5. |
||
| 304 | * |
||
| 305 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 306 | */ |
||
| 307 | 3 | protected function registerV5(Application $app) |
|
| 308 | { |
||
| 309 | // regist service providers |
||
| 310 | 3 | $providers = $this->config('addon.providers', []); |
|
| 311 | 3 | foreach ($providers as $provider) { |
|
| 312 | 1 | $app->register($provider); |
|
| 313 | 3 | } |
|
| 314 | 3 | } |
|
| 315 | |||
| 316 | /** |
||
| 317 | * boot addon. |
||
| 318 | * |
||
| 319 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 320 | */ |
||
| 321 | 3 | public function boot(Application $app) |
|
| 322 | { |
||
| 323 | 3 | $this->registerPackage($app); |
|
| 324 | |||
| 325 | 3 | $version = $this->version(); |
|
| 326 | 3 | if ($version == 4) { |
|
| 327 | 1 | $this->bootV4($app); |
|
| 328 | 3 | } elseif ($version == 5) { |
|
| 329 | 2 | $this->bootV5($app); |
|
| 330 | 2 | } else { |
|
| 331 | throw new RuntimeException($version.': Illigal addon version.'); |
||
| 332 | } |
||
| 333 | |||
| 334 | 3 | } |
|
| 335 | |||
| 336 | /** |
||
| 337 | * boot addon version 4. |
||
| 338 | * |
||
| 339 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 340 | */ |
||
| 341 | 1 | protected function bootV4(Application $app) |
|
| 342 | { |
||
| 343 | 1 | $filenames = $this->config('addon.files'); |
|
| 344 | |||
| 345 | 1 | $files = []; |
|
| 346 | |||
| 347 | 1 | if ($filenames !== null) { |
|
| 348 | foreach ($filenames as $filename) { |
||
| 349 | $files[] = $this->path($filename); |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | // load *.php on addon's root directory |
||
| 353 | 1 | foreach ($app['files']->files($this->path) as $file) { |
|
| 354 | if (ends_with($file, '.php')) { |
||
| 355 | require $file; |
||
| 356 | } |
||
| 357 | 1 | } |
|
| 358 | } |
||
| 359 | |||
| 360 | 1 | $this->loadFiles($files); |
|
| 361 | 1 | } |
|
| 362 | |||
| 363 | /** |
||
| 364 | * boot addon version 5. |
||
| 365 | * |
||
| 366 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 367 | */ |
||
| 368 | 2 | protected function bootV5(Application $app) |
|
| 369 | { |
||
| 370 | 2 | $filenames = $this->config('addon.files'); |
|
| 371 | |||
| 372 | 2 | $files = []; |
|
| 373 | |||
| 374 | 2 | if ($filenames !== null) { |
|
| 375 | 1 | foreach ($filenames as $filename) { |
|
| 376 | 1 | $files[] = $this->path($filename); |
|
| 377 | 1 | } |
|
| 378 | 1 | } |
|
| 379 | |||
| 380 | 2 | $this->loadFiles($files); |
|
| 381 | 2 | } |
|
| 382 | |||
| 383 | /** |
||
| 384 | * load addon initial script files. |
||
| 385 | * |
||
| 386 | * @param array $files |
||
| 387 | */ |
||
| 388 | 3 | protected function loadFiles(array $files) |
|
| 389 | { |
||
| 390 | 3 | foreach ($files as $file) { |
|
| 391 | 1 | if (!file_exists($file)) { |
|
| 392 | $message = "Warning: PHP Script '$file' is nothing."; |
||
| 393 | info($message); |
||
| 394 | echo $message; |
||
| 395 | } |
||
| 396 | |||
| 397 | 1 | include $file; |
|
| 398 | 3 | } |
|
| 399 | 3 | } |
|
| 400 | /** |
||
| 401 | * Register the package's component namespaces. |
||
| 402 | * |
||
| 403 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
| 404 | */ |
||
| 405 | 3 | protected function registerPackage(Application $app) |
|
| 424 | } |
||
| 425 |
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.