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 ModelManager 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 ModelManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class ModelManager { |
||
57 | |||
58 | |||
59 | /** @var CoreQueryBuilder */ |
||
60 | private $coreRequestBuilder; |
||
61 | |||
62 | /** @var MemberRequest */ |
||
63 | private $memberRequest; |
||
64 | |||
65 | /** @var MembershipRequest */ |
||
66 | private $membershipRequest; |
||
67 | |||
68 | /** @var InterfaceService */ |
||
69 | private $interfaceService; |
||
70 | |||
71 | /** @var ConfigService */ |
||
72 | private $configService; |
||
73 | |||
74 | |||
75 | /** @var bool */ |
||
76 | private $fullDetails = false; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * ModelManager constructor. |
||
81 | * |
||
82 | * @param CoreQueryBuilder $coreRequestBuilder |
||
83 | * @param MemberRequest $memberRequest |
||
84 | * @param MembershipRequest $membershipRequest |
||
85 | * @param ConfigService $configService |
||
86 | */ |
||
87 | public function __construct( |
||
88 | CoreQueryBuilder $coreRequestBuilder, |
||
89 | MemberRequest $memberRequest, |
||
90 | MembershipRequest $membershipRequest, |
||
91 | InterfaceService $interfaceService, |
||
92 | ConfigService $configService |
||
93 | ) { |
||
94 | $this->coreRequestBuilder = $coreRequestBuilder; |
||
95 | $this->memberRequest = $memberRequest; |
||
96 | $this->membershipRequest = $membershipRequest; |
||
97 | $this->interfaceService = $interfaceService; |
||
98 | $this->configService = $configService; |
||
99 | } |
||
100 | |||
101 | |||
102 | /** |
||
103 | * @return ConfigService |
||
104 | */ |
||
105 | public function getConfigService(): ConfigService { |
||
106 | return $this->configService; |
||
107 | } |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @param Circle $circle |
||
112 | */ |
||
113 | public function getMembers(Circle $circle): void { |
||
114 | try { |
||
115 | $circle->setMembers($this->memberRequest->getMembers($circle->getSingleId())); |
||
116 | } catch (RequestBuilderException $e) { |
||
117 | // TODO: debug log |
||
118 | } |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * @param Circle $circle |
||
124 | * @param bool $detailed |
||
125 | */ |
||
126 | public function getInheritedMembers(Circle $circle, bool $detailed = false): void { |
||
127 | try { |
||
128 | $circle->setInheritedMembers( |
||
129 | $this->memberRequest->getInheritedMembers($circle->getSingleId(), $detailed), |
||
130 | $detailed |
||
131 | ); |
||
132 | } catch (RequestBuilderException $e) { |
||
133 | // TODO: debug log |
||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @param IMemberships $member |
||
140 | */ |
||
141 | public function getMemberships(IMemberships $member): void { |
||
142 | $memberships = $this->membershipRequest->getMemberships($member->getSingleId()); |
||
143 | $member->setMemberships($memberships); |
||
144 | } |
||
145 | |||
146 | |||
147 | /** |
||
148 | * @param ManagedModel $model |
||
149 | * @param array $data |
||
150 | * @param string $base |
||
151 | */ |
||
152 | public function manageImportFromDatabase(ManagedModel $model, array $data, string $base): void { |
||
181 | |||
182 | |||
183 | private function importBasedOnPath(ManagedModel $model, array $data, string $path, string $prefix) { |
||
204 | |||
205 | |||
206 | /** |
||
207 | * @param Circle $circle |
||
208 | * @param array $data |
||
209 | * @param string $path |
||
210 | * @param string $prefix |
||
211 | */ |
||
212 | View Code Duplication | private function importIntoCircle(Circle $circle, array $data, string $path, string $prefix): void { |
|
233 | |||
234 | |||
235 | /** |
||
236 | * @param Member $member |
||
237 | * @param array $data |
||
238 | * @param string $path |
||
239 | * @param string $prefix |
||
240 | */ |
||
241 | private function importIntoMember(Member $member, array $data, string $path, string $prefix): void { |
||
289 | |||
290 | |||
291 | /** |
||
292 | * @param FederatedUser $federatedUser |
||
293 | * @param array $data |
||
294 | * @param string $path |
||
295 | * @param string $prefix |
||
296 | */ |
||
297 | private function importIntoFederatedUser( |
||
314 | |||
315 | |||
316 | /** |
||
317 | * @param ShareWrapper $shareWrapper |
||
318 | * @param array $data |
||
319 | * @param string $path |
||
320 | * @param string $prefix |
||
321 | */ |
||
322 | private function importIntoShareWrapper( |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @param Mount $mount |
||
370 | * @param array $data |
||
371 | * @param string $path |
||
372 | * @param string $prefix |
||
373 | */ |
||
374 | View Code Duplication | private function importIntoMount( |
|
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | public function getLocalInstance(): string { |
||
407 | |||
408 | /** |
||
409 | * @param string $instance |
||
410 | * |
||
411 | * @return string |
||
412 | * @throws UnknownInterfaceException |
||
413 | */ |
||
414 | public function fixInstance(string $instance): string { |
||
425 | |||
426 | /** |
||
427 | * @param bool $full |
||
428 | */ |
||
429 | public function setFullDetails(bool $full): void { |
||
432 | |||
433 | /** |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function isFullDetails(): bool { |
||
439 | |||
440 | } |
||
441 | |||
442 |
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.