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 | 36 | public function __construct($context) |
|
72 | { |
||
73 | 36 | $this->context = $context; |
|
74 | 36 | } |
|
75 | |||
76 | 10 | public function getConstantName() |
|
77 | { |
||
78 | 10 | return $this->constantName; |
|
79 | } |
||
80 | |||
81 | 22 | public function getValue() |
|
82 | { |
||
83 | 22 | return $this->value; |
|
84 | } |
||
85 | |||
86 | 10 | public function isConstant() |
|
87 | { |
||
88 | 10 | return $this->isConstant; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | 22 | public function process(Node $node) |
|
95 | { |
||
96 | 22 | $this->nodeLevel = 0; |
|
97 | 22 | $this->isConstant = false; |
|
98 | 22 | $this->constantName = null; |
|
99 | 22 | $this->value = $this->resolve($node); |
|
100 | 22 | } |
|
101 | |||
102 | /** |
||
103 | * Resolves node into valid value |
||
104 | * |
||
105 | * @param Node $node |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 22 | protected function resolve(Node $node) |
|
110 | { |
||
111 | 22 | $value = null; |
|
112 | try { |
||
113 | 22 | ++$this->nodeLevel; |
|
114 | |||
115 | 22 | $nodeType = $node->getType(); |
|
116 | 22 | $methodName = 'resolve' . str_replace('_', '', $nodeType); |
|
117 | 22 | if (method_exists($this, $methodName)) { |
|
118 | 22 | $value = $this->$methodName($node); |
|
119 | } |
||
120 | 22 | } finally { |
|
121 | 22 | --$this->nodeLevel; |
|
122 | } |
||
123 | |||
124 | 22 | return $value; |
|
125 | } |
||
126 | |||
127 | 4 | protected function resolveScalarDNumber(Scalar\DNumber $node) |
|
128 | { |
||
129 | 4 | return $node->value; |
|
130 | } |
||
131 | |||
132 | 13 | protected function resolveScalarLNumber(Scalar\LNumber $node) |
|
133 | { |
||
134 | 13 | return $node->value; |
|
135 | } |
||
136 | |||
137 | 9 | protected function resolveScalarString(Scalar\String_ $node) |
|
138 | { |
||
139 | 9 | 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 | 10 | protected function resolveScalarMagicConstNamespace() |
|
174 | |||
175 | 8 | protected function resolveScalarMagicConstClass() |
|
176 | { |
||
177 | 8 | if ($this->context instanceof \ReflectionClass) { |
|
178 | 1 | return $this->context->getName(); |
|
179 | } |
||
180 | 7 | if (method_exists($this->context, 'getDeclaringClass')) { |
|
181 | $declaringClass = $this->context->getDeclaringClass(); |
||
182 | if ($declaringClass instanceof \ReflectionClass) { |
||
183 | return $declaringClass->getName(); |
||
184 | } |
||
185 | } |
||
189 | |||
190 | 1 | protected function resolveScalarMagicConstDir() |
|
198 | |||
199 | 3 | protected function resolveScalarMagicConstFile() |
|
207 | |||
208 | 3 | protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
|
212 | |||
213 | 1 | protected function resolveScalarMagicConstTrait() |
|
221 | |||
222 | 13 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
256 | |||
257 | 4 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
269 | |||
270 | 9 | protected function resolveExprArray(Expr\Array_ $node) |
|
281 | |||
282 | /** |
||
283 | * Utility method to fetch reflection class instance by name |
||
284 | * |
||
285 | * Supports: |
||
286 | * 'self' keyword |
||
287 | * 'parent' keyword |
||
288 | * not-FQN class names |
||
289 | * |
||
290 | * @param Node\Name $node Class name node |
||
291 | * |
||
292 | * @return bool|\ReflectionClass |
||
293 | * |
||
294 | * @throws ReflectionException |
||
295 | */ |
||
296 | 4 | private function fetchReflectionClass(Node\Name $node) |
|
331 | } |
||
332 |