Complex classes like Instantiator 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 Instantiator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Instantiator |
||
8 | { |
||
9 | const MODE_DEPENDENCY = false; |
||
10 | const MODE_FACTORY = 'new'; |
||
11 | |||
12 | protected $instance; |
||
13 | protected $reflection; |
||
14 | protected $constructor = array(); |
||
15 | protected $className; |
||
16 | protected $params = array(); |
||
17 | protected $staticMethodCalls = array(); |
||
18 | protected $methodCalls = array(); |
||
19 | protected $propertySetters = array(); |
||
20 | protected $mode = self::MODE_DEPENDENCY; |
||
21 | |||
22 | 34 | public function __construct($className) |
|
23 | { |
||
24 | 34 | if (false !== stripos($className, ' ')) { |
|
25 | 1 | list($mode, $className) = explode(' ', $className, 2); |
|
26 | 1 | $this->mode = $mode; |
|
27 | 1 | } |
|
28 | 34 | $this->reflection = new ReflectionClass($className); |
|
29 | 34 | $this->constructor = $this->findConstructorParams($this->reflection); |
|
30 | 34 | $this->className = $className; |
|
31 | 34 | } |
|
32 | |||
33 | 19 | public function getMode() |
|
37 | |||
38 | 20 | public function __invoke() |
|
42 | |||
43 | public function getClassName() |
||
44 | { |
||
45 | return $this->className; |
||
46 | } |
||
47 | |||
48 | 31 | public function getInstance($forceNew = false) |
|
49 | { |
||
50 | 31 | if ($this->mode == static::MODE_FACTORY) { |
|
51 | 1 | $this->instance = null; |
|
52 | 1 | } |
|
53 | |||
54 | 31 | if ($this->instance && !$forceNew) { |
|
55 | 2 | return $this->instance; |
|
56 | } |
||
57 | |||
58 | 31 | $className = $this->className; |
|
59 | 31 | $staticMethods = count($this->staticMethodCalls); |
|
60 | 31 | foreach ($this->staticMethodCalls as $methodCalls) { |
|
61 | 2 | $this->performMethodCalls( |
|
62 | 2 | $className, |
|
63 | 2 | $methodCalls, |
|
64 | function ($result) use ($className, &$instance, $staticMethods) { |
||
65 | 2 | if ($result instanceof $className || ($staticMethods == 1 && is_object($result))) { |
|
66 | 2 | $instance = $result; |
|
67 | 2 | } |
|
68 | 2 | } |
|
69 | 2 | ); |
|
70 | 31 | } |
|
71 | |||
72 | 31 | $constructor = $this->reflection->getConstructor(); |
|
73 | 31 | $hasConstructor = ($constructor) ? $constructor->isPublic() : false; |
|
74 | 31 | if (empty($instance)) { |
|
75 | 29 | if (empty($this->constructor) || !$hasConstructor) { |
|
76 | 12 | $instance = new $className(); |
|
|
|||
77 | 12 | } else { |
|
78 | 18 | $instance = $this->reflection->newInstanceArgs( |
|
79 | 18 | $this->cleanupParams($this->constructor) |
|
80 | 18 | ); |
|
81 | } |
||
82 | 29 | } |
|
83 | |||
84 | 31 | foreach ($this->propertySetters as $property => $value) { |
|
85 | 9 | $instance->{$property} = $this->lazyLoad($value); |
|
86 | 31 | } |
|
87 | |||
88 | 31 | foreach ($this->methodCalls as $methodCalls) { |
|
89 | 7 | $this->performMethodCalls($instance, $methodCalls); |
|
90 | 31 | } |
|
91 | |||
92 | 31 | return $this->instance = $instance; |
|
93 | } |
||
94 | |||
95 | 3 | public function getParam($name) |
|
99 | |||
100 | public function setInstance($instance) |
||
101 | { |
||
102 | $this->instance = $instance; |
||
103 | } |
||
104 | |||
105 | 30 | public function setParam($name, $value) |
|
106 | { |
||
107 | 30 | $value = $this->processValue($value); |
|
108 | |||
109 | 30 | if ($this->matchStaticMethod($name)) { |
|
110 | 2 | $this->staticMethodCalls[] = array($name, $value); |
|
111 | 30 | } elseif ($this->matchConstructorParam($name)) { |
|
112 | 8 | $this->constructor[$name] = $value; |
|
113 | 28 | } elseif ($this->matchFullConstructor($name, $value)) { |
|
114 | 4 | $this->constructor = $value; |
|
115 | 22 | } elseif ($this->matchMethod($name)) { |
|
116 | 7 | $this->methodCalls[] = array($name, $value); |
|
117 | 7 | } else { |
|
118 | 12 | $this->propertySetters[$name] = $value; |
|
119 | } |
||
120 | |||
121 | 30 | $this->params[$name] = $value; |
|
122 | 30 | } |
|
123 | |||
124 | public function getParams() |
||
128 | |||
129 | 21 | protected function cleanupParams(array $params) |
|
130 | { |
||
131 | 21 | while (null === end($params)) { |
|
132 | 11 | unset($params[key($params)]); |
|
133 | 11 | } |
|
134 | |||
135 | 21 | foreach ($params as &$p) { |
|
136 | 16 | $p = $this->lazyLoad($p); |
|
137 | 21 | } |
|
138 | |||
139 | 21 | return $params; |
|
140 | } |
||
141 | |||
142 | 25 | protected function lazyLoad($value) |
|
146 | |||
147 | 34 | protected function findConstructorParams(ReflectionClass $class) |
|
148 | { |
||
149 | 34 | $params = array(); |
|
150 | 34 | $constructor = $class->getConstructor(); |
|
151 | |||
152 | 34 | if (!$constructor) { |
|
153 | 14 | return array(); |
|
154 | } |
||
155 | |||
156 | 21 | foreach ($constructor->getParameters() as $param) { |
|
157 | 20 | $params[$param->getName()] = $param->isDefaultValueAvailable() ? |
|
158 | 20 | $param->getDefaultValue() : null; |
|
159 | 21 | } |
|
160 | |||
161 | 21 | return $params; |
|
162 | } |
||
163 | |||
164 | 30 | protected function processValue($value) |
|
165 | { |
||
166 | 30 | if (is_array($value)) { |
|
167 | 15 | foreach ($value as $valueKey => $subValue) { |
|
168 | 15 | $value[$valueKey] = $this->processValue($subValue); |
|
169 | 15 | } |
|
170 | 15 | } |
|
171 | |||
172 | 30 | return $value; |
|
173 | } |
||
174 | |||
175 | 28 | protected function matchConstructorParam($name) |
|
179 | |||
180 | 22 | protected function matchFullConstructor($name, &$value) |
|
181 | { |
||
182 | return $name == '__construct' |
||
183 | 22 | || ($name == $this->className && stripos($this->className, '\\')); |
|
184 | } |
||
185 | |||
186 | 18 | protected function matchMethod($name) |
|
190 | |||
191 | 30 | protected function matchStaticMethod($name) |
|
196 | |||
197 | 9 | protected function performMethodCalls($class, array $methodCalls, $resultCallback = null) |
|
198 | { |
||
199 | 9 | list($methodName, $calls) = $methodCalls; |
|
200 | 9 | $resultCallback = $resultCallback ?: function () { |
|
201 | |||
202 | 9 | }; |
|
203 | |||
217 | } |
||
218 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope