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 | 76 | ||
73 | /** |
||
74 | 76 | * @var mixed Value of expression/constant |
|
75 | 76 | */ |
|
76 | private $value; |
||
77 | 21 | ||
78 | public function __construct($context) |
||
82 | 51 | ||
83 | public function getConstantName(): ?string |
||
87 | 21 | ||
88 | public function getValue() |
||
92 | |||
93 | 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 | 53 | * |
|
116 | * @return mixed |
||
117 | 53 | */ |
|
118 | protected function resolve(Node $node) |
||
134 | 9 | ||
135 | protected function resolveScalarDNumber(DNumber $node): float |
||
139 | 28 | ||
140 | protected function resolveScalarLNumber(LNumber $node): int |
||
144 | 25 | ||
145 | protected function resolveExprUnaryMinus(Expr\UnaryMinus $node) |
||
150 | |||
151 | protected function resolveExprUnaryPlus(Expr\UnaryMinus $node) |
||
155 | |||
156 | protected function resolveScalarString(String_ $node): string |
||
160 | 1 | ||
161 | 1 | protected function resolveScalarMagicConstMethod(): string |
|
171 | |||
172 | protected function resolveScalarMagicConstFunction(): string |
||
180 | 11 | ||
181 | protected function resolveScalarMagicConstNamespace(): string |
||
193 | |||
194 | protected function resolveScalarMagicConstClass(): string |
||
208 | |||
209 | protected function resolveScalarMagicConstDir(): string |
||
217 | |||
218 | 2 | protected function resolveScalarMagicConstFile(): string |
|
226 | |||
227 | 28 | protected function resolveScalarMagicConstLine(Line $node): int |
|
231 | |||
232 | 28 | protected function resolveScalarMagicConstTrait(): string |
|
240 | 24 | ||
241 | 13 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
271 | 2 | ||
272 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
||
302 | |||
303 | 3 | protected function resolveExprArray(Expr\Array_ $node): array |
|
314 | |||
315 | 2 | protected function resolveExprBinaryOpPlus(Expr\BinaryOp\Plus $node) |
|
319 | |||
320 | 1 | protected function resolveExprBinaryOpMinus(Expr\BinaryOp\Minus $node) |
|
324 | |||
325 | 1 | protected function resolveExprBinaryOpMul(Expr\BinaryOp\Mul $node) |
|
329 | |||
330 | 1 | protected function resolveExprBinaryOpPow(Expr\BinaryOp\Pow $node) |
|
334 | |||
335 | 1 | protected function resolveExprBinaryOpDiv(Expr\BinaryOp\Div $node) |
|
339 | |||
340 | 1 | protected function resolveExprBinaryOpMod(Expr\BinaryOp\Mod $node): int |
|
344 | |||
345 | 1 | protected function resolveExprBooleanNot(Expr\BooleanNot $node): bool |
|
349 | |||
350 | 1 | protected function resolveExprBitwiseNot(Expr\BitwiseNot $node) |
|
354 | |||
355 | 1 | protected function resolveExprBinaryOpBitwiseOr(Expr\BinaryOp\BitwiseOr $node): int |
|
359 | |||
360 | 1 | protected function resolveExprBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $node): int |
|
364 | |||
365 | 1 | protected function resolveExprBinaryOpBitwiseXor(Expr\BinaryOp\BitwiseXor $node): int |
|
369 | |||
370 | 3 | protected function resolveExprBinaryOpShiftLeft(Expr\BinaryOp\ShiftLeft $node): int |
|
374 | |||
375 | 1 | protected function resolveExprBinaryOpShiftRight(Expr\BinaryOp\ShiftRight $node): int |
|
379 | |||
380 | protected function resolveExprBinaryOpConcat(Expr\BinaryOp\Concat $node): string |
||
384 | |||
385 | protected function resolveExprTernary(Expr\Ternary $node) |
||
395 | |||
396 | 1 | protected function resolveExprBinaryOpSmallerOrEqual(Expr\BinaryOp\SmallerOrEqual $node): bool |
|
400 | |||
401 | 1 | protected function resolveExprBinaryOpGreaterOrEqual(Expr\BinaryOp\GreaterOrEqual $node): bool |
|
405 | |||
406 | 1 | protected function resolveExprBinaryOpEqual(Expr\BinaryOp\Equal $node): bool |
|
411 | 2 | ||
412 | protected function resolveExprBinaryOpNotEqual(Expr\BinaryOp\NotEqual $node): bool |
||
417 | |||
418 | 1 | protected function resolveExprBinaryOpSmaller(Expr\BinaryOp\Smaller $node): bool |
|
422 | |||
423 | 1 | protected function resolveExprBinaryOpGreater(Expr\BinaryOp\Greater $node): bool |
|
427 | |||
428 | 1 | protected function resolveExprBinaryOpIdentical(Expr\BinaryOp\Identical $node): bool |
|
432 | |||
433 | 1 | protected function resolveExprBinaryOpNotIdentical(Expr\BinaryOp\NotIdentical $node): bool |
|
437 | |||
438 | 1 | protected function resolveExprBinaryOpBooleanAnd(Expr\BinaryOp\BooleanAnd $node): bool |
|
442 | |||
443 | 1 | protected function resolveExprBinaryOpLogicalAnd(Expr\BinaryOp\LogicalAnd $node): bool |
|
447 | |||
448 | 1 | protected function resolveExprBinaryOpBooleanOr(Expr\BinaryOp\BooleanOr $node): bool |
|
452 | |||
453 | 53 | protected function resolveExprBinaryOpLogicalOr(Expr\BinaryOp\LogicalOr $node): bool |
|
457 | |||
458 | protected function resolveExprBinaryOpLogicalXor(Expr\BinaryOp\LogicalXor $node): bool |
||
462 | |||
463 | private function getDispatchMethodFor(Node $node): string |
||
469 | |||
470 | /** |
||
471 | 13 | * Utility method to fetch reflection class instance by name |
|
472 | * |
||
473 | 13 | * Supports: |
|
474 | 13 | * 'self' keyword |
|
475 | 13 | * 'parent' keyword |
|
476 | * not-FQN class names |
||
477 | * |
||
478 | 6 | * @param Node\Name $node Class name node |
|
479 | 6 | * |
|
480 | 6 | * @return bool|\ReflectionClass |
|
481 | 3 | * |
|
482 | * @throws ReflectionException |
||
483 | */ |
||
484 | 4 | private function fetchReflectionClass(Node\Name $node) |
|
535 | } |
||
536 |