Complex classes like Injector 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 Injector, and based on these observations, apply Extract Interface, too.
1 | <?php /** MicroInjector */ |
||
18 | class Injector implements InjectorInterface |
||
19 | { |
||
20 | /** @var array $CONFIG Configuration */ |
||
21 | private static $CONFIG = []; |
||
22 | /** @var array $INJECTS Configured injects */ |
||
23 | private static $INJECTS = []; |
||
24 | |||
25 | /** |
||
26 | * Injector constructor. |
||
27 | * |
||
28 | * @access public |
||
29 | * @param string $configPath |
||
30 | * @result void |
||
31 | */ |
||
32 | public function __construct($configPath = '') |
||
39 | |||
40 | /** |
||
41 | * @return null |
||
42 | */ |
||
43 | public function build() |
||
47 | |||
48 | /** |
||
49 | * @param string $name |
||
50 | * @return string |
||
51 | */ |
||
52 | public function param($name) |
||
56 | |||
57 | /** |
||
58 | * Add requirement to injector |
||
59 | * |
||
60 | * @access public |
||
61 | * @param string $name |
||
62 | * @param mixed $component |
||
63 | * @return void |
||
64 | */ |
||
65 | public function addRequirement($name, $component) |
||
73 | |||
74 | /** |
||
75 | * Build object with injector |
||
76 | * |
||
77 | * class LogInject extends Injector { |
||
78 | * public function build() { |
||
79 | * return $this->get('logger'); |
||
80 | * } |
||
81 | * } |
||
82 | * $log = (new LogInject())->build(); |
||
83 | * |
||
84 | * @access protected |
||
85 | * @param string $name |
||
86 | * @return boolean|mixed |
||
87 | */ |
||
88 | protected function get($name) |
||
104 | |||
105 | /** |
||
106 | * Load injection |
||
107 | * |
||
108 | * @access public |
||
109 | * |
||
110 | * @param string $name Name injection |
||
111 | * |
||
112 | * @return boolean|mixed |
||
113 | */ |
||
114 | private function loadInjection($name) |
||
159 | |||
160 | /** |
||
161 | * Build params from array |
||
162 | * |
||
163 | * @access private |
||
164 | * @param array $params |
||
165 | * @return array |
||
166 | */ |
||
167 | private function buildParams(array $params) |
||
182 | |||
183 | /** |
||
184 | * Build calls arguments |
||
185 | * |
||
186 | * @access private |
||
187 | * @param array $params |
||
188 | * @return array |
||
189 | */ |
||
190 | private function buildCalls(array $params) |
||
212 | |||
213 | /** |
||
214 | * Make object with arguments |
||
215 | * |
||
216 | * @access private |
||
217 | * |
||
218 | * @param string $className |
||
219 | * @param array $arguments |
||
220 | * |
||
221 | * @return mixed |
||
222 | */ |
||
223 | private function makeObject($className, array $arguments = []) |
||
238 | } |