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:
1 | <?php |
||
18 | class CdnHelper implements CdnHelperInterface |
||
19 | { |
||
20 | /** |
||
21 | * An object of the 'Repository' class that allows reading the laravel config files. |
||
22 | * |
||
23 | * @var \Illuminate\Config\Repository |
||
24 | */ |
||
25 | protected $configurations; |
||
26 | |||
27 | /** |
||
28 | * @param \Illuminate\Config\Repository $configurations |
||
29 | */ |
||
30 | public function __construct(Repository $configurations) |
||
34 | |||
35 | /** |
||
36 | * Check if the config file exist and return it or |
||
37 | * throw an exception. |
||
38 | * |
||
39 | * @return array |
||
40 | * |
||
41 | * @throws Exceptions\MissingConfigurationFileException |
||
42 | */ |
||
43 | public function getConfigurations() |
||
53 | |||
54 | /** |
||
55 | * Checks for any required configuration is missed. |
||
56 | * |
||
57 | * @param $configuration |
||
58 | * @param $required |
||
59 | * |
||
60 | * @throws \Vinelab\Cdn\Exceptions\MissingConfigurationException |
||
61 | */ |
||
62 | View Code Duplication | public function validate($configuration, $required) |
|
78 | |||
79 | /** |
||
80 | * Take url as string and return it parsed object. |
||
81 | * |
||
82 | * @param $url |
||
83 | * |
||
84 | * @return mixed |
||
85 | */ |
||
86 | public function parseUrl($url) |
||
90 | |||
91 | /** |
||
92 | * check if a string starts with a string. |
||
93 | * |
||
94 | * @param $with |
||
95 | * @param $str |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function startsWith($with, $str) |
||
103 | |||
104 | /** |
||
105 | * remove any extra slashes '/' from the path. |
||
106 | * |
||
107 | * @param $path |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function cleanPath($path) |
||
115 | } |
||
116 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.