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|\Go\ParserReflection\ReflectionClass |
||
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 | 73 | public function __construct($context) |
|
75 | |||
76 | 21 | public function getConstantName() |
|
80 | |||
81 | 48 | public function getValue() |
|
85 | |||
86 | 21 | public function isConstant() |
|
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | 50 | public function process(Node $node) |
|
101 | |||
102 | /** |
||
103 | * Resolves node into valid value |
||
104 | * |
||
105 | * @param Node $node |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 50 | protected function resolve(Node $node) |
|
125 | |||
126 | 9 | protected function resolveScalarDNumber(Scalar\DNumber $node) |
|
130 | |||
131 | 27 | protected function resolveScalarLNumber(Scalar\LNumber $node) |
|
135 | |||
136 | 23 | protected function resolveScalarString(Scalar\String_ $node) |
|
140 | |||
141 | protected function resolveScalarMagicConstMethod() |
||
151 | |||
152 | 1 | protected function resolveScalarMagicConstFunction() |
|
160 | |||
161 | 29 | protected function resolveScalarMagicConstNamespace() |
|
173 | |||
174 | 10 | protected function resolveScalarMagicConstClass() |
|
188 | |||
189 | 3 | protected function resolveScalarMagicConstDir() |
|
197 | |||
198 | 6 | protected function resolveScalarMagicConstFile() |
|
206 | |||
207 | 6 | protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
|
211 | |||
212 | 2 | protected function resolveScalarMagicConstTrait() |
|
220 | |||
221 | 26 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
253 | |||
254 | 14 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
284 | |||
285 | 9 | protected function resolveExprArray(Expr\Array_ $node) |
|
296 | |||
297 | 1 | protected function resolveExprBinaryOpPlus(Expr\BinaryOp\Plus $node) |
|
301 | |||
302 | 1 | protected function resolveExprBinaryOpMinus(Expr\BinaryOp\Minus $node) |
|
306 | |||
307 | 2 | protected function resolveExprBinaryOpMul(Expr\BinaryOp\Mul $node) |
|
311 | |||
312 | 1 | protected function resolveExprBinaryOpPow(Expr\BinaryOp\Pow $node) |
|
316 | |||
317 | 1 | protected function resolveExprBinaryOpDiv(Expr\BinaryOp\Div $node) |
|
321 | |||
322 | 1 | protected function resolveExprBinaryOpMod(Expr\BinaryOp\Mod $node) |
|
326 | |||
327 | 1 | protected function resolveExprBooleanNot(Expr\BooleanNot $node) |
|
331 | |||
332 | 1 | protected function resolveExprBitwiseNot(Expr\BitwiseNot $node) |
|
336 | |||
337 | 1 | protected function resolveExprBinaryOpBitwiseOr(Expr\BinaryOp\BitwiseOr $node) |
|
341 | |||
342 | 1 | protected function resolveExprBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $node) |
|
346 | |||
347 | 1 | protected function resolveExprBinaryOpBitwiseXor(Expr\BinaryOp\BitwiseXor $node) |
|
351 | |||
352 | 1 | protected function resolveExprBinaryOpShiftLeft(Expr\BinaryOp\ShiftLeft $node) |
|
356 | |||
357 | 1 | protected function resolveExprBinaryOpShiftRight(Expr\BinaryOp\ShiftRight $node) |
|
361 | |||
362 | 3 | protected function resolveExprBinaryOpConcat(Expr\BinaryOp\Concat $node) |
|
366 | |||
367 | 1 | protected function resolveExprTernary(Expr\Ternary $node) |
|
379 | |||
380 | 1 | protected function resolveExprBinaryOpSmallerOrEqual(Expr\BinaryOp\SmallerOrEqual $node) |
|
384 | |||
385 | 1 | protected function resolveExprBinaryOpGreaterOrEqual(Expr\BinaryOp\GreaterOrEqual $node) |
|
389 | |||
390 | 1 | protected function resolveExprBinaryOpEqual(Expr\BinaryOp\Equal $node) |
|
394 | |||
395 | 1 | protected function resolveExprBinaryOpNotEqual(Expr\BinaryOp\NotEqual $node) |
|
399 | |||
400 | 1 | protected function resolveExprBinaryOpSmaller(Expr\BinaryOp\Smaller $node) |
|
404 | |||
405 | 2 | protected function resolveExprBinaryOpGreater(Expr\BinaryOp\Greater $node) |
|
409 | |||
410 | 1 | protected function resolveExprBinaryOpIdentical(Expr\BinaryOp\Identical $node) |
|
414 | |||
415 | 1 | protected function resolveExprBinaryOpNotIdentical(Expr\BinaryOp\NotIdentical $node) |
|
419 | |||
420 | 1 | protected function resolveExprBinaryOpBooleanAnd(Expr\BinaryOp\BooleanAnd $node) |
|
424 | |||
425 | 1 | protected function resolveExprBinaryOpLogicalAnd(Expr\BinaryOp\LogicalAnd $node) |
|
429 | |||
430 | 1 | protected function resolveExprBinaryOpBooleanOr(Expr\BinaryOp\BooleanOr $node) |
|
434 | |||
435 | 1 | protected function resolveExprBinaryOpLogicalOr(Expr\BinaryOp\LogicalOr $node) |
|
439 | |||
440 | 1 | protected function resolveExprBinaryOpLogicalXor(Expr\BinaryOp\LogicalXor $node) |
|
444 | |||
445 | 50 | private function getDispatchMethodFor(Node $node) |
|
450 | |||
451 | /** |
||
452 | * Utility method to fetch reflection class instance by name |
||
453 | * |
||
454 | * Supports: |
||
455 | * 'self' keyword |
||
456 | * 'parent' keyword |
||
457 | * not-FQN class names |
||
458 | * |
||
459 | * @param Node\Name $node Class name node |
||
460 | * |
||
461 | * @return bool|\ReflectionClass |
||
462 | * |
||
463 | * @throws ReflectionException |
||
464 | */ |
||
465 | 12 | private function fetchReflectionClass(Node\Name $node) |
|
508 | } |
||
509 |