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 |
||
| 25 | class NodeExpressionResolver |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of exception for constant fetch |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private static $notConstants = [ |
||
| 34 | 'true' => true, |
||
| 35 | 'false' => true, |
||
| 36 | 'null' => true, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Name of the constant (if present) |
||
| 41 | * |
||
| 42 | * @var null|string |
||
| 43 | */ |
||
| 44 | private $constantName = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Current reflection context for parsing |
||
| 48 | * |
||
| 49 | * @var mixed|\Go\ParserReflection\ReflectionClass |
||
| 50 | */ |
||
| 51 | private $context; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Flag if expression is constant |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $isConstant = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Node resolving level, 1 = top-level |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $nodeLevel = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var mixed Value of expression/constant |
||
| 69 | */ |
||
| 70 | private $value; |
||
| 71 | |||
| 72 | 74 | public function __construct($context) |
|
| 76 | |||
| 77 | 21 | public function getConstantName() |
|
| 81 | |||
| 82 | 49 | public function getValue() |
|
| 86 | |||
| 87 | 21 | public function isConstant() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritDoc} |
||
| 94 | */ |
||
| 95 | 51 | public function process(Node $node) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Resolves node into valid value |
||
| 110 | * |
||
| 111 | * @param Node $node |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | 51 | protected function resolve(Node $node) |
|
| 131 | |||
| 132 | 9 | protected function resolveScalarDNumber(Scalar\DNumber $node) |
|
| 136 | |||
| 137 | 27 | protected function resolveScalarLNumber(Scalar\LNumber $node) |
|
| 141 | |||
| 142 | 24 | protected function resolveScalarString(Scalar\String_ $node) |
|
| 146 | |||
| 147 | protected function resolveScalarMagicConstMethod() |
||
| 157 | |||
| 158 | 1 | protected function resolveScalarMagicConstFunction() |
|
| 166 | |||
| 167 | 29 | protected function resolveScalarMagicConstNamespace() |
|
| 179 | |||
| 180 | 11 | protected function resolveScalarMagicConstClass() |
|
| 194 | |||
| 195 | 4 | protected function resolveScalarMagicConstDir() |
|
| 203 | |||
| 204 | 7 | protected function resolveScalarMagicConstFile() |
|
| 212 | |||
| 213 | 7 | protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
|
| 217 | |||
| 218 | 2 | protected function resolveScalarMagicConstTrait() |
|
| 226 | |||
| 227 | 27 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
| 259 | |||
| 260 | 15 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
| 261 | { |
||
| 262 | 15 | $classToReflect = $node->class; |
|
| 263 | 15 | if (!($classToReflect instanceof Node\Name)) { |
|
| 264 | 3 | $classToReflect = $this->resolve($classToReflect) ?: $classToReflect; |
|
| 265 | 3 | if (!is_string($classToReflect)) { |
|
| 266 | 2 | $reason = 'Unable'; |
|
| 267 | 2 | if ($classToReflect instanceof Expr) { |
|
| 268 | 1 | $methodName = $this->getDispatchMethodFor($classToReflect); |
|
| 269 | 1 | $reason = "Method " . __CLASS__ . "::{$methodName}() not found trying"; |
|
| 270 | } |
||
| 271 | 2 | throw new ReflectionException("$reason to resolve class constant."); |
|
| 272 | } |
||
| 273 | // Strings evaluated as class names are always treated as fully |
||
| 274 | // qualified. |
||
| 275 | 1 | $classToReflect = new Node\Name\FullyQualified(ltrim($classToReflect, '\\')); |
|
| 276 | } |
||
| 277 | 13 | $refClass = $this->fetchReflectionClass($classToReflect); |
|
| 278 | 13 | $constantName = ($node->name instanceof Expr\Error) ? '' : $node->name->toString(); |
|
| 279 | |||
| 280 | // special handling of ::class constants |
||
| 281 | 13 | if ('class' === $constantName) { |
|
| 282 | 3 | return $refClass->getName(); |
|
| 283 | } |
||
| 284 | |||
| 285 | 11 | $this->isConstant = true; |
|
| 286 | 11 | $this->constantName = (string)$classToReflect . '::' . $constantName; |
|
| 287 | |||
| 288 | 11 | return $refClass->getConstant($constantName); |
|
| 289 | } |
||
| 290 | |||
| 291 | 9 | protected function resolveExprArray(Expr\Array_ $node) |
|
| 302 | |||
| 303 | 1 | protected function resolveExprBinaryOpPlus(Expr\BinaryOp\Plus $node) |
|
| 307 | |||
| 308 | 1 | protected function resolveExprBinaryOpMinus(Expr\BinaryOp\Minus $node) |
|
| 312 | |||
| 313 | 2 | protected function resolveExprBinaryOpMul(Expr\BinaryOp\Mul $node) |
|
| 317 | |||
| 318 | 1 | protected function resolveExprBinaryOpPow(Expr\BinaryOp\Pow $node) |
|
| 322 | |||
| 323 | 1 | protected function resolveExprBinaryOpDiv(Expr\BinaryOp\Div $node) |
|
| 327 | |||
| 328 | 1 | protected function resolveExprBinaryOpMod(Expr\BinaryOp\Mod $node) |
|
| 332 | |||
| 333 | 1 | protected function resolveExprBooleanNot(Expr\BooleanNot $node) |
|
| 337 | |||
| 338 | 1 | protected function resolveExprBitwiseNot(Expr\BitwiseNot $node) |
|
| 342 | |||
| 343 | 1 | protected function resolveExprBinaryOpBitwiseOr(Expr\BinaryOp\BitwiseOr $node) |
|
| 347 | |||
| 348 | 1 | protected function resolveExprBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $node) |
|
| 352 | |||
| 353 | 1 | protected function resolveExprBinaryOpBitwiseXor(Expr\BinaryOp\BitwiseXor $node) |
|
| 357 | |||
| 358 | 1 | protected function resolveExprBinaryOpShiftLeft(Expr\BinaryOp\ShiftLeft $node) |
|
| 362 | |||
| 363 | 1 | protected function resolveExprBinaryOpShiftRight(Expr\BinaryOp\ShiftRight $node) |
|
| 367 | |||
| 368 | 3 | protected function resolveExprBinaryOpConcat(Expr\BinaryOp\Concat $node) |
|
| 372 | |||
| 373 | 1 | protected function resolveExprTernary(Expr\Ternary $node) |
|
| 385 | |||
| 386 | 1 | protected function resolveExprBinaryOpSmallerOrEqual(Expr\BinaryOp\SmallerOrEqual $node) |
|
| 390 | |||
| 391 | 1 | protected function resolveExprBinaryOpGreaterOrEqual(Expr\BinaryOp\GreaterOrEqual $node) |
|
| 395 | |||
| 396 | 1 | protected function resolveExprBinaryOpEqual(Expr\BinaryOp\Equal $node) |
|
| 400 | |||
| 401 | 1 | protected function resolveExprBinaryOpNotEqual(Expr\BinaryOp\NotEqual $node) |
|
| 405 | |||
| 406 | 1 | protected function resolveExprBinaryOpSmaller(Expr\BinaryOp\Smaller $node) |
|
| 410 | |||
| 411 | 2 | protected function resolveExprBinaryOpGreater(Expr\BinaryOp\Greater $node) |
|
| 415 | |||
| 416 | 1 | protected function resolveExprBinaryOpIdentical(Expr\BinaryOp\Identical $node) |
|
| 420 | |||
| 421 | 1 | protected function resolveExprBinaryOpNotIdentical(Expr\BinaryOp\NotIdentical $node) |
|
| 425 | |||
| 426 | 1 | protected function resolveExprBinaryOpBooleanAnd(Expr\BinaryOp\BooleanAnd $node) |
|
| 430 | |||
| 431 | 1 | protected function resolveExprBinaryOpLogicalAnd(Expr\BinaryOp\LogicalAnd $node) |
|
| 435 | |||
| 436 | 1 | protected function resolveExprBinaryOpBooleanOr(Expr\BinaryOp\BooleanOr $node) |
|
| 440 | |||
| 441 | 1 | protected function resolveExprBinaryOpLogicalOr(Expr\BinaryOp\LogicalOr $node) |
|
| 445 | |||
| 446 | 1 | protected function resolveExprBinaryOpLogicalXor(Expr\BinaryOp\LogicalXor $node) |
|
| 450 | |||
| 451 | 51 | private function getDispatchMethodFor(Node $node) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Utility method to fetch reflection class instance by name |
||
| 459 | * |
||
| 460 | * Supports: |
||
| 461 | * 'self' keyword |
||
| 462 | * 'parent' keyword |
||
| 463 | * not-FQN class names |
||
| 464 | * |
||
| 465 | * @param Node\Name $node Class name node |
||
| 466 | * |
||
| 467 | * @return bool|\ReflectionClass |
||
| 468 | * |
||
| 469 | * @throws ReflectionException |
||
| 470 | */ |
||
| 471 | 13 | private function fetchReflectionClass(Node\Name $node) |
|
| 514 | } |
||
| 515 |