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 DiscoveryManagerImpl 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 DiscoveryManagerImpl, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class DiscoveryManagerImpl implements DiscoveryManager |
||
67 | { |
||
68 | /** |
||
69 | * @var ProjectContext |
||
70 | */ |
||
71 | private $context; |
||
72 | |||
73 | /** |
||
74 | * @var LoggerInterface |
||
75 | */ |
||
76 | private $logger; |
||
77 | |||
78 | /** |
||
79 | * @var EditableDiscovery |
||
80 | */ |
||
81 | private $discovery; |
||
82 | |||
83 | /** |
||
84 | * @var ModuleList |
||
85 | */ |
||
86 | private $modules; |
||
87 | |||
88 | /** |
||
89 | * @var JsonStorage |
||
90 | */ |
||
91 | private $jsonStorage; |
||
92 | |||
93 | /** |
||
94 | * @var RootModule |
||
95 | */ |
||
96 | private $rootModule; |
||
97 | |||
98 | /** |
||
99 | * @var RootModuleFile |
||
100 | */ |
||
101 | private $rootModuleFile; |
||
102 | |||
103 | /** |
||
104 | * @var BindingTypeDescriptorCollection |
||
105 | */ |
||
106 | private $typeDescriptors; |
||
107 | |||
108 | /** |
||
109 | * @var BindingDescriptorCollection |
||
110 | */ |
||
111 | private $bindingDescriptors; |
||
112 | |||
113 | /** |
||
114 | * Creates a tag manager. |
||
115 | * |
||
116 | * @param ProjectContext $context |
||
117 | * @param EditableDiscovery $discovery |
||
118 | * @param ModuleList $modules |
||
119 | * @param JsonStorage $jsonStorage |
||
120 | * @param LoggerInterface|null $logger |
||
121 | */ |
||
122 | 98 | public function __construct( |
|
123 | ProjectContext $context, |
||
124 | EditableDiscovery $discovery, |
||
125 | ModuleList $modules, |
||
126 | JsonStorage $jsonStorage, |
||
127 | LoggerInterface $logger = null |
||
128 | ) { |
||
129 | 98 | $this->context = $context; |
|
130 | 98 | $this->discovery = $discovery; |
|
131 | 98 | $this->modules = $modules; |
|
132 | 98 | $this->jsonStorage = $jsonStorage; |
|
133 | 98 | $this->rootModule = $modules->getRootModule(); |
|
134 | 98 | $this->rootModuleFile = $context->getRootModuleFile(); |
|
135 | 98 | $this->logger = $logger ?: new NullLogger(); |
|
136 | 98 | } |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 1 | public function getContext() |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 5 | public function addRootTypeDescriptor(BindingTypeDescriptor $typeDescriptor, $flags = 0) |
|
150 | { |
||
151 | 5 | Assert::integer($flags, 'The argument $flags must be a boolean.'); |
|
152 | |||
153 | 5 | $this->assertModulesLoaded(); |
|
154 | 5 | $this->emitWarningForDuplicateTypes(); |
|
155 | |||
156 | 5 | $typeName = $typeDescriptor->getTypeName(); |
|
157 | |||
158 | 5 | if (!($flags & self::OVERRIDE) && $this->typeDescriptors->contains($typeName)) { |
|
159 | 1 | throw DuplicateTypeException::forTypeName($typeName); |
|
160 | } |
||
161 | |||
162 | 4 | $tx = new Transaction(); |
|
163 | |||
164 | try { |
||
165 | 4 | $syncBindingOps = array(); |
|
166 | |||
167 | 4 | View Code Duplication | foreach ($this->getUuidsByTypeName($typeName) as $uuid) { |
168 | $syncBindingOp = $this->syncBinding($uuid); |
||
169 | $syncBindingOp->takeSnapshot(); |
||
170 | $syncBindingOps[] = $syncBindingOp; |
||
171 | } |
||
172 | |||
173 | $syncOp = $this->syncTypeName($typeName); |
||
174 | $syncOp->takeSnapshot(); |
||
175 | |||
176 | $tx->execute($this->loadTypeDescriptor($typeDescriptor, $this->rootModule)); |
||
177 | $tx->execute($this->addTypeDescriptorToModuleFile($typeDescriptor)); |
||
178 | $tx->execute($syncOp); |
||
179 | |||
180 | foreach ($syncBindingOps as $syncBindingOp) { |
||
181 | $tx->execute($syncBindingOp); |
||
182 | } |
||
183 | |||
184 | $this->saveRootModuleFile(); |
||
185 | |||
186 | $tx->commit(); |
||
187 | 4 | } catch (Exception $e) { |
|
188 | $tx->rollback(); |
||
189 | |||
190 | throw $e; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 11 | public function removeRootTypeDescriptor($typeName) |
|
198 | { |
||
199 | // Only check that this is a string. The error message "not found" is |
||
200 | // more helpful than e.g. "type name must contain /". |
||
201 | 11 | Assert::string($typeName, 'The type name must be a string'); |
|
202 | |||
203 | 11 | $this->assertModulesLoaded(); |
|
204 | |||
205 | 11 | if (!$this->typeDescriptors->contains($typeName, $this->rootModule->getName())) { |
|
206 | 2 | return; |
|
207 | } |
||
208 | |||
209 | 9 | $typeDescriptor = $this->typeDescriptors->get($typeName, $this->rootModule->getName()); |
|
210 | 9 | $tx = new Transaction(); |
|
211 | |||
212 | try { |
||
213 | 9 | $tx->execute($this->removeTypeDescriptorFromModuleFile($typeName)); |
|
214 | |||
215 | 9 | $syncBindingOps = array(); |
|
216 | |||
217 | 9 | View Code Duplication | foreach ($this->getUuidsByTypeName($typeName) as $uuid) { |
218 | $syncBindingOp = $this->syncBinding($uuid); |
||
219 | $syncBindingOp->takeSnapshot(); |
||
220 | $syncBindingOps[] = $syncBindingOp; |
||
221 | } |
||
222 | |||
223 | $syncOp = $this->syncTypeName($typeName); |
||
224 | $syncOp->takeSnapshot(); |
||
225 | |||
226 | $tx->execute($this->unloadTypeDescriptor($typeDescriptor)); |
||
227 | $tx->execute($syncOp); |
||
228 | |||
229 | foreach ($syncBindingOps as $syncBindingOp) { |
||
230 | $tx->execute($syncBindingOp); |
||
231 | } |
||
232 | |||
233 | $this->saveRootModuleFile(); |
||
234 | |||
235 | $tx->commit(); |
||
236 | 9 | } catch (Exception $e) { |
|
237 | $tx->rollback(); |
||
238 | |||
239 | throw $e; |
||
240 | } |
||
241 | |||
242 | $this->emitWarningForDuplicateTypes(); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | 4 | public function removeRootTypeDescriptors(Expression $expr) |
|
249 | { |
||
250 | 4 | $this->assertModulesLoaded(); |
|
251 | |||
252 | 4 | $tx = new Transaction(); |
|
253 | 4 | $syncBindingOps = array(); |
|
254 | |||
255 | try { |
||
256 | 4 | foreach ($this->getRootTypeDescriptors() as $typeDescriptor) { |
|
257 | 4 | if ($expr->evaluate($typeDescriptor)) { |
|
258 | 4 | $typeName = $typeDescriptor->getTypeName(); |
|
259 | |||
260 | 4 | $tx->execute($this->removeTypeDescriptorFromModuleFile($typeName)); |
|
261 | |||
262 | 4 | View Code Duplication | foreach ($this->getUuidsByTypeName($typeName) as $uuid) { |
263 | $syncBindingOp = $this->syncBinding($uuid); |
||
264 | $syncBindingOp->takeSnapshot(); |
||
265 | $syncBindingOps[] = $syncBindingOp; |
||
266 | } |
||
267 | |||
268 | $syncOp = $this->syncTypeName($typeName); |
||
269 | $syncOp->takeSnapshot(); |
||
270 | |||
271 | $tx->execute($this->unloadTypeDescriptor($typeDescriptor)); |
||
272 | $tx->execute($syncOp); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | foreach ($syncBindingOps as $syncBindingOp) { |
||
277 | $tx->execute($syncBindingOp); |
||
278 | } |
||
279 | |||
280 | $this->saveRootModuleFile(); |
||
281 | |||
282 | $tx->commit(); |
||
283 | 4 | } catch (Exception $e) { |
|
284 | $tx->rollback(); |
||
285 | |||
286 | throw $e; |
||
287 | } |
||
288 | |||
289 | $this->emitWarningForDuplicateTypes(); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 1 | public function clearRootTypeDescriptors() |
|
296 | { |
||
297 | 1 | $this->removeRootTypeDescriptors(Expr::true()); |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | 2 | public function getRootTypeDescriptor($typeName) |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 5 | View Code Duplication | public function getRootTypeDescriptors() |
312 | { |
||
313 | 5 | $this->assertModulesLoaded(); |
|
314 | |||
315 | 5 | $types = array(); |
|
316 | 5 | $rootModuleName = $this->rootModule->getName(); |
|
317 | |||
318 | 5 | foreach ($this->typeDescriptors->toArray() as $typeName => $typesByModule) { |
|
319 | 5 | if (isset($typesByModule[$rootModuleName])) { |
|
320 | 5 | $types[] = $typesByModule[$rootModuleName]; |
|
321 | } |
||
322 | } |
||
323 | |||
324 | 5 | return $types; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | 1 | View Code Duplication | public function findRootTypeDescriptors(Expression $expr) |
331 | { |
||
332 | 1 | $expr = Expr::method('getContainingModule', Expr::same($this->rootModule)) |
|
333 | 1 | ->andX($expr); |
|
334 | |||
335 | 1 | return $this->findTypeDescriptors($expr); |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | 1 | public function hasRootTypeDescriptor($typeName) |
|
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | */ |
||
349 | 1 | View Code Duplication | public function hasRootTypeDescriptors(Expression $expr = null) |
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | 6 | public function getTypeDescriptor($typeName, $moduleName) |
|
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | 2 | public function getTypeDescriptors() |
|
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | 2 | View Code Duplication | public function findTypeDescriptors(Expression $expr) |
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | 3 | public function hasTypeDescriptor($typeName, $moduleName = null) |
|
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | 3 | public function hasTypeDescriptors(Expression $expr = null) |
|
448 | |||
449 | /** |
||
450 | * {@inheritdoc} |
||
451 | */ |
||
452 | 12 | public function addRootBindingDescriptor(BindingDescriptor $bindingDescriptor, $flags = 0) |
|
453 | { |
||
500 | |||
501 | /** |
||
502 | * {@inheritdoc} |
||
503 | */ |
||
504 | 3 | View Code Duplication | public function removeRootBindingDescriptors(Expression $expr) |
531 | |||
532 | /** |
||
533 | * {@inheritdoc} |
||
534 | */ |
||
535 | 1 | public function clearRootBindingDescriptors() |
|
539 | |||
540 | /** |
||
541 | * {@inheritdoc} |
||
542 | */ |
||
543 | 3 | View Code Duplication | public function getRootBindingDescriptors() |
557 | |||
558 | /** |
||
559 | * {@inheritdoc} |
||
560 | */ |
||
561 | 1 | View Code Duplication | public function findRootBindingDescriptors(Expression $expr) |
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | 1 | View Code Duplication | public function hasRootBindingDescriptors(Expression $expr = null) |
582 | |||
583 | /** |
||
584 | * {@inheritdoc} |
||
585 | */ |
||
586 | 1 | public function getBindingDescriptors() |
|
592 | |||
593 | /** |
||
594 | * {@inheritdoc} |
||
595 | */ |
||
596 | 2 | View Code Duplication | public function findBindingDescriptors(Expression $expr) |
610 | |||
611 | /** |
||
612 | * {@inheritdoc} |
||
613 | */ |
||
614 | 3 | View Code Duplication | public function hasBindingDescriptors(Expression $expr = null) |
630 | |||
631 | /** |
||
632 | * {@inheritdoc} |
||
633 | */ |
||
634 | 7 | public function buildDiscovery() |
|
668 | |||
669 | /** |
||
670 | * {@inheritdoc} |
||
671 | */ |
||
672 | 1 | public function clearDiscovery() |
|
676 | |||
677 | 61 | private function assertModulesLoaded() |
|
683 | |||
684 | 6 | private function assertBindingValid(BindingDescriptor $bindingDescriptor) |
|
694 | |||
695 | 61 | private function loadModules() |
|
722 | |||
723 | 12 | private function emitWarningForDuplicateTypes() |
|
741 | |||
742 | 7 | private function emitWarningForInvalidBindings() |
|
755 | |||
756 | 5 | private function saveRootModuleFile() |
|
760 | |||
761 | private function addTypeDescriptorToModuleFile(BindingTypeDescriptor $typeDescriptor) |
||
765 | |||
766 | 13 | private function removeTypeDescriptorFromModuleFile($typeName) |
|
770 | |||
771 | 48 | View Code Duplication | private function loadTypeDescriptor(BindingTypeDescriptor $typeDescriptor, Module $module) |
783 | |||
784 | View Code Duplication | private function unloadTypeDescriptor(BindingTypeDescriptor $typeDescriptor) |
|
796 | |||
797 | private function addBindingType(BindingTypeDescriptor $typeDescriptor) |
||
801 | |||
802 | private function syncTypeName($typeName) |
||
806 | |||
807 | 5 | private function addBindingDescriptorToModuleFile(BindingDescriptor $bindingDescriptor) |
|
811 | |||
812 | private function removeBindingDescriptorFromModuleFile(Uuid $uuid) |
||
816 | |||
817 | 24 | private function loadBindingDescriptor(BindingDescriptor $bindingDescriptor, Module $module) |
|
821 | |||
822 | private function unloadBindingDescriptor(BindingDescriptor $bindingDescriptor) |
||
826 | |||
827 | private function addBinding(BindingDescriptor $bindingDescriptor) |
||
831 | |||
832 | 6 | private function syncBinding(Binding $binding) |
|
836 | } |
||
837 |
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.