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 | 363 | public function __construct(Context $context) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @param $expr |
||
| 33 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 34 | */ |
||
| 35 | 353 | protected function factory($expr) |
|
| 36 | { |
||
| 37 | 353 | switch (get_class($expr)) { |
|
| 38 | /** |
||
| 39 | * Call(s) |
||
| 40 | */ |
||
| 41 | 353 | case 'PhpParser\Node\Expr\MethodCall': |
|
| 42 | return new Expression\MethodCall(); |
||
| 43 | 353 | case 'PhpParser\Node\Expr\FuncCall': |
|
| 44 | return new Expression\FunctionCall(); |
||
| 45 | 353 | case 'PhpParser\Node\Expr\StaticCall': |
|
| 46 | return new Expression\StaticCall(); |
||
| 47 | /** |
||
| 48 | * Operators |
||
| 49 | */ |
||
| 50 | 353 | case 'PhpParser\Node\Expr\New_': |
|
| 51 | return new Expression\Operators\NewOp(); |
||
| 52 | 353 | case 'PhpParser\Node\Expr\Instanceof_': |
|
| 53 | return new Expression\Operators\InstanceOfOp(); |
||
| 54 | 353 | case 'PhpParser\Node\Expr\BinaryOp\Identical': |
|
| 55 | 28 | return new Expression\BinaryOp\Identical(); |
|
| 56 | 325 | case 'PhpParser\Node\Expr\BinaryOp\Concat': |
|
| 57 | return new Expression\Operators\Contact(); |
||
| 58 | 325 | case 'PhpParser\Node\Expr\BinaryOp\NotIdentical': |
|
| 59 | 14 | return new Expression\BinaryOp\NotIdentical(); |
|
| 60 | 311 | case 'PhpParser\Node\Expr\BinaryOp\Equal': |
|
| 61 | 34 | return new Expression\BinaryOp\Equal(); |
|
| 62 | 277 | case 'PhpParser\Node\Expr\BinaryOp\NotEqual': |
|
| 63 | 17 | return new Expression\BinaryOp\NotEqual(); |
|
| 64 | /** |
||
| 65 | * @link http://php.net/manual/en/language.operators.increment.php |
||
| 66 | */ |
||
| 67 | 260 | case 'PhpParser\Node\Expr\PostInc': |
|
| 68 | 4 | return new Expression\Operators\PostInc(); |
|
| 69 | 256 | case 'PhpParser\Node\Expr\PostDec': |
|
| 70 | 4 | return new Expression\Operators\PostDec(); |
|
| 71 | /** |
||
| 72 | * Arithmetical |
||
| 73 | */ |
||
| 74 | 252 | case 'PhpParser\Node\Expr\BinaryOp\Div': |
|
| 75 | 37 | return new Expression\Operators\Arithmetical\Div(); |
|
| 76 | 215 | case 'PhpParser\Node\Expr\BinaryOp\Plus': |
|
| 77 | 41 | return new Expression\Operators\Arithmetical\Plus(); |
|
| 78 | 174 | case 'PhpParser\Node\Expr\BinaryOp\Minus': |
|
| 79 | 18 | return new Expression\Operators\Arithmetical\Minus(); |
|
| 80 | 156 | case 'PhpParser\Node\Expr\BinaryOp\Mul': |
|
| 81 | 35 | return new Expression\Operators\Arithmetical\Mul(); |
|
| 82 | 121 | case 'PhpParser\Node\Expr\BinaryOp\Mod': |
|
| 83 | 35 | return new Expression\Operators\Arithmetical\Mod(); |
|
| 84 | /** |
||
| 85 | * Bitwise |
||
| 86 | * @link http://php.net/manual/ru/language.operators.bitwise.php |
||
| 87 | */ |
||
| 88 | 86 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseOr': |
|
| 89 | return new Expression\Operators\Bitwise\BitwiseOr(); |
||
| 90 | 86 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseXor': |
|
| 91 | return new Expression\Operators\Bitwise\BitwiseXor(); |
||
| 92 | 86 | case 'PhpParser\Node\Expr\BinaryOp\BitwiseAnd': |
|
| 93 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
||
| 94 | 86 | case 'PhpParser\Node\Expr\BinaryOp\ShiftRight': |
|
| 95 | return new Expression\Operators\Bitwise\ShiftRight(); |
||
| 96 | 86 | case 'PhpParser\Node\Expr\BinaryOp\ShiftLeft': |
|
| 97 | return new Expression\Operators\Bitwise\ShiftLeft(); |
||
| 98 | 86 | case 'PhpParser\Node\Expr\BitwiseNot': |
|
| 99 | return new Expression\Operators\Bitwise\BitwiseNot(); |
||
| 100 | /** |
||
| 101 | * Logical |
||
| 102 | */ |
||
| 103 | 86 | case 'PhpParser\Node\Expr\BinaryOp\BooleanOr': |
|
| 104 | 10 | return new Expression\Operators\Logical\BooleanOr(); |
|
| 105 | 76 | case 'PhpParser\Node\Expr\BinaryOp\BooleanAnd': |
|
| 106 | 5 | return new Expression\Operators\Logical\BooleanAnd(); |
|
| 107 | 71 | case 'PhpParser\Node\Expr\BooleanNot': |
|
| 108 | 5 | return new Expression\Operators\Logical\BooleanNot(); |
|
| 109 | /** |
||
| 110 | * Comparison |
||
| 111 | */ |
||
| 112 | 66 | case 'PhpParser\Node\Expr\BinaryOp\Greater': |
|
| 113 | 12 | return new Expression\Operators\Comparison\Greater(); |
|
| 114 | 54 | case 'PhpParser\Node\Expr\BinaryOp\GreaterOrEqual': |
|
| 115 | 12 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
|
| 116 | 42 | case 'PhpParser\Node\Expr\BinaryOp\Smaller': |
|
| 117 | 12 | return new Expression\Operators\Comparison\Smaller(); |
|
| 118 | 30 | case 'PhpParser\Node\Expr\BinaryOp\SmallerOrEqual': |
|
| 119 | 12 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
|
| 120 | /** |
||
| 121 | * Another |
||
| 122 | */ |
||
| 123 | 18 | case 'PhpParser\Node\Expr\UnaryMinus': |
|
| 124 | 9 | return new Expression\Operators\UnaryMinus(); |
|
| 125 | 9 | case 'PhpParser\Node\Expr\UnaryPlus': |
|
| 126 | 9 | return new Expression\Operators\UnaryPlus(); |
|
| 127 | } |
||
| 128 | |||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param $expr |
||
| 134 | * @return CompiledExpression |
||
| 135 | */ |
||
| 136 | 363 | public function compile($expr) |
|
| 137 | { |
||
| 138 | 363 | $className = get_class($expr); |
|
| 139 | switch ($className) { |
||
| 140 | 363 | case 'PhpParser\Node\Expr\PropertyFetch': |
|
| 141 | return $this->passPropertyFetch($expr); |
||
| 142 | 363 | case 'PhpParser\Node\Stmt\Property': |
|
| 143 | return $this->passProperty($expr); |
||
| 144 | 363 | case 'PhpParser\Node\Expr\ClassConstFetch': |
|
| 145 | return $this->passConstFetch($expr); |
||
| 146 | 363 | case 'PhpParser\Node\Expr\Assign': |
|
| 147 | return $this->passSymbol($expr); |
||
| 148 | 363 | case 'PhpParser\Node\Expr\AssignRef': |
|
| 149 | return $this->passSymbolByRef($expr); |
||
| 150 | 363 | case 'PhpParser\Node\Expr\Variable': |
|
| 151 | return $this->passExprVariable($expr); |
||
| 152 | /** |
||
| 153 | * Cast operators |
||
| 154 | */ |
||
| 155 | 363 | case 'PhpParser\Node\Expr\Cast\Bool_': |
|
| 156 | return $this->passCastBoolean($expr); |
||
| 157 | 363 | case 'PhpParser\Node\Expr\Cast\Int_': |
|
| 158 | return $this->passCastInt($expr); |
||
| 159 | 363 | case 'PhpParser\Node\Expr\Cast\Double': |
|
| 160 | return $this->passCastFloat($expr); |
||
| 161 | 363 | case 'PhpParser\Node\Expr\Cast\String_': |
|
| 162 | return $this->passCastString($expr); |
||
| 163 | 363 | case 'PhpParser\Node\Expr\Cast\Unset_': |
|
| 164 | return $this->passCastUnset($expr); |
||
| 165 | /** |
||
| 166 | * Expressions |
||
| 167 | */ |
||
| 168 | 363 | case 'PhpParser\Node\Expr\Array_': |
|
| 169 | 11 | return $this->getArray($expr); |
|
| 170 | 362 | case 'PhpParser\Node\Expr\ConstFetch': |
|
| 171 | return $this->constFetch($expr); |
||
| 172 | 362 | case 'PhpParser\Node\Name': |
|
| 173 | return $this->getNodeName($expr); |
||
| 174 | /** |
||
| 175 | * Simple Scalar(s) |
||
| 176 | */ |
||
| 177 | 362 | case 'PHPSA\Node\Scalar\Nil': |
|
| 178 | 8 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 179 | 361 | case 'PhpParser\Node\Scalar\LNumber': |
|
| 180 | 256 | return new CompiledExpression(CompiledExpression::INTEGER, $expr->value); |
|
| 181 | 358 | case 'PhpParser\Node\Scalar\DNumber': |
|
| 182 | 131 | return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value); |
|
| 183 | 357 | case 'PhpParser\Node\Scalar\String_': |
|
| 184 | 8 | return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
|
| 185 | 355 | case 'PHPSA\Node\Scalar\Boolean': |
|
| 186 | 60 | return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
|
| 187 | 353 | case 'PHPSA\Node\Scalar\Fake': |
|
| 188 | 29 | return new CompiledExpression($expr->type, $expr->value); |
|
| 189 | } |
||
| 190 | |||
| 191 | 353 | $expressionCompiler = $this->factory($expr); |
|
| 192 | 353 | if (!$expressionCompiler) { |
|
| 193 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
||
| 194 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
||
| 195 | } |
||
| 196 | |||
| 197 | 353 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 198 | 353 | if (!$result instanceof CompiledExpression) { |
|
| 199 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 200 | } |
||
| 201 | |||
| 202 | 353 | return $result; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @todo Implement |
||
| 207 | * |
||
| 208 | * @param Node\Stmt\Property $st |
||
| 209 | * @return CompiledExpression |
||
| 210 | */ |
||
| 211 | public function passProperty(Node\Stmt\Property $st) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Node\Expr\Variable $expr |
||
| 218 | * @return CompiledExpression |
||
| 219 | */ |
||
| 220 | public function declareVariable(Node\Expr\Variable $expr) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param Node\Name $expr |
||
| 236 | * @return CompiledExpression |
||
| 237 | */ |
||
| 238 | public function getNodeName(Node\Name $expr) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * (bool) {$expr} |
||
| 262 | * |
||
| 263 | * @param Node\Expr\Cast\Bool_ $expr |
||
| 264 | * @return CompiledExpression |
||
| 265 | */ |
||
| 266 | protected function passCastBoolean(Node\Expr\Cast\Bool_ $expr) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * (int) {$expr} |
||
| 285 | * |
||
| 286 | * @param Node\Expr\Cast\Int_ $expr |
||
| 287 | * @return CompiledExpression |
||
| 288 | */ |
||
| 289 | protected function passCastInt(Node\Expr\Cast\Int_ $expr) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * (float) {$expr} |
||
| 309 | * |
||
| 310 | * @param Node\Expr\Cast\Double $expr |
||
| 311 | * @return CompiledExpression |
||
| 312 | */ |
||
| 313 | protected function passCastFloat(Node\Expr\Cast\Double $expr) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * (string) {$expr} |
||
| 333 | * |
||
| 334 | * @param Node\Expr\Cast\String_ $expr |
||
| 335 | * @return CompiledExpression |
||
| 336 | */ |
||
| 337 | protected function passCastString(Node\Expr\Cast\String_ $expr) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * (unset) {$expr} |
||
| 357 | * |
||
| 358 | * @param Node\Expr\Cast\Unset_ $expr |
||
| 359 | * @return CompiledExpression |
||
| 360 | */ |
||
| 361 | protected function passCastUnset(Node\Expr\Cast\Unset_ $expr) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param Node\Expr\PropertyFetch $expr |
||
| 377 | * @return CompiledExpression |
||
| 378 | */ |
||
| 379 | protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param Node\Expr\ClassConstFetch $expr |
||
| 423 | * @return CompiledExpression |
||
| 424 | */ |
||
| 425 | protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param Node\Expr\Assign $expr |
||
| 453 | * @return CompiledExpression |
||
| 454 | */ |
||
| 455 | protected function passSymbol(Node\Expr\Assign $expr) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param Node\Expr\AssignRef $expr |
||
| 537 | * @return CompiledExpression |
||
| 538 | */ |
||
| 539 | protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @param Node\Expr\Variable $expr |
||
| 588 | * @return CompiledExpression |
||
| 589 | */ |
||
| 590 | protected function passExprVariable(Node\Expr\Variable $expr) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Compile Array_ expression to CompiledExpression |
||
| 609 | * |
||
| 610 | * @param Node\Expr\Array_ $expr |
||
| 611 | * @return CompiledExpression |
||
| 612 | */ |
||
| 613 | 11 | protected function getArray(Node\Expr\Array_ $expr) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Convert const fetch expr to CompiledExpression |
||
| 648 | * |
||
| 649 | * @param Node\Expr\ConstFetch $expr |
||
| 650 | * @return CompiledExpression |
||
| 651 | */ |
||
| 652 | protected function constFetch(Node\Expr\ConstFetch $expr) |
||
| 673 | } |
||
| 674 |
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.