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 |
||
| 16 | class Expression |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Context |
||
| 20 | */ |
||
| 21 | protected $context; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param Context $context |
||
| 25 | */ |
||
| 26 | 316 | public function __construct(Context $context) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @param $expr |
||
| 33 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 34 | */ |
||
| 35 | 312 | protected function factory($expr) |
|
| 36 | { |
||
| 37 | 312 | switch (get_class($expr)) { |
|
| 38 | /** |
||
| 39 | * Call(s) |
||
| 40 | */ |
||
| 41 | 312 | case 'PhpParser\Node\Expr\MethodCall': |
|
| 42 | return new Expression\MethodCall(); |
||
| 43 | 312 | case 'PhpParser\Node\Expr\FuncCall': |
|
| 44 | return new Expression\FunctionCall(); |
||
| 45 | 312 | case 'PhpParser\Node\Expr\StaticCall': |
|
| 46 | return new Expression\StaticCall(); |
||
| 47 | /** |
||
| 48 | * Operators |
||
| 49 | */ |
||
| 50 | 312 | case 'PhpParser\Node\Expr\New_': |
|
| 51 | return new Expression\Operators\NewOp(); |
||
| 52 | 312 | case 'PhpParser\Node\Expr\BinaryOp\Identical': |
|
| 53 | 28 | return new Expression\BinaryOp\Identical(); |
|
| 54 | 284 | case 'PhpParser\Node\Expr\BinaryOp\Concat': |
|
| 55 | return new Expression\Operators\Contact(); |
||
| 56 | 284 | case 'PhpParser\Node\Expr\BinaryOp\NotIdentical': |
|
| 57 | 14 | return new Expression\BinaryOp\NotIdentical(); |
|
| 58 | 270 | case 'PhpParser\Node\Expr\BinaryOp\Equal': |
|
| 59 | 34 | return new Expression\BinaryOp\Equal(); |
|
| 60 | 236 | case 'PhpParser\Node\Expr\BinaryOp\NotEqual': |
|
| 61 | 17 | return new Expression\BinaryOp\NotEqual(); |
|
| 62 | /** |
||
| 63 | * @link http://php.net/manual/en/language.operators.increment.php |
||
| 64 | */ |
||
| 65 | 219 | case 'PhpParser\Node\Expr\PostInc': |
|
| 66 | return new Expression\Operators\PostInc(); |
||
| 67 | 219 | case 'PhpParser\Node\Expr\PostDec': |
|
| 68 | return new Expression\Operators\PostDec(); |
||
| 69 | /** |
||
| 70 | * Arithmetical |
||
| 71 | */ |
||
| 72 | 219 | case 'PhpParser\Node\Expr\BinaryOp\Div': |
|
| 73 | 37 | return new Expression\Operators\Arithmetical\Div(); |
|
| 74 | 182 | case 'PhpParser\Node\Expr\BinaryOp\Plus': |
|
| 75 | 41 | return new Expression\Operators\Arithmetical\Plus(); |
|
| 76 | 141 | case 'PhpParser\Node\Expr\BinaryOp\Minus': |
|
| 77 | 18 | return new Expression\Operators\Arithmetical\Minus(); |
|
| 78 | 123 | case 'PhpParser\Node\Expr\BinaryOp\Mul': |
|
| 79 | 35 | return new Expression\Operators\Arithmetical\Mul(); |
|
| 80 | 88 | case 'PhpParser\Node\Expr\BinaryOp\Mod': |
|
| 81 | 35 | return new Expression\Operators\Arithmetical\Mod(); |
|
| 82 | /** |
||
| 83 | * Bitwise |
||
| 84 | * @link http://php.net/manual/ru/language.operators.bitwise.php |
||
| 85 | */ |
||
| 86 | 53 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseOr': |
|
| 87 | return new Expression\Operators\Bitwise\BitwiseOr(); |
||
| 88 | 53 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseXor': |
|
| 89 | return new Expression\Operators\Bitwise\BitwiseXor(); |
||
| 90 | 53 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseAnd': |
|
| 91 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
||
| 92 | 53 | case 'PhpParser\Node\Expr\BinaryOp\ShiftRight': |
|
| 93 | return new Expression\Operators\Bitwise\ShiftRight(); |
||
| 94 | 53 | case 'PhpParser\Node\Expr\BinaryOp\ShiftLeft': |
|
| 95 | return new Expression\Operators\Bitwise\ShiftLeft(); |
||
| 96 | 53 | case 'PhpParser\Node\Expr\BitwiseNot': |
|
| 97 | return new Expression\Operators\Bitwise\BitwiseNot(); |
||
| 98 | /** |
||
| 99 | * Logical |
||
| 100 | */ |
||
| 101 | 53 | case 'PhpParser\Node\Expr\BinaryOp\BooleanOr': |
|
| 102 | return new Expression\Operators\Logical\BooleanOr(); |
||
| 103 | 53 | case 'PhpParser\Node\Expr\BinaryOp\BooleanAnd': |
|
| 104 | 5 | return new Expression\Operators\Logical\BooleanAnd(); |
|
| 105 | 48 | case 'PhpParser\Node\Expr\BooleanNot': |
|
| 106 | return new Expression\Operators\Logical\BooleanNot(); |
||
| 107 | /** |
||
| 108 | * Comparison |
||
| 109 | */ |
||
| 110 | 48 | case 'PhpParser\Node\Expr\BinaryOp\Greater': |
|
| 111 | 12 | return new Expression\Operators\Comparison\Greater(); |
|
| 112 | 36 | case 'PhpParser\Node\Expr\BinaryOp\GreaterOrEqual': |
|
| 113 | 12 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
|
| 114 | 24 | case 'PhpParser\Node\Expr\BinaryOp\Smaller': |
|
| 115 | 12 | return new Expression\Operators\Comparison\Smaller(); |
|
| 116 | 12 | case 'PhpParser\Node\Expr\BinaryOp\SmallerOrEqual': |
|
| 117 | 12 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
|
| 118 | /** |
||
| 119 | * Another |
||
| 120 | */ |
||
| 121 | case 'PhpParser\Node\Expr\UnaryMinus': |
||
| 122 | return new Expression\Operators\UnaryMinus(); |
||
| 123 | case 'PhpParser\Node\Expr\UnaryPlus': |
||
| 124 | return new Expression\Operators\UnaryPlus(); |
||
| 125 | } |
||
| 126 | |||
| 127 | return false; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param $expr |
||
| 132 | * @return CompiledExpression |
||
| 133 | */ |
||
| 134 | 316 | public function compile($expr) |
|
| 135 | { |
||
| 136 | 316 | $className = get_class($expr); |
|
| 137 | switch ($className) { |
||
| 138 | 316 | case 'PhpParser\Node\Expr\PropertyFetch': |
|
| 139 | return $this->passPropertyFetch($expr); |
||
| 140 | 316 | case 'PhpParser\Node\Stmt\Property': |
|
| 141 | return $this->passProperty($expr); |
||
| 142 | 316 | case 'PhpParser\Node\Expr\ClassConstFetch': |
|
| 143 | return $this->passConstFetch($expr); |
||
| 144 | 316 | case 'PhpParser\Node\Expr\Assign': |
|
| 145 | return $this->passSymbol($expr); |
||
| 146 | 316 | case 'PhpParser\Node\Expr\AssignRef': |
|
| 147 | return $this->passSymbolByRef($expr); |
||
| 148 | 316 | case 'PhpParser\Node\Expr\Variable': |
|
| 149 | return $this->passExprVariable($expr); |
||
| 150 | /** |
||
| 151 | * Cast operators |
||
| 152 | */ |
||
| 153 | 316 | case 'PhpParser\Node\Expr\Cast\Bool_': |
|
| 154 | return $this->passCastBoolean($expr); |
||
| 155 | 316 | case 'PhpParser\Node\Expr\Cast\Int_': |
|
| 156 | return $this->passCastInt($expr); |
||
| 157 | 316 | case 'PhpParser\Node\Expr\Cast\Double': |
|
| 158 | return $this->passCastFloat($expr); |
||
| 159 | 316 | case 'PhpParser\Node\Expr\Cast\String_': |
|
| 160 | return $this->passCastString($expr); |
||
| 161 | 316 | case 'PhpParser\Node\Expr\Cast\Unset_': |
|
| 162 | return $this->passCastUnset($expr); |
||
| 163 | /** |
||
| 164 | * Expressions |
||
| 165 | */ |
||
| 166 | 316 | case 'PhpParser\Node\Expr\Array_': |
|
| 167 | 9 | return $this->getArray($expr); |
|
| 168 | 315 | case 'PhpParser\Node\Expr\ConstFetch': |
|
| 169 | return $this->constFetch($expr); |
||
| 170 | 315 | case 'PhpParser\Node\Name': |
|
| 171 | return $this->getNodeName($expr); |
||
| 172 | /** |
||
| 173 | * Simple Scalar(s) |
||
| 174 | */ |
||
| 175 | 315 | case 'PHPSA\Node\Scalar\Nil': |
|
| 176 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 177 | 315 | case 'PhpParser\Node\Scalar\LNumber': |
|
| 178 | 248 | return new CompiledExpression(CompiledExpression::LNUMBER, $expr->value); |
|
| 179 | 313 | case 'PhpParser\Node\Scalar\DNumber': |
|
| 180 | 130 | return new CompiledExpression(CompiledExpression::DNUMBER, $expr->value); |
|
| 181 | 313 | case 'PhpParser\Node\Scalar\String_': |
|
| 182 | 1 | return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
|
| 183 | 312 | case 'PHPSA\Node\Scalar\Boolean': |
|
| 184 | 44 | return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
|
| 185 | 312 | case 'PHPSA\Node\Scalar\Fake': |
|
| 186 | 27 | return new CompiledExpression($expr->type, $expr->value); |
|
| 187 | } |
||
| 188 | |||
| 189 | 312 | $expressionCompiler = $this->factory($expr); |
|
| 190 | 312 | if (!$expressionCompiler) { |
|
| 191 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
||
| 192 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
||
| 193 | } |
||
| 194 | |||
| 195 | 312 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 196 | 312 | if (!$result instanceof CompiledExpression) { |
|
| 197 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 198 | } |
||
| 199 | |||
| 200 | 312 | return $result; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @todo Implement |
||
| 205 | * |
||
| 206 | * @param Node\Stmt\Property $st |
||
| 207 | * @return CompiledExpression |
||
| 208 | */ |
||
| 209 | public function passProperty(Node\Stmt\Property $st) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param Node\Expr\Variable $expr |
||
| 216 | * @return CompiledExpression |
||
| 217 | */ |
||
| 218 | public function declareVariable(Node\Expr\Variable $expr) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param Node\Name $expr |
||
| 234 | * @return CompiledExpression |
||
| 235 | */ |
||
| 236 | public function getNodeName(Node\Name $expr) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * (bool) {$expr} |
||
| 248 | * |
||
| 249 | * @param Node\Expr\Cast\Bool_ $expr |
||
| 250 | * @return CompiledExpression |
||
| 251 | */ |
||
| 252 | protected function passCastBoolean(Node\Expr\Cast\Bool_ $expr) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * (int) {$expr} |
||
| 271 | * |
||
| 272 | * @param Node\Expr\Cast\Int_ $expr |
||
| 273 | * @return CompiledExpression |
||
| 274 | */ |
||
| 275 | protected function passCastInt(Node\Expr\Cast\Int_ $expr) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * (float) {$expr} |
||
| 295 | * |
||
| 296 | * @param Node\Expr\Cast\Double $expr |
||
| 297 | * @return CompiledExpression |
||
| 298 | */ |
||
| 299 | protected function passCastFloat(Node\Expr\Cast\Double $expr) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * (string) {$expr} |
||
| 319 | * |
||
| 320 | * @param Node\Expr\Cast\String_ $expr |
||
| 321 | * @return CompiledExpression |
||
| 322 | */ |
||
| 323 | protected function passCastString(Node\Expr\Cast\String_ $expr) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * (unset) {$expr} |
||
| 343 | * |
||
| 344 | * @param Node\Expr\Cast\Unset_ $expr |
||
| 345 | * @return CompiledExpression |
||
| 346 | */ |
||
| 347 | protected function passCastUnset(Node\Expr\Cast\Unset_ $expr) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param Node\Expr\PropertyFetch $expr |
||
| 363 | * @return CompiledExpression |
||
| 364 | */ |
||
| 365 | protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
||
| 366 | { |
||
| 367 | $scopeExpression = $this->compile($expr->var); |
||
| 368 | switch ($scopeExpression->getType()) { |
||
| 369 | case CompiledExpression::OBJECT: |
||
| 370 | $scopeExpressionValue = $scopeExpression->getValue(); |
||
| 371 | if ($scopeExpressionValue instanceof ClassDefinition) { |
||
| 372 | $propertyName = is_string($expr->name) ? $expr->name : false; |
||
| 373 | if ($expr->name instanceof Variable) { |
||
| 374 | /** |
||
| 375 | * @todo implement fetch from symbol table |
||
| 376 | */ |
||
| 377 | //$methodName = $expr->name->name; |
||
| 378 | } |
||
| 379 | |||
| 380 | if ($propertyName) { |
||
| 381 | if ($scopeExpressionValue->hasProperty($propertyName, true)) { |
||
| 382 | $property = $scopeExpressionValue->getProperty($propertyName, true); |
||
| 383 | return new CompiledExpression(); |
||
| 384 | } else { |
||
| 385 | $this->context->notice( |
||
| 386 | 'undefined-property', |
||
| 387 | sprintf( |
||
| 388 | 'Property %s does not exist in %s scope', |
||
| 389 | $propertyName, |
||
| 390 | $scopeExpressionValue->getName() |
||
| 391 | ), |
||
| 392 | $expr |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | } |
||
| 397 | break; |
||
| 398 | default: |
||
| 399 | $this->context->debug('You cannot fetch property from not object type'); |
||
| 400 | break; |
||
| 401 | } |
||
| 402 | |||
| 403 | $this->context->debug('Unknown property fetch'); |
||
| 404 | return new CompiledExpression(); |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param Node\Expr\ClassConstFetch $expr |
||
| 409 | * @return CompiledExpression |
||
| 410 | */ |
||
| 411 | protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param Node\Expr\Assign $expr |
||
| 439 | * @return CompiledExpression |
||
| 440 | */ |
||
| 441 | protected function passSymbol(Node\Expr\Assign $expr) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param Node\Expr\AssignRef $expr |
||
| 523 | * @return CompiledExpression |
||
| 524 | */ |
||
| 525 | protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @param Node\Expr\Variable $expr |
||
| 574 | * @return CompiledExpression |
||
| 575 | */ |
||
| 576 | protected function passExprVariable(Node\Expr\Variable $expr) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Compile Array_ expression to CompiledExpression |
||
| 595 | * |
||
| 596 | * @param Node\Expr\Array_ $expr |
||
| 597 | * @return CompiledExpression |
||
| 598 | */ |
||
| 599 | 9 | protected function getArray(Node\Expr\Array_ $expr) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Convert const fetch expr to CompiledExpression |
||
| 634 | * |
||
| 635 | * @param Node\Expr\ConstFetch $expr |
||
| 636 | * @return CompiledExpression |
||
| 637 | */ |
||
| 638 | protected function constFetch(Node\Expr\ConstFetch $expr) |
||
| 659 | } |
||
| 660 |
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.