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 ApplicationPackage 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 ApplicationPackage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class ApplicationPackage implements RegistrationInterface |
||
34 | { |
||
35 | /** @var array $config */ |
||
36 | private $config; |
||
37 | |||
38 | /** @var Router $router */ |
||
39 | private $router; |
||
40 | |||
41 | /** |
||
42 | * ApplicationPackage constructor. |
||
43 | * @param array $config |
||
44 | 10 | * @param Router $router |
|
|
|||
45 | */ |
||
46 | 10 | public function __construct(array $config) |
|
47 | 10 | { |
|
48 | 10 | $this->config = $config; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param Container $c |
||
53 | * @throws \Bone\Exception |
||
54 | * @throws \Exception |
||
55 | 10 | */ |
|
56 | public function addToContainer(Container $c) |
||
73 | |||
74 | /** |
||
75 | * @param Container $c |
||
76 | 10 | */ |
|
77 | private function setConfigArray(Container $c) |
||
83 | |||
84 | /** |
||
85 | * @param Container $c |
||
86 | 10 | */ |
|
87 | View Code Duplication | private function setupViewEngine(Container $c) |
|
94 | |||
95 | 10 | /** |
|
96 | * @param Container $c |
||
97 | 10 | */ |
|
98 | 10 | private function setupRouter(Container $c) |
|
104 | 10 | ||
105 | /** |
||
106 | * @param Container $c |
||
107 | 10 | */ |
|
108 | 10 | private function setupPackages(Container $c) |
|
123 | |||
124 | 10 | /** |
|
125 | * @param string $packageName |
||
126 | * @param Container $c |
||
127 | 10 | */ |
|
128 | 10 | private function registerPackage(string $packageName, Container $c): void |
|
139 | |||
140 | 10 | /** |
|
141 | * @param RegistrationInterface $package |
||
142 | 10 | */ |
|
143 | 10 | private function registerConsoleCommands(RegistrationInterface $package, Container $c): void |
|
157 | |||
158 | 10 | /** |
|
159 | 10 | * @param RegistrationInterface $package |
|
160 | 10 | */ |
|
161 | private function registerMiddleware(RegistrationInterface $package, Container $c): void |
||
171 | |||
172 | 10 | /** |
|
173 | * @param MiddlewareRegistrationInterface $package |
||
174 | * @param Container $c |
||
175 | */ |
||
176 | private function addMiddlewaresToContainer(MiddlewareRegistrationInterface $package, Container $c): void |
||
185 | 10 | ||
186 | 10 | /** |
|
187 | * @param GlobalMiddlewareRegistrationInterface $package |
||
188 | * @param Container $c |
||
189 | 10 | */ |
|
190 | private function addMiddlewaresToStack(GlobalMiddlewareRegistrationInterface $package, Container $c): void |
||
200 | |||
201 | /** |
||
202 | 10 | * @param RegistrationInterface $package |
|
203 | */ |
||
204 | 10 | private function registerRoutes(RegistrationInterface $package, Container $c): void |
|
210 | |||
211 | /** |
||
212 | 10 | * @param RegistrationInterface $package |
|
213 | */ |
||
214 | 10 | private function registerViews(RegistrationInterface $package, Container $c): void |
|
231 | |||
232 | 10 | /** |
|
233 | * @param RegistrationInterface $package |
||
234 | 10 | */ |
|
235 | 10 | private function registerTranslations(RegistrationInterface $package, Container $c): void |
|
248 | 10 | ||
249 | /** |
||
250 | * @param Container $c |
||
251 | */ |
||
252 | private function initConsoleApp(Container $c): void |
||
256 | 10 | ||
257 | 10 | /** |
|
258 | 10 | * @param Container $c |
|
259 | 10 | */ |
|
260 | 10 | private function setupConsoleApp(Container $c): void |
|
265 | 10 | ||
266 | /** |
||
267 | 10 | * @param array $packages |
|
268 | 10 | * @param Container $c |
|
269 | 10 | */ |
|
270 | private function addEntityPathsFromPackages(array $packages, Container $c): void |
||
285 | |||
286 | /** |
||
287 | 10 | * @param Container $c |
|
288 | 10 | * @throws \Bone\Exception |
|
289 | 10 | */ |
|
290 | View Code Duplication | private function setupTranslator(Container $c) |
|
297 | |||
298 | |||
299 | /** |
||
300 | * @param Container $c |
||
301 | 10 | * @throws \Bone\Exception |
|
302 | */ |
||
303 | 10 | private function setupPdoConnection(Container $c) |
|
308 | 10 | ||
309 | /** |
||
310 | * @param Container $c |
||
311 | */ |
||
312 | private function setupDownloadController(Container $c): void |
||
320 | |||
321 | /** |
||
322 | 10 | * @param Container $c |
|
323 | */ |
||
324 | 10 | private function setupRouteFirewall(Container $c): void |
|
331 | 10 | ||
332 | /** |
||
333 | 10 | * @param Container $c |
|
334 | * @throws \Exception |
||
335 | */ |
||
336 | 10 | private function setupLogs(Container $c) |
|
341 | |||
342 | /** |
||
343 | * @param Container $c |
||
344 | */ |
||
345 | private function setupVendorViewOverrides(Container $c): void |
||
356 | |||
357 | /** |
||
358 | * @param string $view |
||
359 | * @param string $folder |
||
360 | * @param Folders $registeredViews |
||
361 | */ |
||
362 | private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
||
370 | |||
371 | /** |
||
372 | * @param Container $c |
||
373 | */ |
||
374 | private function initMiddlewareStack(Container $c): void |
||
378 | |||
379 | /** |
||
380 | * @param Container $c |
||
381 | */ |
||
382 | private function setupMiddlewareStack(Container $c): void |
||
397 | } |
||
398 |
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.