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 | 43 | public function __construct($context) |
|
| 75 | |||
| 76 | 13 | public function getConstantName() |
|
| 80 | |||
| 81 | 29 | public function getValue() |
|
| 85 | |||
| 86 | 13 | public function isConstant() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritDoc} |
||
| 93 | */ |
||
| 94 | 29 | public function process(Node $node) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Resolves node into valid value |
||
| 104 | * |
||
| 105 | * @param Node $node |
||
| 106 | * |
||
| 107 | * @return mixed |
||
| 108 | */ |
||
| 109 | 29 | protected function resolve(Node $node) |
|
| 126 | |||
| 127 | 6 | protected function resolveScalarDNumber(Scalar\DNumber $node) |
|
| 131 | |||
| 132 | 18 | protected function resolveScalarLNumber(Scalar\LNumber $node) |
|
| 136 | |||
| 137 | 13 | protected function resolveScalarString(Scalar\String_ $node) |
|
| 141 | |||
| 142 | protected function resolveScalarMagicConstMethod() |
||
| 152 | |||
| 153 | protected function resolveScalarMagicConstFunction() |
||
| 161 | |||
| 162 | 22 | protected function resolveScalarMagicConstNamespace() |
|
| 163 | { |
||
| 164 | 22 | if (method_exists($this->context, 'getNamespaceName')) { |
|
| 165 | 18 | return $this->context->getNamespaceName(); |
|
| 166 | } |
||
| 167 | |||
| 168 | 4 | if ($this->context instanceof ReflectionFileNamespace) { |
|
| 169 | 4 | return $this->context->getName(); |
|
| 170 | } |
||
| 171 | |||
| 172 | return ''; |
||
| 173 | } |
||
| 174 | |||
| 175 | 9 | protected function resolveScalarMagicConstClass() |
|
| 189 | |||
| 190 | 1 | protected function resolveScalarMagicConstDir() |
|
| 198 | |||
| 199 | 4 | protected function resolveScalarMagicConstFile() |
|
| 207 | |||
| 208 | 4 | protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
|
| 212 | |||
| 213 | 1 | protected function resolveScalarMagicConstTrait() |
|
| 221 | |||
| 222 | 18 | protected function resolveExprConstFetch(Expr\ConstFetch $node) |
|
| 256 | |||
| 257 | 6 | protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
|
| 272 | |||
| 273 | 9 | protected function resolveExprArray(Expr\Array_ $node) |
|
| 284 | |||
| 285 | protected function resolveExprBinaryOpPlus(Expr\BinaryOp\Plus $node) |
||
| 286 | { |
||
| 287 | return $this->resolve($node->left) + $this->resolve($node->right); |
||
| 288 | } |
||
| 289 | |||
| 290 | protected function resolveExprBinaryOpMinus(Expr\BinaryOp\Minus $node) |
||
| 291 | { |
||
| 292 | return $this->resolve($node->left) - $this->resolve($node->right); |
||
| 293 | } |
||
| 294 | |||
| 295 | protected function resolveExprBinaryOpMul(Expr\BinaryOp\Mul $node) |
||
| 296 | { |
||
| 297 | return $this->resolve($node->left) * $this->resolve($node->right); |
||
| 298 | } |
||
| 299 | |||
| 300 | protected function resolveExprBinaryOpDiv(Expr\BinaryOp\Div $node) |
||
| 301 | { |
||
| 302 | return $this->resolve($node->left) / $this->resolve($node->right); |
||
| 303 | } |
||
| 304 | |||
| 305 | protected function resolveExprBinaryOpMod(Expr\BinaryOp\Mod $node) |
||
| 306 | { |
||
| 307 | return $this->resolve($node->left) % $this->resolve($node->right); |
||
| 308 | } |
||
| 309 | |||
| 310 | protected function resolveExprBooleanNot(Expr\BooleanNot $node) |
||
| 311 | { |
||
| 312 | return !$this->resolve($node->expr); |
||
| 313 | } |
||
| 314 | |||
| 315 | protected function resolveExprBitwiseNot(Expr\BitwiseNot $node) |
||
| 316 | { |
||
| 317 | return ~$this->resolve($node->expr); |
||
| 318 | } |
||
| 319 | |||
| 320 | protected function resolveExprBinaryOpBitwiseOr(Expr\BinaryOp\BitwiseOr $node) |
||
| 321 | { |
||
| 322 | return $this->resolve($node->left) | $this->resolve($node->right); |
||
| 323 | } |
||
| 324 | |||
| 325 | protected function resolveExprBinaryOpBitwiseAnd(Expr\BinaryOp\BitwiseAnd $node) |
||
| 326 | { |
||
| 327 | return $this->resolve($node->left) & $this->resolve($node->right); |
||
| 328 | } |
||
| 329 | |||
| 330 | protected function resolveExprBinaryOpBitwiseXor(Expr\BinaryOp\BitwiseXor $node) |
||
| 331 | { |
||
| 332 | return $this->resolve($node->left) ^ $this->resolve($node->right); |
||
| 333 | } |
||
| 334 | |||
| 335 | protected function resolveExprBinaryOpShiftLeft(Expr\BinaryOp\ShiftLeft $node) |
||
| 336 | { |
||
| 337 | return $this->resolve($node->left) << $this->resolve($node->right); |
||
| 338 | } |
||
| 339 | |||
| 340 | protected function resolveExprBinaryOpShiftRight(Expr\BinaryOp\ShiftRight $node) |
||
| 341 | { |
||
| 342 | return $this->resolve($node->left) >> $this->resolve($node->right); |
||
| 343 | } |
||
| 344 | |||
| 345 | protected function resolveExprBinaryOpConcat(Expr\BinaryOp\Concat $node) |
||
| 346 | { |
||
| 347 | return $this->resolve($node->left) . $this->resolve($node->right); |
||
| 348 | } |
||
| 349 | |||
| 350 | protected function resolveExprTernary(Expr\Ternary $node) |
||
| 351 | { |
||
| 352 | if (isset($node->if)) { |
||
| 353 | // Full syntax $a ? $b : $c; |
||
| 354 | |||
| 355 | return $this->resolve($node->cond) ? $this->resolve($node->if) : $this->resolve($node->else); |
||
| 356 | } else { |
||
| 357 | // Short syntax $a ?: $c; |
||
| 358 | |||
| 359 | return $this->resolve($node->cond) ?: $this->resolve($node->else); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | protected function resolveExprBinaryOpSmallerOrEqual(Expr\BinaryOp\SmallerOrEqual $node) |
||
| 364 | { |
||
| 365 | return $this->resolve($node->left) <= $this->resolve($node->right); |
||
| 366 | } |
||
| 367 | |||
| 368 | protected function resolveExprBinaryOpGreaterOrEqual(Expr\BinaryOp\GreaterOrEqual $node) |
||
| 369 | { |
||
| 370 | return $this->resolve($node->left) >= $this->resolve($node->right); |
||
| 371 | } |
||
| 372 | |||
| 373 | protected function resolveExprBinaryOpEqual(Expr\BinaryOp\Equal $node) |
||
| 374 | { |
||
| 375 | return $this->resolve($node->left) == $this->resolve($node->right); |
||
| 376 | } |
||
| 377 | |||
| 378 | protected function resolveExprBinaryOpNotEqual(Expr\BinaryOp\NotEqual $node) |
||
| 379 | { |
||
| 380 | return $this->resolve($node->left) != $this->resolve($node->right); |
||
| 381 | } |
||
| 382 | |||
| 383 | protected function resolveExprBinaryOpSmaller(Expr\BinaryOp\Smaller $node) |
||
| 384 | { |
||
| 385 | return $this->resolve($node->left) < $this->resolve($node->right); |
||
| 386 | } |
||
| 387 | |||
| 388 | 1 | protected function resolveExprBinaryOpGreater(Expr\BinaryOp\Greater $node) |
|
| 392 | |||
| 393 | protected function resolveExprBinaryOpIdentical(Expr\BinaryOp\Identical $node) |
||
| 394 | { |
||
| 395 | return $this->resolve($node->left) === $this->resolve($node->right); |
||
| 396 | } |
||
| 397 | |||
| 398 | protected function resolveExprBinaryOpNotIdentical(Expr\BinaryOp\NotIdentical $node) |
||
| 399 | { |
||
| 400 | return $this->resolve($node->left) !== $this->resolve($node->right); |
||
| 401 | } |
||
| 402 | |||
| 403 | protected function resolveExprBinaryOpBooleanAnd(Expr\BinaryOp\BooleanAnd $node) |
||
| 404 | { |
||
| 405 | return $this->resolve($node->left) && $this->resolve($node->right); |
||
| 406 | } |
||
| 407 | |||
| 408 | protected function resolveExprBinaryOpLogicalAnd(Expr\BinaryOp\LogicalAnd $node) |
||
| 409 | { |
||
| 410 | return $this->resolve($node->left) and $this->resolve($node->right); |
||
| 411 | } |
||
| 412 | |||
| 413 | protected function resolveExprBinaryOpBooleanOr(Expr\BinaryOp\BooleanOr $node) |
||
| 414 | { |
||
| 415 | return $this->resolve($node->left) || $this->resolve($node->right); |
||
| 416 | } |
||
| 417 | |||
| 418 | protected function resolveExprBinaryOpLogicalOr(Expr\BinaryOp\LogicalOr $node) |
||
| 419 | { |
||
| 420 | return $this->resolve($node->left) or $this->resolve($node->right); |
||
| 421 | } |
||
| 422 | |||
| 423 | protected function resolveExprBinaryOpLogicalXor(Expr\BinaryOp\LogicalXor $node) |
||
| 424 | { |
||
| 425 | return $this->resolve($node->left) xor $this->resolve($node->right); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Utility method to fetch reflection class instance by name |
||
| 430 | * |
||
| 431 | * Supports: |
||
| 432 | * 'self' keyword |
||
| 433 | * 'parent' keyword |
||
| 434 | * not-FQN class names |
||
| 435 | * |
||
| 436 | * @param Node\Name $node Class name node |
||
| 437 | * |
||
| 438 | * @return bool|\ReflectionClass |
||
| 439 | * |
||
| 440 | * @throws ReflectionException |
||
| 441 | */ |
||
| 442 | 6 | private function fetchReflectionClass(Node\Name $node) |
|
| 485 | } |
||
| 486 |