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 ConfigHelper 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 ConfigHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ConfigHelper |
||
12 | { |
||
13 | /** @var ServiceHelper */ |
||
14 | private $serviceHelper; |
||
15 | |||
16 | /** @var string $token */ |
||
17 | private $token = false; |
||
18 | |||
19 | /** @var string $accountId */ |
||
20 | private $propertyId = false; |
||
21 | |||
22 | /** @var string $accountId */ |
||
23 | private $accountId = false; |
||
24 | |||
25 | /** @var string $profileId */ |
||
26 | private $profileId = false; |
||
27 | |||
28 | /** @var EntityManager $em */ |
||
29 | private $em; |
||
30 | |||
31 | /** |
||
32 | * ConfigHelper constructor. |
||
33 | * |
||
34 | * @param ServiceHelper $serviceHelper |
||
35 | * @param EntityManager $em |
||
36 | * |
||
37 | * @throws \Doctrine\ORM\OptimisticLockException |
||
38 | */ |
||
39 | public function __construct(ServiceHelper $serviceHelper, EntityManager $em) |
||
45 | |||
46 | /** |
||
47 | * @param bool $configId |
||
48 | * |
||
49 | * @throws \Doctrine\ORM\OptimisticLockException |
||
50 | */ |
||
51 | public function init($configId = false) |
||
74 | |||
75 | /* =============================== TOKEN =============================== */ |
||
76 | |||
77 | /** |
||
78 | * @param bool $configId |
||
79 | * |
||
80 | * @return string |
||
81 | * @throws \Doctrine\ORM\OptimisticLockException |
||
82 | */ |
||
83 | View Code Duplication | private function getToken($configId = false) |
|
97 | |||
98 | /** |
||
99 | * @param string $token |
||
100 | * @param bool $configId |
||
101 | * |
||
102 | * @throws \Doctrine\ORM\OptimisticLockException |
||
103 | */ |
||
104 | public function saveToken($token, $configId = false) |
||
109 | |||
110 | /** |
||
111 | * @return bool |
||
112 | * |
||
113 | * @throws \Doctrine\ORM\OptimisticLockException |
||
114 | */ |
||
115 | public function tokenIsSet() |
||
119 | |||
120 | /* =============================== ACCOUNT =============================== */ |
||
121 | |||
122 | /** |
||
123 | * Get a list of all available accounts |
||
124 | * |
||
125 | * @return array $data A list of all properties |
||
126 | */ |
||
127 | public function getAccounts() |
||
142 | |||
143 | /** |
||
144 | * @param bool $configId |
||
145 | * |
||
146 | * @return string |
||
147 | * @throws \Doctrine\ORM\OptimisticLockException |
||
148 | */ |
||
149 | View Code Duplication | public function getAccountId($configId = false) |
|
163 | |||
164 | /** |
||
165 | * @param string $accountId |
||
166 | * @param bool $configId |
||
167 | * |
||
168 | * @throws \Doctrine\ORM\OptimisticLockException |
||
169 | */ |
||
170 | public function saveAccountId($accountId, $configId = false) |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | * @throws \Doctrine\ORM\OptimisticLockException |
||
179 | */ |
||
180 | public function accountIsSet() |
||
184 | |||
185 | /* =============================== PROPERTY =============================== */ |
||
186 | |||
187 | /** |
||
188 | * @param bool $accountId |
||
189 | * |
||
190 | * @return array|bool |
||
191 | * @throws \Doctrine\ORM\OptimisticLockException |
||
192 | */ |
||
193 | public function getProperties($accountId = false) |
||
219 | |||
220 | /** |
||
221 | * @param bool $configId |
||
222 | * |
||
223 | * @return string |
||
224 | * @throws \Doctrine\ORM\OptimisticLockException |
||
225 | */ |
||
226 | View Code Duplication | public function getPropertyId($configId = false) |
|
240 | |||
241 | /** |
||
242 | * @param $propertyId |
||
243 | * @param bool $configId |
||
244 | * |
||
245 | * @throws \Doctrine\ORM\OptimisticLockException |
||
246 | */ |
||
247 | public function savePropertyId($propertyId, $configId = false) |
||
252 | |||
253 | /** |
||
254 | * @return bool |
||
255 | * @throws \Doctrine\ORM\OptimisticLockException |
||
256 | */ |
||
257 | public function propertyIsSet() |
||
261 | |||
262 | /* =============================== PROFILE =============================== */ |
||
263 | |||
264 | /** |
||
265 | * @param bool $accountId |
||
266 | * @param bool $propertyId |
||
267 | * |
||
268 | * @return array|bool |
||
269 | * @throws \Doctrine\ORM\OptimisticLockException |
||
270 | */ |
||
271 | public function getProfiles($accountId = false, $propertyId = false) |
||
304 | |||
305 | /** |
||
306 | * @param bool $configId |
||
307 | * |
||
308 | * @return string |
||
309 | * @throws \Doctrine\ORM\OptimisticLockException |
||
310 | */ |
||
311 | View Code Duplication | public function getProfileId($configId = false) |
|
325 | |||
326 | /** |
||
327 | * @param $profileId |
||
328 | * @param bool $configId |
||
329 | * |
||
330 | * @throws \Doctrine\ORM\OptimisticLockException |
||
331 | */ |
||
332 | public function saveProfileId($profileId, $configId = false) |
||
337 | |||
338 | /** |
||
339 | * @return bool |
||
340 | * @throws \Doctrine\ORM\OptimisticLockException |
||
341 | */ |
||
342 | public function profileIsSet() |
||
346 | |||
347 | /** |
||
348 | * Get the active profile |
||
349 | * |
||
350 | * @return mixed |
||
351 | * |
||
352 | * @throws \Exception |
||
353 | */ |
||
354 | public function getActiveProfile() |
||
369 | |||
370 | /* =============================== PROFILE SEGMENTS =============================== */ |
||
371 | /** |
||
372 | * get all segments for the saved profile |
||
373 | * |
||
374 | * @return array |
||
375 | */ |
||
376 | public function getProfileSegments() |
||
403 | |||
404 | /* =============================== CONFIG =============================== */ |
||
405 | |||
406 | /** |
||
407 | * @param string $configName |
||
408 | * @param bool $configId |
||
409 | * |
||
410 | * @throws \Doctrine\ORM\OptimisticLockException |
||
411 | */ |
||
412 | public function saveConfigName($configName, $configId = false) |
||
416 | |||
417 | /* =============================== AUTH URL =============================== */ |
||
418 | |||
419 | /** |
||
420 | * get the authUrl |
||
421 | * |
||
422 | * @return string $authUrl |
||
423 | */ |
||
424 | public function getAuthUrl() |
||
432 | } |
||
433 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.