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 RootPackageFileManagerImpl 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 RootPackageFileManagerImpl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class RootPackageFileManagerImpl extends AbstractConfigManager implements RootPackageFileManager |
||
36 | { |
||
37 | /** |
||
38 | * @var ProjectContext |
||
39 | */ |
||
40 | private $context; |
||
41 | |||
42 | /** |
||
43 | * @var RootPackageFile |
||
44 | */ |
||
45 | private $rootPackageFile; |
||
46 | |||
47 | /** |
||
48 | * @var PackageFileStorage |
||
49 | */ |
||
50 | private $packageFileStorage; |
||
51 | |||
52 | /** |
||
53 | * Creates a new package file manager. |
||
54 | * |
||
55 | * @param ProjectContext $context The project context |
||
56 | * @param PackageFileStorage $packageFileStorage The package file storage. |
||
57 | */ |
||
58 | 79 | public function __construct(ProjectContext $context, PackageFileStorage $packageFileStorage) |
|
59 | { |
||
60 | 79 | $this->context = $context; |
|
61 | 79 | $this->rootPackageFile = $context->getRootPackageFile(); |
|
62 | 79 | $this->packageFileStorage = $packageFileStorage; |
|
63 | 79 | } |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 17 | public function getConfig() |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 1 | public function getContext() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function getPackageFile() |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | 4 | public function getPackageName() |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 3 | View Code Duplication | public function setPackageName($packageName) |
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 7 | View Code Duplication | public function addPluginClass($pluginClass) |
123 | { |
||
124 | 7 | if ($this->rootPackageFile->hasPluginClass($pluginClass)) { |
|
125 | // Already installed locally |
||
126 | 1 | return; |
|
127 | } |
||
128 | |||
129 | 6 | $this->validatePluginClass($pluginClass); |
|
130 | |||
131 | 2 | $previousClasses = $this->rootPackageFile->getPluginClasses(); |
|
132 | |||
133 | 2 | $this->rootPackageFile->addPluginClass($pluginClass); |
|
134 | |||
135 | try { |
||
136 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
137 | 2 | } catch (Exception $e) { |
|
138 | 1 | $this->rootPackageFile->setPluginClasses($previousClasses); |
|
139 | |||
140 | 1 | throw $e; |
|
141 | } |
||
142 | 1 | } |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 3 | public function removePluginClass($pluginClass) |
|
148 | { |
||
149 | 3 | if (!$this->rootPackageFile->hasPluginClass($pluginClass)) { |
|
150 | 1 | return; |
|
151 | } |
||
152 | |||
153 | 2 | $previousClasses = $this->rootPackageFile->getPluginClasses(); |
|
154 | |||
155 | 2 | $this->rootPackageFile->removePluginClass($pluginClass); |
|
156 | |||
157 | try { |
||
158 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
159 | 2 | } catch (Exception $e) { |
|
160 | 1 | $this->rootPackageFile->setPluginClasses($previousClasses); |
|
161 | |||
162 | 1 | throw $e; |
|
163 | } |
||
164 | 1 | } |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 7 | View Code Duplication | public function removePluginClasses(Expression $expr) |
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 3 | public function clearPluginClasses() |
|
198 | { |
||
199 | 3 | $this->removePluginClasses(Expr::true()); |
|
200 | 2 | } |
|
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 1 | public function hasPluginClass($pluginClass) |
|
206 | { |
||
207 | 1 | return $this->rootPackageFile->hasPluginClass($pluginClass); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 1 | View Code Duplication | public function hasPluginClasses(Expression $expr = null) |
214 | { |
||
215 | 1 | if (!$expr) { |
|
216 | 1 | return $this->rootPackageFile->hasPluginClasses(); |
|
217 | } |
||
218 | |||
219 | 1 | foreach ($this->rootPackageFile->getPluginClasses() as $pluginClass) { |
|
220 | 1 | if ($expr->evaluate($pluginClass)) { |
|
221 | 1 | return true; |
|
222 | } |
||
223 | 1 | } |
|
224 | |||
225 | 1 | return false; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 14 | public function getPluginClasses() |
|
232 | { |
||
233 | 14 | return $this->rootPackageFile->getPluginClasses(); |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | 1 | public function findPluginClasses(Expression $expr) |
|
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 4 | public function setExtraKey($key, $value) |
|
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | 3 | public function setExtraKeys(array $values) |
|
283 | { |
||
284 | 3 | $previousValues = array(); |
|
285 | 3 | $previouslyUnset = array(); |
|
286 | |||
287 | 3 | foreach ($values as $key => $value) { |
|
288 | 3 | if ($this->rootPackageFile->hasExtraKey($key)) { |
|
289 | 2 | if ($value !== $previous = $this->rootPackageFile->getExtraKey($key)) { |
|
290 | 1 | $previousValues[$key] = $previous; |
|
291 | 1 | } |
|
292 | 2 | } else { |
|
293 | 2 | $previouslyUnset[$key] = true; |
|
294 | } |
||
295 | 3 | } |
|
296 | |||
297 | 3 | if (!$previousValues && !$previouslyUnset) { |
|
298 | 1 | return; |
|
299 | } |
||
300 | |||
301 | 2 | $this->rootPackageFile->setExtraKeys($values); |
|
302 | |||
303 | try { |
||
304 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
305 | 2 | } catch (Exception $e) { |
|
306 | 1 | foreach ($values as $key => $value) { |
|
307 | 1 | if (isset($previouslyUnset[$key])) { |
|
308 | 1 | $this->rootPackageFile->removeExtraKey($key); |
|
309 | 1 | } else { |
|
310 | 1 | $this->rootPackageFile->setExtraKey($key, $previousValues[$key]); |
|
311 | } |
||
312 | 1 | } |
|
313 | |||
314 | 1 | throw $e; |
|
315 | } |
||
316 | 1 | } |
|
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | 3 | View Code Duplication | public function removeExtraKey($key) |
322 | { |
||
323 | 3 | if (!$this->rootPackageFile->hasExtraKey($key)) { |
|
324 | 1 | return; |
|
325 | } |
||
326 | |||
327 | 2 | $previousValue = $this->rootPackageFile->getExtraKey($key); |
|
328 | |||
329 | 2 | $this->rootPackageFile->removeExtraKey($key); |
|
330 | |||
331 | try { |
||
332 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
333 | 2 | } catch (Exception $e) { |
|
334 | 1 | $this->rootPackageFile->setExtraKey($key, $previousValue); |
|
335 | |||
336 | 1 | throw $e; |
|
337 | } |
||
338 | 1 | } |
|
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | 3 | View Code Duplication | public function removeExtraKeys(Expression $expr) |
344 | { |
||
345 | 3 | $previousValues = $this->rootPackageFile->getExtraKeys(); |
|
346 | 3 | $save = false; |
|
347 | |||
348 | 3 | foreach ($this->rootPackageFile->getExtraKeys() as $key => $value) { |
|
349 | 2 | if ($expr->evaluate($key)) { |
|
350 | 2 | $this->rootPackageFile->removeExtraKey($key); |
|
351 | 2 | $save = true; |
|
352 | 2 | } |
|
353 | 3 | } |
|
354 | |||
355 | 3 | if (!$save) { |
|
356 | 1 | return; |
|
357 | } |
||
358 | |||
359 | try { |
||
360 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
361 | 2 | } catch (Exception $e) { |
|
362 | 1 | $this->rootPackageFile->setExtraKeys($previousValues); |
|
363 | |||
364 | 1 | throw $e; |
|
365 | } |
||
366 | 1 | } |
|
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | 3 | View Code Duplication | public function clearExtraKeys() |
372 | { |
||
373 | 3 | $previousValues = $this->rootPackageFile->getExtraKeys(); |
|
374 | |||
375 | 3 | if (!$previousValues) { |
|
376 | 1 | return; |
|
377 | } |
||
378 | |||
379 | 2 | $this->rootPackageFile->clearExtraKeys(); |
|
380 | |||
381 | try { |
||
382 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
383 | 2 | } catch (Exception $e) { |
|
384 | 1 | $this->rootPackageFile->setExtraKeys($previousValues); |
|
385 | |||
386 | 1 | throw $e; |
|
387 | } |
||
388 | 1 | } |
|
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | 1 | public function hasExtraKey($key) |
|
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | 1 | View Code Duplication | public function hasExtraKeys(Expression $expr = null) |
415 | |||
416 | /** |
||
417 | * {@inheritdoc} |
||
418 | */ |
||
419 | 15 | public function getExtraKey($key, $default = null) |
|
420 | { |
||
421 | 15 | return $this->rootPackageFile->getExtraKey($key, $default); |
|
422 | } |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | */ |
||
427 | 1 | public function getExtraKeys() |
|
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | */ |
||
435 | 1 | public function findExtraKeys(Expression $expr) |
|
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | */ |
||
451 | 2 | View Code Duplication | public function migrate($targetVersion) |
469 | |||
470 | /** |
||
471 | * {@inheritdoc} |
||
472 | */ |
||
473 | 4 | protected function saveConfigFile() |
|
477 | |||
478 | 6 | private function validatePluginClass($pluginClass) |
|
479 | { |
||
480 | try { |
||
481 | 6 | $reflClass = new ReflectionClass($pluginClass); |
|
482 | 6 | } catch (ReflectionException $e) { |
|
483 | 1 | throw new InvalidArgumentException(sprintf( |
|
484 | 1 | 'The plugin class %s does not exist.', |
|
485 | $pluginClass |
||
486 | 1 | ), 0, $e); |
|
487 | } |
||
488 | |||
489 | 5 | if ($reflClass->isInterface()) { |
|
520 | } |
||
521 |
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.