Complex classes like NodeExpressionResolver 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 NodeExpressionResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class NodeExpressionResolver |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * List of exception for constant fetch |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private static $notConstants = [ |
||
33 | 'true' => true, |
||
34 | 'false' => true, |
||
35 | 'null' => true, |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * Name of the constant (if present) |
||
40 | * |
||
41 | * @var null|string |
||
42 | */ |
||
43 | private $constantName = null; |
||
44 | |||
45 | /** |
||
46 | * Current reflection context for parsing |
||
47 | * |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $context; |
||
51 | |||
52 | /** |
||
53 | * Flag if expression is constant |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $isConstant = false; |
||
58 | |||
59 | /** |
||
60 | * Node resolving level, 1 = top-level |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | private $nodeLevel = 0; |
||
65 | |||
66 | /** |
||
67 | * @var mixed Value of expression/constant |
||
68 | */ |
||
69 | private $value; |
||
70 | |||
71 | 13 | public function __construct($context) |
|
75 | |||
76 | 8 | public function getConstantName() |
|
80 | |||
81 | 13 | public function getValue() |
|
85 | |||
86 | 8 | public function isConstant() |
|
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | 13 | public function process(Node $node) |
|
101 | |||
102 | /** |
||
103 | * Resolves node into valid value |
||
104 | * |
||
105 | * @param Node $node |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 13 | protected function resolve(Node $node) |
|
110 | { |
||
111 | 13 | $value = null; |
|
112 | try { |
||
113 | 13 | ++$this->nodeLevel; |
|
114 | |||
115 | 13 | $nodeType = $node->getType(); |
|
116 | 13 | $methodName = 'resolve' . str_replace('_', '', $nodeType); |
|
117 | 13 | if (method_exists($this, $methodName)) { |
|
118 | 13 | $value = $this->$methodName($node); |
|
119 | 13 | } |
|
120 | 13 | } finally { |
|
121 | 13 | --$this->nodeLevel; |
|
122 | } |
||
123 | |||
124 | 13 | return $value; |
|
125 | } |
||
126 | |||
127 | 2 | protected function resolveScalarDNumber(Scalar\DNumber $node) |
|
128 | { |
||
129 | 2 | return $node->value; |
|
130 | } |
||
131 | |||
132 | 11 | protected function resolveScalarLNumber(Scalar\LNumber $node) |
|
133 | { |
||
134 | 11 | return $node->value; |
|
135 | } |
||
136 | |||
137 | 5 | protected function resolveScalarString(Scalar\String_ $node) |
|
138 | { |
||
139 | 5 | return $node->value; |
|
140 | } |
||
141 | |||
142 | protected function resolveScalarMagicConstMethod() |
||
143 | { |
||
144 | if ($this->context instanceof \ReflectionMethod) { |
||
145 | $fullName = $this->context->getDeclaringClass()->getName() . '::' . $this->context->getShortName(); |
||
|
|||
146 | |||
147 | return $fullName; |
||
148 | } |
||
149 | |||
150 | return ''; |
||
151 | } |
||
152 | |||
153 | protected function resolveScalarMagicConstFunction() |
||
154 | { |
||
155 | if ($this->context instanceof \ReflectionFunctionAbstract) { |
||
156 | return $this->context->getName(); |
||
157 | } |
||
158 | |||
159 | return ''; |
||
160 | } |
||
161 | |||
162 | 7 | protected function resolveScalarMagicConstNamespace() |
|
163 | { |
||
164 | 7 | if (method_exists($this->context, 'getNamespaceName')) { |
|
165 | 7 | return $this->context->getNamespaceName(); |
|
166 | } |
||
167 | |||
168 | return ''; |
||
169 | } |
||
170 | |||
171 | 7 | protected function resolveScalarMagicConstClass() |
|
172 | { |
||
173 | 7 | if ($this->context instanceof \ReflectionClass) { |
|
174 | 1 | return $this->context->getName(); |
|
175 | } |
||
176 | 6 | if (method_exists($this->context, 'getDeclaringClass')) { |
|
177 | $declaringClass = $this->context->getDeclaringClass(); |
||
178 | if ($declaringClass instanceof \ReflectionClass) { |
||
179 | return $declaringClass->getName(); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | 6 | return ''; |
|
184 | } |
||
185 | |||
186 | 1 | protected function resolveScalarMagicConstDir() |
|
187 | { |
||
188 | 1 | if (method_exists($this->context, 'getFileName')) { |
|
189 | 1 | return dirname($this->context->getFileName()); |
|
190 | } |
||
191 | |||
192 | return ''; |
||
193 | } |
||
194 | |||
195 | 1 | protected function resolveScalarMagicConstFile() |
|
196 | { |
||
197 | 1 | if (method_exists($this->context, 'getFileName')) { |
|
198 | 1 | return $this->context->getFileName(); |
|
199 | } |
||
200 | |||
201 | return ''; |
||
202 | } |
||
203 | |||
204 | 1 | protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
|
205 | { |
||
206 | 1 | return $node->hasAttribute('startLine') ? $node->getAttribute('startLine') : 0; |
|
207 | } |
||
208 | |||
209 | protected function resolveScalarMagicConstTrait() |
||
210 | { |
||
211 | if ($this->context instanceof \ReflectionClass && $this->context->isTrait()) { |
||
212 | return $this->context->getName(); |
||
213 | } |
||
214 | |||
215 | return ''; |
||
216 | } |
||
217 | |||
218 | 11 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
219 | { |
||
220 | 11 | $constantValue = null; |
|
221 | 11 | $isResolved = false; |
|
222 | |||
223 | /** @var ReflectionFileNamespace|null $fileNamespace */ |
||
224 | 11 | $fileNamespace = null; |
|
225 | 11 | $isFQNConstant = $node->name instanceof Node\Name\FullyQualified; |
|
226 | 11 | $constantName = $node->name->toString(); |
|
227 | |||
228 | 11 | if (!$isFQNConstant) { |
|
229 | 11 | if (method_exists($this->context, 'getFileName')) { |
|
230 | 11 | $fileName = $this->context->getFileName(); |
|
231 | 11 | $namespaceName = $this->context->getNamespaceName(); |
|
232 | 11 | $fileNamespace = new ReflectionFileNamespace($fileName, $namespaceName); |
|
233 | 11 | if ($fileNamespace->hasConstant($constantName)) { |
|
234 | 8 | $constantValue = $fileNamespace->getConstant($constantName); |
|
235 | 8 | $constantName = $fileNamespace->getName() . '\\' . $constantName; |
|
236 | 8 | $isResolved = true; |
|
237 | 8 | } |
|
238 | 11 | } |
|
239 | 11 | } |
|
240 | |||
241 | 11 | if (!$isResolved && defined($constantName)) { |
|
242 | 10 | $constantValue = constant($constantName); |
|
243 | 10 | } |
|
244 | |||
245 | 11 | if ($this->nodeLevel === 1 && !isset(self::$notConstants[$constantName])) { |
|
246 | 8 | $this->isConstant = true; |
|
247 | 8 | $this->constantName = $constantName; |
|
248 | 8 | } |
|
249 | |||
250 | 11 | return $constantValue; |
|
251 | } |
||
252 | |||
253 | 3 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
254 | { |
||
255 | 3 | $refClass = $this->fetchReflectionClass($node->class); |
|
256 | 3 | $constantName = $node->name; |
|
257 | |||
258 | // special handling of ::class constants |
||
259 | 3 | if ('class' === $constantName) { |
|
260 | 1 | return $refClass->getName(); |
|
261 | } |
||
262 | |||
263 | 3 | return $refClass->getConstant($constantName); |
|
264 | } |
||
265 | |||
266 | 7 | protected function resolveExprArray(Expr\Array_ $node) |
|
267 | { |
||
268 | 7 | $result = []; |
|
269 | 7 | foreach ($node->items as $itemIndex => $arrayItem) { |
|
270 | 7 | $itemValue = $this->resolve($arrayItem->value); |
|
271 | 7 | $itemKey = isset($arrayItem->key) ? $this->resolve($arrayItem->key) : $itemIndex; |
|
272 | 7 | $result[$itemKey] = $itemValue; |
|
273 | 7 | } |
|
274 | |||
275 | 7 | return $result; |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * Utility method to fetch reflection class instance by name |
||
280 | * |
||
281 | * Supports: |
||
282 | * 'self' keyword |
||
283 | * 'parent' keyword |
||
284 | * not-FQN class names |
||
285 | * |
||
286 | * @param Node\Name $node Class name node |
||
287 | * |
||
288 | * @return bool|\ReflectionClass |
||
289 | * |
||
290 | * @throws ReflectionException |
||
291 | */ |
||
292 | 3 | private function fetchReflectionClass(Node\Name $node) |
|
327 | } |
||
328 |