Complex classes like SettingsManager 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 SettingsManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class SettingsManager |
||
35 | { |
||
36 | /** |
||
37 | * Symfony event dispatcher. |
||
38 | * |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | private $eventDispatcher; |
||
42 | |||
43 | /** |
||
44 | * Elasticsearch manager which handles setting repository. |
||
45 | * |
||
46 | * @var Manager |
||
47 | */ |
||
48 | private $manager; |
||
49 | |||
50 | /** |
||
51 | * Settings repository. |
||
52 | * |
||
53 | * @var Repository |
||
54 | */ |
||
55 | private $repo; |
||
56 | |||
57 | /** |
||
58 | * Cache pool container. |
||
59 | * |
||
60 | * @var CacheProvider |
||
61 | */ |
||
62 | private $cache; |
||
63 | |||
64 | /** |
||
65 | * Cookie storage for active cookies. |
||
66 | * |
||
67 | * @var GenericCookie |
||
68 | */ |
||
69 | private $activeProfilesCookie; |
||
70 | |||
71 | /** |
||
72 | * Cookie storage for active cookies. |
||
73 | * |
||
74 | * @var GenericCookie |
||
75 | */ |
||
76 | private $activeExperimentProfilesCookie; |
||
77 | |||
78 | /** |
||
79 | * Active profiles setting name to store in the cache engine. |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | private $activeProfilesSettingName; |
||
84 | |||
85 | /** |
||
86 | * Active profiles list collected from es, cache and cookie. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | private $activeProfilesList = []; |
||
91 | |||
92 | /** |
||
93 | * Active experiments setting name to store in the cache engine. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $activeExperimentsSettingName; |
||
98 | |||
99 | /** |
||
100 | * @param Repository $repo |
||
101 | * @param EventDispatcherInterface $eventDispatcher |
||
102 | */ |
||
103 | public function __construct( |
||
111 | |||
112 | /** |
||
113 | * @return CacheProvider |
||
114 | */ |
||
115 | public function getCache() |
||
119 | |||
120 | /** |
||
121 | * @param CacheProvider $cache |
||
122 | */ |
||
123 | public function setCache($cache) |
||
127 | |||
128 | /** |
||
129 | * @return GenericCookie |
||
130 | */ |
||
131 | public function getActiveProfilesCookie() |
||
135 | |||
136 | /** |
||
137 | * @param GenericCookie $activeProfilesCookie |
||
138 | */ |
||
139 | public function setActiveProfilesCookie($activeProfilesCookie) |
||
143 | |||
144 | /** |
||
145 | * @return GenericCookie |
||
146 | */ |
||
147 | public function getActiveExperimentProfilesCookie() |
||
151 | |||
152 | /** |
||
153 | * @param GenericCookie $activeExperimentProfilesCookie |
||
154 | */ |
||
155 | public function setActiveExperimentProfilesCookie($activeExperimentProfilesCookie) |
||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getActiveProfilesSettingName() |
||
167 | |||
168 | /** |
||
169 | * @param string $activeProfilesSettingName |
||
170 | */ |
||
171 | public function setActiveProfilesSettingName($activeProfilesSettingName) |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getActiveProfilesList() |
||
183 | |||
184 | /** |
||
185 | * @param array $activeProfilesList |
||
186 | */ |
||
187 | public function setActiveProfilesList(array $activeProfilesList) |
||
191 | |||
192 | /** |
||
193 | * @param array $activeProfilesList |
||
194 | */ |
||
195 | public function appendActiveProfilesList(array $activeProfilesList) |
||
199 | |||
200 | /** |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getActiveExperimentsSettingName() |
||
207 | |||
208 | /** |
||
209 | * @param string $activeExperimentsSettingName |
||
210 | */ |
||
211 | public function setActiveExperimentsSettingName($activeExperimentsSettingName) |
||
215 | |||
216 | /** |
||
217 | * Creates setting. |
||
218 | * |
||
219 | * @param array $data |
||
220 | * |
||
221 | * @return Setting |
||
222 | */ |
||
223 | public function create(array $data = []) |
||
259 | |||
260 | /** |
||
261 | * Overwrites setting parameters with given name. |
||
262 | * |
||
263 | * @param string $name |
||
264 | * @param array $data |
||
265 | * |
||
266 | * @return Setting |
||
267 | */ |
||
268 | public function update($name, $data = []) |
||
269 | { |
||
270 | $setting = $this->get($name); |
||
271 | |||
272 | if (!$setting) { |
||
273 | throw new \LogicException(sprintf('Setting %s not exist.', $name)); |
||
274 | } |
||
275 | |||
276 | $this->eventDispatcher->dispatch(Events::PRE_UPDATE, new SettingActionEvent($name, $data, $setting)); |
||
277 | |||
278 | #TODO Add populate function to document class |
||
279 | foreach ($data as $key => $value) { |
||
280 | $setting->{'set'.ucfirst($key)}($value); |
||
281 | } |
||
282 | |||
283 | $this->manager->persist($setting); |
||
284 | $this->manager->commit(); |
||
285 | $this->cache->delete($name); |
||
286 | |||
287 | if ($setting->getType() == 'experiment' || $setting->getName() == 'ongr_active_experiments') { |
||
288 | $this->getActiveExperimentProfilesCookie()->setClear(true); |
||
289 | } |
||
290 | |||
291 | $this->eventDispatcher->dispatch(Events::PRE_UPDATE, new SettingActionEvent($name, $data, $setting)); |
||
292 | |||
293 | return $setting; |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Deletes a setting. |
||
298 | * |
||
299 | * @param string $name |
||
300 | * |
||
301 | * @throws \LogicException |
||
302 | * @return array |
||
303 | */ |
||
304 | public function delete($name) |
||
325 | |||
326 | /** |
||
327 | * Returns setting object. |
||
328 | * |
||
329 | * @param string $name |
||
330 | * |
||
331 | * @return Setting |
||
332 | */ |
||
333 | public function get($name) |
||
344 | |||
345 | /** |
||
346 | * Returns setting object. |
||
347 | * |
||
348 | * @param string $name |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | public function has($name) |
||
363 | |||
364 | /** |
||
365 | * Get setting value by current active profiles setting. |
||
366 | * |
||
367 | * @param string $name |
||
368 | * @param bool $default |
||
369 | * |
||
370 | * @return string|array|bool |
||
371 | */ |
||
372 | public function getValue($name, $default = null) |
||
382 | |||
383 | /** |
||
384 | * Get setting value by checking also from cache engine. |
||
385 | * |
||
386 | * @param string $name |
||
387 | * @param bool $checkWithActiveProfiles Checks if setting is in active profile. |
||
388 | * |
||
389 | * @return mixed |
||
390 | */ |
||
391 | public function getCachedValue($name, $checkWithActiveProfiles = true) |
||
416 | |||
417 | /** |
||
418 | * Get all full profile information. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function getAllProfiles() |
||
458 | |||
459 | /** |
||
460 | * Returns profiles settings array |
||
461 | * |
||
462 | * @param string $profile |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | public function getProfileSettings($profile) |
||
477 | |||
478 | /** |
||
479 | * Returns cached active profiles names list. |
||
480 | * |
||
481 | * @return array |
||
482 | */ |
||
483 | public function getActiveProfiles() |
||
506 | |||
507 | /** |
||
508 | * @return DocumentIterator |
||
509 | */ |
||
510 | public function getAllExperiments() |
||
516 | |||
517 | /** |
||
518 | * Returns an array of active experiments names either from cache or from es. |
||
519 | * If none are found, the setting with no active experiments is created. |
||
520 | * |
||
521 | * @return array |
||
522 | */ |
||
523 | public function getActiveExperiments() |
||
546 | |||
547 | /** |
||
548 | * @param string $name |
||
549 | */ |
||
550 | public function toggleExperiment($name) |
||
574 | |||
575 | /** |
||
576 | * Get full experiment by caching. |
||
577 | * |
||
578 | * @param string $name |
||
579 | * |
||
580 | * @return array|null |
||
581 | * |
||
582 | * @throws LogicException |
||
583 | */ |
||
584 | public function getCachedExperiment($name) |
||
604 | } |
||
605 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: