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 | 5 | 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 | 4 | public function addAdapter(AdapterInterface $adapter) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Add configuration source for later use |
||
| 213 | * Config should have keys of component id and values of config. |
||
| 214 | * Example: |
||
| 215 | * ``` |
||
| 216 | * [ |
||
| 217 | * 'logger' => [ |
||
| 218 | * 'class' => Monolog\Logger\Logger, |
||
| 219 | * ], |
||
| 220 | * 'mangan' => [ |
||
| 221 | * '@logger' => 'logger' |
||
| 222 | * ] |
||
| 223 | * ] |
||
| 224 | * ``` |
||
| 225 | * Attributes starting with `@` denotes that link to other |
||
| 226 | * config component should be used. In example above, mangan field `logger` |
||
| 227 | * will be configured with monolog logger. |
||
| 228 | * @deprecated Use Maslosoft\EmbeDi\Adapters\ArrayAdapter instead |
||
| 229 | * @param mixed[] $source |
||
| 230 | */ |
||
| 231 | public function addConfig($source) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check whenever current configuration is stored. |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | 17 | public function isStored($object) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Configure existing object from previously stored configuration. |
||
| 247 | * Typically this will will be called in your class constructor. |
||
| 248 | * Will try to find configuration in adapters if it's not stored. |
||
| 249 | * TODO Use SourceManager here, before adapters |
||
| 250 | * TODO Create AdaptersManager and use here |
||
| 251 | * @param object $object |
||
| 252 | * @return object |
||
| 253 | */ |
||
| 254 | 17 | public function configure($object) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Apply configuration to object from array. |
||
| 294 | * |
||
| 295 | * This can also create object if passed configuration array have `class` field. |
||
| 296 | * |
||
| 297 | * Example of creating object: |
||
| 298 | * ``` |
||
| 299 | * $config = [ |
||
| 300 | * 'class' => Vendor\Component::class, |
||
| 301 | * 'title' => 'bar' |
||
| 302 | * ]; |
||
| 303 | * (new Embedi)->apply($config); |
||
| 304 | * ``` |
||
| 305 | * |
||
| 306 | * Example of applying config to existing object: |
||
| 307 | * ``` |
||
| 308 | * $config = [ |
||
| 309 | * 'title' => 'bar' |
||
| 310 | * ]; |
||
| 311 | * (new Embedi)->apply($config, new Vendor\Component); |
||
| 312 | * ``` |
||
| 313 | * |
||
| 314 | * If `$configuration` arguments is string, it will simply instantiate class: |
||
| 315 | * ``` |
||
| 316 | * (new Embedi)->apply('Vendor\Package\Component'); |
||
| 317 | * ``` |
||
| 318 | * |
||
| 319 | * @param string|mixed[][] $configuration |
||
| 320 | * @param object $object Object to configure, set to null to create new one |
||
| 321 | * @return object |
||
| 322 | */ |
||
| 323 | 11 | public function apply($configuration, $object = null) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Export object configuration to array |
||
| 361 | * @param object $object |
||
| 362 | * @param string[] $fields |
||
| 363 | * @return mixed[][] |
||
| 364 | */ |
||
| 365 | 4 | public function export($object, $fields = []) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Store object configuration. |
||
| 390 | * |
||
| 391 | * This will be typically called in init method of your component. |
||
| 392 | * After storing config, configuration will be available in `configure` method. |
||
| 393 | * `configure` method should be called in your class constructor. |
||
| 394 | * |
||
| 395 | * If you store config and have `configure` method call, |
||
| 396 | * after subsequent creations of your component will be configured by EmbeDi. |
||
| 397 | * |
||
| 398 | * Both methods could be called in constructor, if you don't need additional |
||
| 399 | * initialization code after configuring object. |
||
| 400 | * |
||
| 401 | * Example workflow: |
||
| 402 | * ``` |
||
| 403 | * class Component |
||
| 404 | * { |
||
| 405 | * public $title = ''; |
||
| 406 | * |
||
| 407 | * public function __construct() |
||
| 408 | * { |
||
| 409 | * (new EmbeDi)->configure($this); |
||
| 410 | * } |
||
| 411 | * |
||
| 412 | * public function init() |
||
| 413 | * { |
||
| 414 | * (new EmbeDi)->store($this); |
||
| 415 | * } |
||
| 416 | * } |
||
| 417 | * |
||
| 418 | * $c1 = new Component(); |
||
| 419 | * $c1->title = 'foo'; |
||
| 420 | * $c1->init(); |
||
| 421 | * |
||
| 422 | * $c2 = new Component(); |
||
| 423 | * |
||
| 424 | * echo $c2->title; // 'foo' |
||
| 425 | * ``` |
||
| 426 | * |
||
| 427 | * Parameter `$fields` tell's EmbeDi to store only subset of class fields. |
||
| 428 | * Example: |
||
| 429 | * ``` |
||
| 430 | * (new EmbeDi)->store($this, ['title']); |
||
| 431 | * ``` |
||
| 432 | * |
||
| 433 | * Parameter `$update` tell's EmbeDi to update existing configuration. |
||
| 434 | * By default configuration is not ovveriden on subsequent `store` calls. |
||
| 435 | * This is done on purpose, to not mess basic configuration. |
||
| 436 | * |
||
| 437 | * @param object $object Object to store |
||
| 438 | * @param string[] $fields Fields to store |
||
| 439 | * @param bool $update Whenever to update existing configuration |
||
| 440 | * @return mixed[] Stored data |
||
| 441 | */ |
||
| 442 | 7 | public function store($object, $fields = [], $update = false) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get class fields of object. By default all public and non static fields are returned. |
||
| 477 | * This can be overridden by passing `$fields` names of fields. These are not checked for existence. |
||
| 478 | * @param object $object |
||
| 479 | * @param string[] $fields |
||
| 480 | * @return string[] |
||
| 481 | */ |
||
| 482 | 8 | private function _getFields($object, $fields) |
|
| 497 | |||
| 498 | } |
||
| 499 |