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 RepositoryManagerImpl 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 RepositoryManagerImpl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class RepositoryManagerImpl implements RepositoryManager |
||
55 | { |
||
56 | /** |
||
57 | * @var ProjectContext |
||
58 | */ |
||
59 | private $context; |
||
60 | |||
61 | /** |
||
62 | * @var EventDispatcherInterface |
||
63 | */ |
||
64 | private $dispatcher; |
||
65 | |||
66 | /** |
||
67 | * @var Config |
||
68 | */ |
||
69 | private $config; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | private $rootDir; |
||
75 | |||
76 | /** |
||
77 | * @var RootPackage |
||
78 | */ |
||
79 | private $rootPackage; |
||
80 | |||
81 | /** |
||
82 | * @var RootPackageFile |
||
83 | */ |
||
84 | private $rootPackageFile; |
||
85 | |||
86 | /** |
||
87 | * @var EditableRepository |
||
88 | */ |
||
89 | private $repo; |
||
90 | |||
91 | /** |
||
92 | * @var PackageCollection |
||
93 | */ |
||
94 | private $packages; |
||
95 | |||
96 | /** |
||
97 | * @var PackageFileStorage |
||
98 | */ |
||
99 | private $packageFileStorage; |
||
100 | |||
101 | /** |
||
102 | * @var OverrideGraph |
||
103 | */ |
||
104 | private $overrideGraph; |
||
105 | |||
106 | /** |
||
107 | * @var PackageConflictDetector |
||
108 | */ |
||
109 | private $conflictDetector; |
||
110 | |||
111 | /** |
||
112 | * @var PathMappingCollection |
||
113 | */ |
||
114 | private $mappings; |
||
115 | |||
116 | /** |
||
117 | * @var PathMappingCollection |
||
118 | */ |
||
119 | private $mappingsByResource; |
||
120 | |||
121 | /** |
||
122 | * @var ConflictCollection |
||
123 | */ |
||
124 | private $conflicts; |
||
125 | |||
126 | /** |
||
127 | * Creates a repository manager. |
||
128 | * |
||
129 | * @param ProjectContext $context |
||
130 | * @param EditableRepository $repo |
||
131 | * @param PackageCollection $packages |
||
132 | * @param PackageFileStorage $packageFileStorage |
||
133 | */ |
||
134 | 65 | public function __construct(ProjectContext $context, EditableRepository $repo, PackageCollection $packages, PackageFileStorage $packageFileStorage) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 1 | public function getContext() |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 1 | public function getRepository() |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 15 | public function addRootPathMapping(PathMapping $mapping, $flags = 0) |
|
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | 11 | public function removeRootPathMapping($repositoryPath) |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 3 | View Code Duplication | public function removeRootPathMappings(Expression $expr) |
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | 1 | public function clearRootPathMappings() |
|
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | 3 | public function getRootPathMapping($repositoryPath) |
|
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | 1 | View Code Duplication | public function findRootPathMappings(Expression $expr) |
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 4 | View Code Duplication | public function getRootPathMappings() |
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | 1 | public function hasRootPathMapping($repositoryPath) |
|
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 1 | View Code Duplication | public function hasRootPathMappings(Expression $expr = null) |
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | 6 | public function getPathMapping($repositoryPath, $packageName) |
|
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | 2 | public function getPathMappings() |
|
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | 2 | View Code Duplication | public function findPathMappings(Expression $expr) |
395 | |||
396 | /** |
||
397 | * {@inheritdoc} |
||
398 | */ |
||
399 | 2 | public function hasPathMapping($repositoryPath, $packageName) |
|
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | */ |
||
412 | 3 | View Code Duplication | public function hasPathMappings(Expression $expr = null) |
430 | |||
431 | /** |
||
432 | * {@inheritdoc} |
||
433 | */ |
||
434 | 10 | public function getPathConflicts() |
|
440 | |||
441 | /** |
||
442 | * {@inheritdoc} |
||
443 | */ |
||
444 | 15 | public function buildRepository() |
|
464 | |||
465 | /** |
||
466 | * {@inheritdoc} |
||
467 | */ |
||
468 | 1 | public function clearRepository() |
|
472 | |||
473 | 63 | private function loadPathMappings() |
|
495 | |||
496 | 13 | private function addPathMappingToPackageFile(PathMapping $mapping) |
|
500 | |||
501 | 12 | private function removePathMappingFromPackageFile($repositoryPath) |
|
505 | |||
506 | 57 | private function loadPathMapping(PathMapping $mapping, Package $package) |
|
510 | |||
511 | 12 | private function unloadPathMapping(PathMapping $mapping) |
|
515 | |||
516 | 26 | private function syncRepositoryPath($repositoryPath) |
|
520 | |||
521 | 14 | private function populateRepository() |
|
525 | |||
526 | 63 | private function updateConflicts(array $repositoryPaths = array()) |
|
530 | |||
531 | 13 | private function overrideConflictingPackages(PathMapping $mapping) |
|
535 | |||
536 | 25 | private function saveRootPackageFile() |
|
540 | |||
541 | 8 | private function removeResolvedConflicts() |
|
549 | |||
550 | 63 | private function assertMappingsLoaded() |
|
556 | |||
557 | 13 | private function assertNoLoadErrors(PathMapping $mapping) |
|
566 | } |
||
567 |
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.