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 | * Translate the given message. |
||
163 | * |
||
164 | * @param string $id |
||
|
|||
165 | * @param array $parameters |
||
166 | * @param string $domain |
||
167 | * @param string $locale |
||
168 | * @return string |
||
169 | */ |
||
170 | 1 | View Code Duplication | public function trans() |
177 | |||
178 | /** |
||
179 | * Translates the given message based on a count. |
||
180 | * |
||
181 | * @param string $id |
||
182 | * @param int $number |
||
183 | * @param array $parameters |
||
184 | * @param string $domain |
||
185 | * @param string $locale |
||
186 | * @return string |
||
187 | */ |
||
188 | 1 | View Code Duplication | public function transChoice() |
195 | |||
196 | /** |
||
197 | * @param string $view |
||
198 | * @param array $data |
||
199 | * @param array $mergeData |
||
200 | * |
||
201 | * @return \Illuminate\View\View |
||
202 | */ |
||
203 | public function view($view, $data = [], $mergeData = []) |
||
207 | |||
208 | /** |
||
209 | * Get spec. |
||
210 | * |
||
211 | * @param string $path |
||
212 | * |
||
213 | * @return \Jumilla\Addomnipot\Laravel\Specs\InputSpec |
||
214 | */ |
||
215 | public function spec($path) |
||
219 | |||
220 | /** |
||
221 | * register addon. |
||
222 | * |
||
223 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
224 | */ |
||
225 | 4 | public function register(Application $app) |
|
236 | |||
237 | /** |
||
238 | * register addon version 4. |
||
239 | * |
||
240 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
241 | */ |
||
242 | 1 | private function registerV4(Application $app) |
|
265 | |||
266 | /** |
||
267 | * register addon version 5. |
||
268 | * |
||
269 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
270 | */ |
||
271 | 3 | private function registerV5(Application $app) |
|
281 | |||
282 | /** |
||
283 | * boot addon. |
||
284 | * |
||
285 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
286 | */ |
||
287 | 3 | public function boot(Application $app) |
|
298 | |||
299 | /** |
||
300 | * boot addon version 4. |
||
301 | * |
||
302 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
303 | */ |
||
304 | 1 | private function bootV4(Application $app) |
|
325 | |||
326 | /** |
||
327 | * boot addon version 5. |
||
328 | * |
||
329 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
330 | */ |
||
331 | 2 | private function bootV5(Application $app) |
|
345 | |||
346 | /** |
||
347 | * load addon initial script files. |
||
348 | * |
||
349 | * @param array $files |
||
350 | */ |
||
351 | 3 | private function loadFiles(array $files) |
|
363 | } |
||
364 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.