Complex classes like EmbeDi 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 EmbeDi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class EmbeDi |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * This is default instance name, and component name. |
||
32 | */ |
||
33 | const DefaultInstanceId = 'embedi'; |
||
|
|||
34 | |||
35 | /** |
||
36 | * Class field in configuration arrays |
||
37 | * @see apply() |
||
38 | * @see export() |
||
39 | * @var string |
||
40 | */ |
||
41 | public $classField = 'class'; |
||
42 | |||
43 | /** |
||
44 | * Instance id |
||
45 | * @var string |
||
46 | */ |
||
47 | private $_instanceId = ''; |
||
48 | |||
49 | /** |
||
50 | * Preset ID |
||
51 | * @var string |
||
52 | */ |
||
53 | private $_presetId = ''; |
||
54 | |||
55 | /** |
||
56 | * Storage container |
||
57 | * @var EmbeDiStore |
||
58 | */ |
||
59 | private $storage = null; |
||
60 | |||
61 | /** |
||
62 | * Configs source manager |
||
63 | * @var SourceManager |
||
64 | */ |
||
65 | private $sm = null; |
||
66 | |||
67 | /** |
||
68 | * Flyweight instances of EmbeDi |
||
69 | * @var EmbeDi[] |
||
70 | */ |
||
71 | private static $_instances = []; |
||
72 | |||
73 | /** |
||
74 | * Create container with provided id |
||
75 | * @param string $instanceId |
||
76 | * @param string $presetId If set will lookup configuration in depper array level |
||
77 | * @param array $config Configuration of EmbeDi |
||
78 | */ |
||
79 | 17 | public function __construct($instanceId = EmbeDi::DefaultInstanceId, $presetId = null, $config = []) |
|
107 | |||
108 | 2 | public function __get($name) |
|
113 | |||
114 | 3 | public function __set($name, $value) |
|
119 | |||
120 | /** |
||
121 | * Get flyweight instance of embedi. |
||
122 | * This will create instance only if `$instanceId` insntace id does not exists. |
||
123 | * If named instance exists, or was ever create - existing instance will be used. |
||
124 | * Use this function especially when require many `EmbeDi` calls, |
||
125 | * for instance when creating `EmbeDi` in loops: |
||
126 | * ```php |
||
127 | * foreach($configs as $config) |
||
128 | * { |
||
129 | * (new EmbeDi)->apply($config); |
||
130 | * } |
||
131 | * ``` |
||
132 | * In abowe example at each loop iteration new `EmbeDi` instance is created. |
||
133 | * While it is still lightweight, it's unnessesary overhead. |
||
134 | * |
||
135 | * This can be made in slightly more optimized way by using `fly` function: |
||
136 | * ```php |
||
137 | * foreach($configs as $config) |
||
138 | * { |
||
139 | * EmbeDi::fly()->apply($config); |
||
140 | * } |
||
141 | * ``` |
||
142 | * In above example only one instance of `EmbeDi` is used. |
||
143 | * @param string $instanceId |
||
144 | * @return EmbeDi |
||
145 | */ |
||
146 | 4 | public static function fly($instanceId = EmbeDi::DefaultInstanceId, $presetId = null) |
|
162 | |||
163 | 2 | public function getAdapters() |
|
167 | |||
168 | /** |
||
169 | * TODO Create AdaptersManager |
||
170 | */ |
||
171 | 7 | public function setAdapters($adapters) |
|
196 | |||
197 | /** |
||
198 | * Add configuration adapter |
||
199 | * TODO Create AdaptersManager |
||
200 | * @param AdapterInterface $adapter |
||
201 | */ |
||
202 | 3 | public function addAdapter(AdapterInterface $adapter) |
|
206 | |||
207 | /** |
||
208 | * Add configuration source for later use |
||
209 | * Config should have keys of component id and values of config. |
||
210 | * Example: |
||
211 | * ``` |
||
212 | * [ |
||
213 | * 'logger' => [ |
||
214 | * 'class' => Monolog\Logger\Logger, |
||
215 | * ], |
||
216 | * 'mangan' => [ |
||
217 | * '@logger' => 'logger' |
||
218 | * ] |
||
219 | * ] |
||
220 | * ``` |
||
221 | * Attributes starting with `@` denotes that link to other |
||
222 | * config component should be used. In example above, mangan field `logger` |
||
223 | * will be configured with monolog logger. |
||
224 | * @deprecated Use Maslosoft\EmbeDi\Adapters\ArrayAdapter instead |
||
225 | * @param mixed[] $source |
||
226 | */ |
||
227 | public function addConfig($source) |
||
231 | |||
232 | /** |
||
233 | * Check whenever current configuration is stored. |
||
234 | * @return bool |
||
235 | */ |
||
236 | 16 | public function isStored($object) |
|
240 | |||
241 | /** |
||
242 | * Configure existing object from previously stored configuration. |
||
243 | * Typically this will will be called in your class constructor. |
||
244 | * Will try to find configuration in adapters if it's not stored. |
||
245 | * TODO Use SourceManager here, before adapters |
||
246 | * TODO Create AdaptersManager and use here |
||
247 | * @param object $object |
||
248 | * @return object |
||
249 | */ |
||
250 | 16 | public function configure($object) |
|
287 | |||
288 | /** |
||
289 | * Apply configuration to object from array. |
||
290 | * |
||
291 | * This can also create object if passed configuration array have `class` field. |
||
292 | * |
||
293 | * Example of creating object: |
||
294 | * ``` |
||
295 | * $config = [ |
||
296 | * 'class' => Vendor\Component::class, |
||
297 | * 'title' => 'bar' |
||
298 | * ]; |
||
299 | * (new Embedi)->apply($config); |
||
300 | * ``` |
||
301 | * |
||
302 | * Example of applying config to existing object: |
||
303 | * ``` |
||
304 | * $config = [ |
||
305 | * 'title' => 'bar' |
||
306 | * ]; |
||
307 | * (new Embedi)->apply($config, new Vendor\Component); |
||
308 | * ``` |
||
309 | * |
||
310 | * If `$configuration` arguments is string, it will simply instantiate class: |
||
311 | * ``` |
||
312 | * (new Embedi)->apply('Vendor\Package\Component'); |
||
313 | * ``` |
||
314 | * |
||
315 | * @param string|mixed[][] $configuration |
||
316 | * @param object $object Object to configure, set to null to create new one |
||
317 | * @return object |
||
318 | */ |
||
319 | 10 | public function apply($configuration, $object = null) |
|
354 | |||
355 | /** |
||
356 | * Export object configuration to array |
||
357 | * @param object $object |
||
358 | * @param string[] $fields |
||
359 | * @return mixed[][] |
||
360 | */ |
||
361 | 4 | public function export($object, $fields = []) |
|
383 | |||
384 | /** |
||
385 | * Store object configuration. |
||
386 | * |
||
387 | * This will be typically called in init method of your component. |
||
388 | * After storing config, configuration will be available in `configure` method. |
||
389 | * `configure` method should be called in your class constructor. |
||
390 | * |
||
391 | * If you store config and have `configure` method call, |
||
392 | * after subsequent creations of your component will be configured by EmbeDi. |
||
393 | * |
||
394 | * Both methods could be called in constructor, if you don't need additional |
||
395 | * initialization code after configuring object. |
||
396 | * |
||
397 | * Example workflow: |
||
398 | * ``` |
||
399 | * class Component |
||
400 | * { |
||
401 | * public $title = ''; |
||
402 | * |
||
403 | * public function __construct() |
||
404 | * { |
||
405 | * (new EmbeDi)->configure($this); |
||
406 | * } |
||
407 | * |
||
408 | * public function init() |
||
409 | * { |
||
410 | * (new EmbeDi)->store($this); |
||
411 | * } |
||
412 | * } |
||
413 | * |
||
414 | * $c1 = new Component(); |
||
415 | * $c1->title = 'foo'; |
||
416 | * $c1->init(); |
||
417 | * |
||
418 | * $c2 = new Component(); |
||
419 | * |
||
420 | * echo $c2->title; // 'foo' |
||
421 | * ``` |
||
422 | * |
||
423 | * Parameter `$fields` tell's EmbeDi to store only subset of class fields. |
||
424 | * Example: |
||
425 | * ``` |
||
426 | * (new EmbeDi)->store($this, ['title']); |
||
427 | * ``` |
||
428 | * |
||
429 | * Parameter `$update` tell's EmbeDi to update existing configuration. |
||
430 | * By default configuration is not ovveriden on subsequent `store` calls. |
||
431 | * This is done on purpose, to not mess basic configuration. |
||
432 | * |
||
433 | * @param object $object Object to store |
||
434 | * @param string[] $fields Fields to store |
||
435 | * @param bool $update Whenever to update existing configuration |
||
436 | * @return mixed[] Stored data |
||
437 | */ |
||
438 | 7 | public function store($object, $fields = [], $update = false) |
|
470 | |||
471 | /** |
||
472 | * Get class fields of object. By default all public and non static fields are returned. |
||
473 | * This can be overridden by passing `$fields` names of fields. These are not checked for existence. |
||
474 | * @param object $object |
||
475 | * @param string[] $fields |
||
476 | * @return string[] |
||
477 | */ |
||
478 | 8 | private function _getFields($object, $fields) |
|
493 | |||
494 | } |
||
495 |