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 |
||
31 | class NodeExpressionResolver |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * List of exception for constant fetch |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $notConstants = [ |
||
40 | 'true' => true, |
||
41 | 'false' => true, |
||
42 | 'null' => true, |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * Name of the constant (if present) |
||
47 | * |
||
48 | * @var ?string |
||
49 | */ |
||
50 | private $constantName; |
||
51 | |||
52 | /** |
||
53 | * Current reflection context for parsing |
||
54 | * |
||
55 | * @var mixed|ReflectionClass |
||
56 | */ |
||
57 | private $context; |
||
58 | |||
59 | /** |
||
60 | * Flag if expression is constant |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | private $isConstant = false; |
||
65 | |||
66 | /** |
||
67 | * Node resolving level, 1 = top-level |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | private $nodeLevel = 0; |
||
72 | |||
73 | /** |
||
74 | * @var mixed Value of expression/constant |
||
75 | */ |
||
76 | private $value; |
||
77 | |||
78 | 76 | public function __construct($context) |
|
82 | |||
83 | 21 | public function getConstantName(): ?string |
|
87 | |||
88 | 51 | public function getValue() |
|
92 | |||
93 | 21 | public function isConstant(): bool |
|
97 | |||
98 | 53 | public function process(Node $node): void |
|
110 | |||
111 | /** |
||
112 | * Resolves node into valid value |
||
113 | * |
||
114 | * @param Node $node |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | 53 | protected function resolve(Node $node) |
|
134 | |||
135 | 9 | protected function resolveScalarDNumber(DNumber $node): float |
|
139 | |||
140 | 28 | protected function resolveScalarLNumber(LNumber $node): int |
|
144 | |||
145 | 25 | protected function resolveScalarString(String_ $node): string |
|
149 | |||
150 | protected function resolveScalarMagicConstMethod(): string |
||
160 | |||
161 | 1 | protected function resolveScalarMagicConstFunction(): string |
|
169 | |||
170 | 30 | protected function resolveScalarMagicConstNamespace(): string |
|
182 | |||
183 | 11 | protected function resolveScalarMagicConstClass(): string |
|
197 | |||
198 | 4 | protected function resolveScalarMagicConstDir(): string |
|
206 | |||
207 | 7 | protected function resolveScalarMagicConstFile(): string |
|
215 | |||
216 | 7 | protected function resolveScalarMagicConstLine(Line $node): int |
|
220 | |||
221 | 2 | protected function resolveScalarMagicConstTrait(): string |
|
229 | |||
230 | 28 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
260 | |||
261 | 15 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
291 | |||
292 | 9 | protected function resolveExprArray(Expr\Array_ $node): array |
|
303 | |||
304 | 3 | protected function resolveExprBinaryOpPlus(Expr\BinaryOp\Plus $node) |
|
308 | |||
309 | 1 | protected function resolveExprBinaryOpMinus(Expr\BinaryOp\Minus $node) |
|
313 | |||
314 | 2 | protected function resolveExprBinaryOpMul(Expr\BinaryOp\Mul $node) |
|
318 | |||
319 | 1 | protected function resolveExprBinaryOpPow(Expr\BinaryOp\Pow $node) |
|
323 | |||
324 | 1 | protected function resolveExprBinaryOpDiv(Expr\BinaryOp\Div $node) |
|
328 | |||
329 | 1 | protected function resolveExprBinaryOpMod(Expr\BinaryOp\Mod $node): int |
|
333 | |||
334 | 1 | protected function resolveExprBooleanNot(Expr\BooleanNot $node): bool |
|
338 | |||
339 | 1 | protected function resolveExprBitwiseNot(Expr\BitwiseNot $node) |
|
343 | |||
344 | 1 | protected function resolveExprBinaryOpBitwiseOr(Expr\BinaryOp\BitwiseOr $node): int |
|
348 | |||
349 | 1 | protected function resolveExprBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $node): int |
|
353 | |||
354 | 1 | protected function resolveExprBinaryOpBitwiseXor(Expr\BinaryOp\BitwiseXor $node): int |
|
358 | |||
359 | 1 | protected function resolveExprBinaryOpShiftLeft(Expr\BinaryOp\ShiftLeft $node): int |
|
363 | |||
364 | 1 | protected function resolveExprBinaryOpShiftRight(Expr\BinaryOp\ShiftRight $node): int |
|
368 | |||
369 | 3 | protected function resolveExprBinaryOpConcat(Expr\BinaryOp\Concat $node): string |
|
373 | |||
374 | 1 | protected function resolveExprTernary(Expr\Ternary $node) |
|
384 | |||
385 | 1 | protected function resolveExprBinaryOpSmallerOrEqual(Expr\BinaryOp\SmallerOrEqual $node): bool |
|
389 | |||
390 | 1 | protected function resolveExprBinaryOpGreaterOrEqual(Expr\BinaryOp\GreaterOrEqual $node): bool |
|
394 | |||
395 | 1 | protected function resolveExprBinaryOpEqual(Expr\BinaryOp\Equal $node): bool |
|
400 | |||
401 | 1 | protected function resolveExprBinaryOpNotEqual(Expr\BinaryOp\NotEqual $node): bool |
|
406 | |||
407 | 1 | protected function resolveExprBinaryOpSmaller(Expr\BinaryOp\Smaller $node): bool |
|
411 | |||
412 | 2 | protected function resolveExprBinaryOpGreater(Expr\BinaryOp\Greater $node): bool |
|
416 | |||
417 | 1 | protected function resolveExprBinaryOpIdentical(Expr\BinaryOp\Identical $node): bool |
|
421 | |||
422 | 1 | protected function resolveExprBinaryOpNotIdentical(Expr\BinaryOp\NotIdentical $node): bool |
|
426 | |||
427 | 1 | protected function resolveExprBinaryOpBooleanAnd(Expr\BinaryOp\BooleanAnd $node): bool |
|
431 | |||
432 | 1 | protected function resolveExprBinaryOpLogicalAnd(Expr\BinaryOp\LogicalAnd $node): bool |
|
436 | |||
437 | 1 | protected function resolveExprBinaryOpBooleanOr(Expr\BinaryOp\BooleanOr $node): bool |
|
441 | |||
442 | 1 | protected function resolveExprBinaryOpLogicalOr(Expr\BinaryOp\LogicalOr $node): bool |
|
446 | |||
447 | 1 | protected function resolveExprBinaryOpLogicalXor(Expr\BinaryOp\LogicalXor $node): bool |
|
451 | |||
452 | 53 | private function getDispatchMethodFor(Node $node): string |
|
458 | |||
459 | /** |
||
460 | * Utility method to fetch reflection class instance by name |
||
461 | * |
||
462 | * Supports: |
||
463 | * 'self' keyword |
||
464 | * 'parent' keyword |
||
465 | * not-FQN class names |
||
466 | * |
||
467 | * @param Node\Name $node Class name node |
||
468 | * |
||
469 | * @return bool|\ReflectionClass |
||
470 | * |
||
471 | * @throws ReflectionException |
||
472 | */ |
||
473 | 13 | private function fetchReflectionClass(Node\Name $node) |
|
524 | } |
||
525 |