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 | 9 | public static function create($path) |
|
18 | { |
||
19 | 9 | $pathComponents = explode('/', $path); |
|
20 | |||
21 | 9 | $name = $pathComponents[count($pathComponents) - 1]; |
|
22 | |||
23 | 9 | $addonConfig = static::loadConfig($path, $name); |
|
24 | |||
25 | 8 | $config = ConfigLoader::load($path.'/'.array_get($addonConfig, 'paths.config', 'config')); |
|
26 | |||
27 | 8 | $config->set('addon', $addonConfig); |
|
28 | |||
29 | 8 | return new static($name, $path, $config); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param string $path |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | 9 | protected static function loadConfig($path, $name) |
|
38 | { |
||
39 | 9 | if (file_exists($path.'/addon.php')) { |
|
40 | 8 | $config = require $path.'/addon.php'; |
|
41 | 9 | } 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 | 1 | 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 | 8 | 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 | 18 | public function __construct($name, $path, Repository $config) |
|
84 | { |
||
85 | 18 | $this->name = $name; |
|
86 | 18 | $this->path = $path; |
|
87 | 18 | $this->config = $config; |
|
88 | 18 | } |
|
89 | |||
90 | /** |
||
91 | * get name. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 13 | 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 | 4 | return $this->path; |
|
111 | } else { |
||
112 | 6 | 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 | 8 | 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 | 9 | public function phpNamespace() |
|
147 | |||
148 | /** |
||
149 | * get config value. |
||
150 | * |
||
151 | * @param string $key |
||
152 | * @param mixed $default |
||
153 | * |
||
154 | * @return mixed |
||
155 | */ |
||
156 | 13 | 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) |
|
274 | |||
275 | /** |
||
276 | * register addon version 4. |
||
277 | * |
||
278 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
279 | */ |
||
280 | 1 | protected function registerV4(Application $app) |
|
301 | |||
302 | /** |
||
303 | * register addon version 5. |
||
304 | * |
||
305 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
306 | */ |
||
307 | 3 | protected function registerV5(Application $app) |
|
315 | |||
316 | /** |
||
317 | * boot addon. |
||
318 | * |
||
319 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
320 | */ |
||
321 | 3 | public function boot(Application $app) |
|
335 | |||
336 | /** |
||
337 | * boot addon version 4. |
||
338 | * |
||
339 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
340 | */ |
||
341 | 1 | protected function bootV4(Application $app) |
|
362 | |||
363 | /** |
||
364 | * boot addon version 5. |
||
365 | * |
||
366 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
367 | */ |
||
368 | 2 | protected function bootV5(Application $app) |
|
382 | |||
383 | /** |
||
384 | * load addon initial script files. |
||
385 | * |
||
386 | * @param array $files |
||
387 | */ |
||
388 | 3 | protected function loadFiles(array $files) |
|
401 | /** |
||
402 | * Register the package's component namespaces. |
||
403 | * |
||
404 | * @param \Illuminate\Contracts\Foundation\Application $app |
||
405 | */ |
||
406 | 3 | protected function registerPackage(Application $app) |
|
425 | } |
||
426 |
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.