Complex classes like Expression 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 Expression, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Expression |
||
|
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var Context |
||
| 24 | */ |
||
| 25 | protected $context; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var EventManager |
||
| 29 | */ |
||
| 30 | protected $eventManager; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param Context $context |
||
| 34 | */ |
||
| 35 | 17 | public function __construct(Context $context, EventManager $eventManager) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param $expr |
||
| 43 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 44 | */ |
||
| 45 | 11 | protected function factory($expr) |
|
| 46 | { |
||
| 47 | 11 | switch (get_class($expr)) { |
|
| 48 | /** |
||
| 49 | * Call(s) |
||
| 50 | */ |
||
| 51 | 11 | case Node\Expr\MethodCall::class: |
|
| 52 | return new Expression\MethodCall(); |
||
| 53 | 11 | case Node\Expr\FuncCall::class: |
|
| 54 | 10 | return new Expression\FunctionCall(); |
|
| 55 | 3 | case Node\Expr\StaticCall::class: |
|
| 56 | return new Expression\StaticCall(); |
||
| 57 | /** |
||
| 58 | * Operators |
||
| 59 | */ |
||
| 60 | 3 | case Node\Expr\New_::class: |
|
| 61 | 1 | return new Expression\Operators\NewOp(); |
|
| 62 | 2 | case Node\Expr\Instanceof_::class: |
|
| 63 | return new Expression\Operators\InstanceOfOp(); |
||
| 64 | /** |
||
| 65 | * AssignOp |
||
| 66 | */ |
||
| 67 | 2 | case Node\Expr\AssignOp\Pow::class: |
|
| 68 | return new Expression\AssignOp\Pow(); |
||
| 69 | 2 | case Node\Expr\AssignOp\Plus::class: |
|
| 70 | return new Expression\AssignOp\Plus(); |
||
| 71 | 2 | case Node\Expr\AssignOp\Minus::class: |
|
| 72 | return new Expression\AssignOp\Minus(); |
||
| 73 | 2 | case Node\Expr\AssignOp\Mod::class: |
|
| 74 | return new Expression\AssignOp\Mod(); |
||
| 75 | 2 | case Node\Expr\AssignOp\BitwiseOr::class: |
|
| 76 | return new Expression\AssignOp\BitwiseOr(); |
||
| 77 | 2 | case Node\Expr\AssignOp\BitwiseAnd::class: |
|
| 78 | return new Expression\AssignOp\BitwiseAnd(); |
||
| 79 | /** |
||
| 80 | * BinaryOp |
||
| 81 | */ |
||
| 82 | 2 | case Node\Expr\BinaryOp\Identical::class: |
|
| 83 | return new Expression\BinaryOp\Identical(); |
||
| 84 | 2 | case Node\Expr\BinaryOp\Concat::class: |
|
| 85 | 1 | return new Expression\Operators\Concat(); |
|
| 86 | 1 | case Node\Expr\BinaryOp\NotIdentical::class: |
|
| 87 | return new Expression\BinaryOp\NotIdentical(); |
||
| 88 | 1 | case Node\Expr\BinaryOp\Equal::class: |
|
| 89 | return new Expression\BinaryOp\Equal(); |
||
| 90 | 1 | case Node\Expr\BinaryOp\NotEqual::class: |
|
| 91 | return new Expression\BinaryOp\NotEqual(); |
||
| 92 | /** |
||
| 93 | * @link http://php.net/manual/en/language.operators.increment.php |
||
| 94 | */ |
||
| 95 | 1 | case Node\Expr\PostInc::class: |
|
| 96 | return new Expression\Operators\PostInc(); |
||
| 97 | 1 | case Node\Expr\PostDec::class: |
|
| 98 | return new Expression\Operators\PostDec(); |
||
| 99 | /** |
||
| 100 | * Arithmetical |
||
| 101 | */ |
||
| 102 | 1 | case Node\Expr\BinaryOp\Div::class: |
|
| 103 | return new Expression\Operators\Arithmetical\Div(); |
||
| 104 | 1 | case Node\Expr\BinaryOp\Plus::class: |
|
| 105 | return new Expression\Operators\Arithmetical\Plus(); |
||
| 106 | 1 | case Node\Expr\BinaryOp\Minus::class: |
|
| 107 | return new Expression\Operators\Arithmetical\Minus(); |
||
| 108 | 1 | case Node\Expr\BinaryOp\Mul::class: |
|
| 109 | return new Expression\Operators\Arithmetical\Mul(); |
||
| 110 | 1 | case Node\Expr\BinaryOp\Mod::class: |
|
| 111 | return new Expression\Operators\Arithmetical\Mod(); |
||
| 112 | /** |
||
| 113 | * Bitwise |
||
| 114 | * @link http://php.net/manual/ru/language.operators.bitwise.php |
||
| 115 | */ |
||
| 116 | 1 | case Node\Expr\BinaryOp\BitwiseOr::class: |
|
| 117 | return new Expression\Operators\Bitwise\BitwiseOr(); |
||
| 118 | 1 | case Node\Expr\BinaryOp\BitwiseXor::class: |
|
| 119 | return new Expression\Operators\Bitwise\BitwiseXor(); |
||
| 120 | 1 | case Node\Expr\BinaryOp\BitwiseAnd::class: |
|
| 121 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
||
| 122 | 1 | case Node\Expr\BinaryOp\ShiftRight::class: |
|
| 123 | return new Expression\Operators\Bitwise\ShiftRight(); |
||
| 124 | 1 | case Node\Expr\BinaryOp\ShiftLeft::class: |
|
| 125 | return new Expression\Operators\Bitwise\ShiftLeft(); |
||
| 126 | 1 | case Node\Expr\BitwiseNot::class: |
|
| 127 | return new Expression\Operators\Bitwise\BitwiseNot(); |
||
| 128 | /** |
||
| 129 | * Logical |
||
| 130 | */ |
||
| 131 | 1 | case Node\Expr\BinaryOp\BooleanOr::class: |
|
| 132 | return new Expression\Operators\Logical\BooleanOr(); |
||
| 133 | 1 | case Node\Expr\BinaryOp\BooleanAnd::class: |
|
| 134 | return new Expression\Operators\Logical\BooleanAnd(); |
||
| 135 | 1 | case Node\Expr\BooleanNot::class: |
|
| 136 | return new Expression\Operators\Logical\BooleanNot(); |
||
| 137 | /** |
||
| 138 | * Comparison |
||
| 139 | */ |
||
| 140 | 1 | case Node\Expr\BinaryOp\Greater::class: |
|
| 141 | return new Expression\Operators\Comparison\Greater(); |
||
| 142 | 1 | case Node\Expr\BinaryOp\GreaterOrEqual::class: |
|
| 143 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
||
| 144 | 1 | case Node\Expr\BinaryOp\Smaller::class: |
|
| 145 | return new Expression\Operators\Comparison\Smaller(); |
||
| 146 | 1 | case Node\Expr\BinaryOp\SmallerOrEqual::class: |
|
| 147 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Casts |
||
| 151 | */ |
||
| 152 | 1 | case Node\Expr\Cast\Array_::class: |
|
| 153 | return new Expression\Casts\ArrayCast(); |
||
| 154 | 1 | case Node\Expr\Cast\Bool_::class: |
|
| 155 | return new Expression\Casts\BoolCast(); |
||
| 156 | 1 | case Node\Expr\Cast\Int_::class: |
|
| 157 | return new Expression\Casts\IntCast(); |
||
| 158 | 1 | case Node\Expr\Cast\Double::class: |
|
| 159 | return new Expression\Casts\DoubleCast(); |
||
| 160 | 1 | case Node\Expr\Cast\Object_::class: |
|
| 161 | return new Expression\Casts\ObjectCast(); |
||
| 162 | 1 | case Node\Expr\Cast\String_::class: |
|
| 163 | return new Expression\Casts\StringCast(); |
||
| 164 | 1 | case Node\Expr\Cast\Unset_::class: |
|
| 165 | return new Expression\Casts\UnsetCast(); |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Other |
||
| 170 | */ |
||
| 171 | 1 | case Node\Expr\Closure::class: |
|
| 172 | return new Expression\Closure(); |
||
| 173 | 1 | case Node\Expr\UnaryMinus::class: |
|
| 174 | return new Expression\Operators\UnaryMinus(); |
||
| 175 | 1 | case Node\Expr\UnaryPlus::class: |
|
| 176 | return new Expression\Operators\UnaryPlus(); |
||
| 177 | 1 | } |
|
| 178 | |||
| 179 | 1 | return false; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param object|string $expr |
||
| 184 | * @return CompiledExpression |
||
| 185 | */ |
||
| 186 | 17 | public function compile($expr) |
|
| 187 | { |
||
| 188 | 17 | if (is_string($expr)) { |
|
| 189 | 7 | return new CompiledExpression(CompiledExpression::STRING, $expr); |
|
| 190 | } |
||
| 191 | |||
| 192 | 17 | if (is_null($expr)) { |
|
| 193 | 1 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 194 | } |
||
| 195 | |||
| 196 | 17 | if (!is_object($expr)) { |
|
| 197 | throw new InvalidArgumentException('$expr must be string/object/null'); |
||
| 198 | } |
||
| 199 | |||
| 200 | 17 | $this->eventManager->fire( |
|
| 201 | 17 | ExpressionBeforeCompile::EVENT_NAME, |
|
| 202 | 17 | new ExpressionBeforeCompile( |
|
| 203 | 17 | $expr, |
|
| 204 | 17 | $this->context |
|
| 205 | 17 | ) |
|
| 206 | 17 | ); |
|
| 207 | |||
| 208 | 17 | $className = get_class($expr); |
|
| 209 | switch ($className) { |
||
| 210 | 17 | case Node\Arg::class: |
|
| 211 | /** |
||
| 212 | * @todo Better compile |
||
| 213 | */ |
||
| 214 | 2 | return $this->compile($expr->value); |
|
| 215 | 17 | case Node\Expr\PropertyFetch::class: |
|
| 216 | return $this->passPropertyFetch($expr); |
||
| 217 | 17 | case Node\Stmt\Property::class: |
|
| 218 | return $this->passProperty($expr); |
||
| 219 | 17 | case Node\Expr\ClassConstFetch::class: |
|
| 220 | return $this->passConstFetch($expr); |
||
| 221 | 17 | case Node\Expr\Assign::class: |
|
| 222 | 6 | return $this->passSymbol($expr); |
|
| 223 | 17 | case Node\Expr\AssignRef::class: |
|
| 224 | return $this->passSymbolByRef($expr); |
||
| 225 | 17 | case Node\Expr\Variable::class: |
|
| 226 | 4 | return $this->passExprVariable($expr); |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Expressions |
||
| 230 | */ |
||
| 231 | 17 | case Node\Expr\Array_::class: |
|
| 232 | 4 | return $this->getArray($expr); |
|
| 233 | 17 | case Node\Expr\ConstFetch::class: |
|
| 234 | 4 | return $this->constFetch($expr); |
|
| 235 | 17 | case Node\Name::class: |
|
| 236 | 10 | return $this->getNodeName($expr); |
|
| 237 | 17 | case Node\Name\FullyQualified::class: |
|
| 238 | return $this->getFullyQualifiedNodeName($expr); |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Simple Scalar(s) |
||
| 242 | */ |
||
| 243 | 17 | case \PHPSA\Node\Scalar\Nil::class: |
|
| 244 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 245 | 17 | case Node\Scalar\LNumber::class: |
|
| 246 | 11 | return new CompiledExpression(CompiledExpression::INTEGER, $expr->value); |
|
| 247 | 13 | case Node\Scalar\DNumber::class: |
|
| 248 | return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value); |
||
| 249 | 13 | case Node\Scalar\String_::class: |
|
| 250 | 8 | return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
|
| 251 | 11 | case \PHPSA\Node\Scalar\Boolean::class: |
|
| 252 | return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
||
| 253 | 11 | case \PHPSA\Node\Scalar\Fake::class: |
|
| 254 | return new CompiledExpression($expr->type, $expr->value); |
||
| 255 | } |
||
| 256 | |||
| 257 | 11 | $expressionCompiler = $this->factory($expr); |
|
| 258 | 11 | if (!$expressionCompiler) { |
|
| 259 | 1 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
|
| 260 | 1 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
|
| 261 | } |
||
| 262 | |||
| 263 | 11 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 264 | 11 | if (!$result instanceof CompiledExpression) { |
|
| 265 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 266 | } |
||
| 267 | |||
| 268 | 11 | return $result; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @todo Implement |
||
| 273 | * |
||
| 274 | * @param Node\Stmt\Property $st |
||
| 275 | * @return CompiledExpression |
||
| 276 | */ |
||
| 277 | public function passProperty(Node\Stmt\Property $st) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param Node\Expr\Variable $expr |
||
| 340 | * @return CompiledExpression |
||
| 341 | */ |
||
| 342 | 1 | public function declareVariable(Node\Expr\Variable $expr) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @param Node\Name\FullyQualified $expr |
||
| 355 | * @return CompiledExpression |
||
| 356 | */ |
||
| 357 | public function getFullyQualifiedNodeName(Node\Name\FullyQualified $expr) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param Node\Name $expr |
||
| 366 | * @return CompiledExpression |
||
| 367 | */ |
||
| 368 | 10 | public function getNodeName(Node\Name $expr) |
|
| 369 | { |
||
| 370 | 10 | $nodeString = $expr->toString(); |
|
| 371 | 10 | if ($nodeString === 'null') { |
|
| 372 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 373 | } |
||
| 374 | |||
| 375 | 10 | if (in_array($nodeString, ['parent'], true)) { |
|
| 376 | /** @var ClassDefinition $scope */ |
||
| 377 | $scope = $this->context->scope; |
||
| 378 | assert($scope instanceof ClassDefinition); |
||
| 379 | |||
| 380 | if ($scope->getExtendsClass()) { |
||
| 381 | $definition = $scope->getExtendsClassDefinition(); |
||
| 382 | if ($definition) { |
||
| 383 | return new CompiledExpression(CompiledExpression::OBJECT, $definition); |
||
| 384 | } |
||
| 385 | } else { |
||
| 386 | $this->context->notice( |
||
| 387 | 'no-parent', |
||
| 388 | 'Cannot access parent:: when current class scope has no parent', |
||
| 389 | $expr |
||
| 390 | ); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | 10 | if (in_array($nodeString, ['self', 'static'], true)) { |
|
| 395 | return CompiledExpression::fromZvalValue($this->context->scope); |
||
| 396 | } |
||
| 397 | |||
| 398 | 10 | if (defined($nodeString)) { |
|
| 399 | 1 | return CompiledExpression::fromZvalValue(constant($expr)); |
|
| 400 | } |
||
| 401 | |||
| 402 | 10 | return new CompiledExpression(CompiledExpression::STRING, $expr->toString()); |
|
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param Node\Expr\PropertyFetch $expr |
||
| 407 | * @return CompiledExpression |
||
| 408 | */ |
||
| 409 | protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param Node\Expr\ClassConstFetch $expr |
||
| 453 | * @return CompiledExpression |
||
| 454 | */ |
||
| 455 | protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param Node\Expr\Assign $expr |
||
| 480 | * @return CompiledExpression |
||
| 481 | */ |
||
| 482 | 6 | protected function passSymbol(Node\Expr\Assign $expr) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * @param Node\Expr\AssignRef $expr |
||
| 579 | * @return CompiledExpression |
||
| 580 | */ |
||
| 581 | protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
||
| 582 | { |
||
| 583 | if ($expr->var instanceof Node\Expr\Variable) { |
||
| 584 | $name = $expr->var->name; |
||
| 585 | |||
| 586 | $compiledExpression = $this->compile($expr->expr); |
||
| 587 | |||
| 588 | $symbol = $this->context->getSymbol($name); |
||
| 589 | if ($symbol) { |
||
| 590 | $symbol->modify($compiledExpression->getType(), $compiledExpression->getValue()); |
||
| 591 | } else { |
||
| 592 | $symbol = new Variable( |
||
| 593 | $name, |
||
| 594 | $compiledExpression->getValue(), |
||
| 595 | $compiledExpression->getType(), |
||
| 596 | $this->context->getCurrentBranch() |
||
| 597 | ); |
||
| 598 | $this->context->addVariable($symbol); |
||
| 599 | } |
||
| 600 | |||
| 601 | if ($expr->expr instanceof Node\Expr\Variable) { |
||
| 602 | $rightVarName = $expr->expr->name; |
||
| 603 | |||
| 604 | $rightSymbol = $this->context->getSymbol($rightVarName); |
||
| 605 | if ($rightSymbol) { |
||
| 606 | $rightSymbol->incUse(); |
||
| 607 | $symbol->setReferencedTo($rightSymbol); |
||
| 608 | } else { |
||
| 609 | $this->context->debug('Cannot fetch variable by name: ' . $rightVarName); |
||
| 610 | } |
||
| 611 | } |
||
| 612 | |||
| 613 | $symbol->incSets(); |
||
| 614 | return $compiledExpression; |
||
| 615 | } |
||
| 616 | |||
| 617 | $this->context->debug('Unknown how to pass symbol by ref'); |
||
| 618 | return new CompiledExpression(); |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param Node\Expr\Variable $expr |
||
| 623 | * @return CompiledExpression |
||
| 624 | */ |
||
| 625 | 4 | protected function passExprVariable(Node\Expr\Variable $expr) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Compile Array_ expression to CompiledExpression |
||
| 644 | * |
||
| 645 | * @param Node\Expr\Array_ $expr |
||
| 646 | * @return CompiledExpression |
||
| 647 | */ |
||
| 648 | 4 | protected function getArray(Node\Expr\Array_ $expr) |
|
| 649 | { |
||
| 650 | 4 | if ($expr->items === []) { |
|
| 651 | return new CompiledExpression(CompiledExpression::ARR, []); |
||
| 652 | } |
||
| 653 | |||
| 654 | 4 | $resultArray = []; |
|
| 655 | |||
| 656 | 4 | foreach ($expr->items as $item) { |
|
| 657 | 4 | $compiledValueResult = $this->compile($item->value); |
|
| 658 | 4 | if ($item->key) { |
|
| 659 | $compiledKeyResult = $this->compile($item->key); |
||
| 660 | switch ($compiledKeyResult->getType()) { |
||
| 661 | case CompiledExpression::INTEGER: |
||
| 662 | case CompiledExpression::DOUBLE: |
||
| 663 | case CompiledExpression::BOOLEAN: |
||
| 664 | case CompiledExpression::NULL: |
||
| 665 | case CompiledExpression::STRING: |
||
| 666 | $resultArray[$compiledKeyResult->getValue()] = $compiledValueResult->getValue(); |
||
| 667 | break; |
||
| 668 | default: |
||
| 669 | $this->context->debug("Type {$compiledKeyResult->getType()} is not supported for key value"); |
||
| 670 | return new CompiledExpression(CompiledExpression::ARR); |
||
| 671 | break; |
||
| 672 | } |
||
| 673 | } else { |
||
| 674 | 4 | $resultArray[] = $compiledValueResult->getValue(); |
|
| 675 | } |
||
| 676 | 4 | } |
|
| 677 | |||
| 678 | 4 | return new CompiledExpression(CompiledExpression::ARR, $resultArray); |
|
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Convert const fetch expr to CompiledExpression |
||
| 683 | * |
||
| 684 | * @param Node\Expr\ConstFetch $expr |
||
| 685 | * @return CompiledExpression |
||
| 686 | */ |
||
| 687 | 4 | protected function constFetch(Node\Expr\ConstFetch $expr) |
|
| 704 | } |
||
| 705 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.