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 | 373 | public function __construct(Context $context, EventManager $eventManager) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param $expr |
||
| 43 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 44 | */ |
||
| 45 | 362 | protected function factory($expr) |
|
| 46 | { |
||
| 47 | 362 | switch (get_class($expr)) { |
|
| 48 | /** |
||
| 49 | * Call(s) |
||
| 50 | */ |
||
| 51 | 362 | case Node\Expr\MethodCall::class: |
|
| 52 | return new Expression\MethodCall(); |
||
| 53 | 362 | case Node\Expr\FuncCall::class: |
|
| 54 | 5 | return new Expression\FunctionCall(); |
|
| 55 | 358 | case Node\Expr\StaticCall::class: |
|
| 56 | return new Expression\StaticCall(); |
||
| 57 | /** |
||
| 58 | * Operators |
||
| 59 | */ |
||
| 60 | 358 | case Node\Expr\New_::class: |
|
| 61 | return new Expression\Operators\NewOp(); |
||
| 62 | 358 | case Node\Expr\Instanceof_::class: |
|
| 63 | return new Expression\Operators\InstanceOfOp(); |
||
| 64 | /** |
||
| 65 | * Assign |
||
| 66 | */ |
||
| 67 | 358 | case Node\Expr\AssignOp\Pow::class: |
|
| 68 | return new Expression\AssignOp\Pow(); |
||
| 69 | 358 | case Node\Expr\AssignOp\Plus::class: |
|
| 70 | return new Expression\AssignOp\Plus(); |
||
| 71 | 358 | case Node\Expr\AssignOp\Minus::class: |
|
| 72 | return new Expression\AssignOp\Minus(); |
||
| 73 | 358 | case Node\Expr\AssignOp\Mod::class: |
|
| 74 | return new Expression\AssignOp\Mod(); |
||
| 75 | 358 | case Node\Expr\AssignOp\BitwiseOr::class: |
|
| 76 | return new Expression\AssignOp\BitwiseOr(); |
||
| 77 | 358 | case Node\Expr\AssignOp\BitwiseAnd::class: |
|
| 78 | return new Expression\AssignOp\BitwiseAnd(); |
||
| 79 | /** |
||
| 80 | * BinaryOp |
||
| 81 | */ |
||
| 82 | 358 | case Node\Expr\BinaryOp\Identical::class: |
|
| 83 | 28 | return new Expression\BinaryOp\Identical(); |
|
| 84 | 330 | case Node\Expr\BinaryOp\Concat::class: |
|
| 85 | 1 | return new Expression\Operators\Contact(); |
|
| 86 | 329 | case Node\Expr\BinaryOp\NotIdentical::class: |
|
| 87 | 14 | return new Expression\BinaryOp\NotIdentical(); |
|
| 88 | 315 | case Node\Expr\BinaryOp\Equal::class: |
|
| 89 | 34 | return new Expression\BinaryOp\Equal(); |
|
| 90 | 281 | case Node\Expr\BinaryOp\NotEqual::class: |
|
| 91 | 17 | return new Expression\BinaryOp\NotEqual(); |
|
| 92 | /** |
||
| 93 | * @link http://php.net/manual/en/language.operators.increment.php |
||
| 94 | */ |
||
| 95 | 264 | case Node\Expr\PostInc::class: |
|
| 96 | 4 | return new Expression\Operators\PostInc(); |
|
| 97 | 260 | case Node\Expr\PostDec::class: |
|
| 98 | 4 | return new Expression\Operators\PostDec(); |
|
| 99 | /** |
||
| 100 | * Arithmetical |
||
| 101 | */ |
||
| 102 | 256 | case Node\Expr\BinaryOp\Div::class: |
|
| 103 | 37 | return new Expression\Operators\Arithmetical\Div(); |
|
| 104 | 219 | case Node\Expr\BinaryOp\Plus::class: |
|
| 105 | 45 | return new Expression\Operators\Arithmetical\Plus(); |
|
| 106 | 174 | case Node\Expr\BinaryOp\Minus::class: |
|
| 107 | 18 | return new Expression\Operators\Arithmetical\Minus(); |
|
| 108 | 156 | case Node\Expr\BinaryOp\Mul::class: |
|
| 109 | 35 | return new Expression\Operators\Arithmetical\Mul(); |
|
| 110 | 121 | case Node\Expr\BinaryOp\Mod::class: |
|
| 111 | 35 | return new Expression\Operators\Arithmetical\Mod(); |
|
| 112 | /** |
||
| 113 | * Bitwise |
||
| 114 | * @link http://php.net/manual/ru/language.operators.bitwise.php |
||
| 115 | */ |
||
| 116 | 86 | case Node\Expr\BinaryOp\BitwiseOr::class: |
|
| 117 | return new Expression\Operators\Bitwise\BitwiseOr(); |
||
| 118 | 86 | case Node\Expr\BinaryOp\BitwiseXor::class: |
|
| 119 | return new Expression\Operators\Bitwise\BitwiseXor(); |
||
| 120 | 86 | case Node\Expr\BinaryOp\BitwiseAnd::class: |
|
| 121 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
||
| 122 | 86 | case Node\Expr\BinaryOp\ShiftRight::class: |
|
| 123 | return new Expression\Operators\Bitwise\ShiftRight(); |
||
| 124 | 86 | case Node\Expr\BinaryOp\ShiftLeft::class: |
|
| 125 | return new Expression\Operators\Bitwise\ShiftLeft(); |
||
| 126 | 86 | case Node\Expr\BitwiseNot::class: |
|
| 127 | return new Expression\Operators\Bitwise\BitwiseNot(); |
||
| 128 | /** |
||
| 129 | * Logical |
||
| 130 | */ |
||
| 131 | 86 | case Node\Expr\BinaryOp\BooleanOr::class: |
|
| 132 | 10 | return new Expression\Operators\Logical\BooleanOr(); |
|
| 133 | 76 | case Node\Expr\BinaryOp\BooleanAnd::class: |
|
| 134 | 5 | return new Expression\Operators\Logical\BooleanAnd(); |
|
| 135 | 71 | case Node\Expr\BooleanNot::class: |
|
| 136 | 5 | return new Expression\Operators\Logical\BooleanNot(); |
|
| 137 | /** |
||
| 138 | * Comparison |
||
| 139 | */ |
||
| 140 | 66 | case Node\Expr\BinaryOp\Greater::class: |
|
| 141 | 12 | return new Expression\Operators\Comparison\Greater(); |
|
| 142 | 54 | case Node\Expr\BinaryOp\GreaterOrEqual::class: |
|
| 143 | 12 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
|
| 144 | 42 | case Node\Expr\BinaryOp\Smaller::class: |
|
| 145 | 12 | return new Expression\Operators\Comparison\Smaller(); |
|
| 146 | 30 | case Node\Expr\BinaryOp\SmallerOrEqual::class: |
|
| 147 | 12 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
|
| 148 | /** |
||
| 149 | * Another |
||
| 150 | */ |
||
| 151 | 18 | case Node\Expr\UnaryMinus::class: |
|
| 152 | 9 | return new Expression\Operators\UnaryMinus(); |
|
| 153 | 9 | case Node\Expr\UnaryPlus::class: |
|
| 154 | 9 | return new Expression\Operators\UnaryPlus(); |
|
| 155 | } |
||
| 156 | |||
| 157 | return false; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param object|string $expr |
||
| 162 | * @return CompiledExpression |
||
| 163 | */ |
||
| 164 | 373 | public function compile($expr) |
|
| 165 | { |
||
| 166 | 373 | if (is_string($expr)) { |
|
| 167 | return new CompiledExpression(CompiledExpression::STRING, $expr); |
||
| 168 | } |
||
| 169 | |||
| 170 | 373 | if (is_null($expr)) { |
|
| 171 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 172 | } |
||
| 173 | |||
| 174 | 373 | if (!is_object($expr)) { |
|
| 175 | throw new InvalidArgumentException('$expr must be string/object/null'); |
||
| 176 | } |
||
| 177 | |||
| 178 | 373 | $this->eventManager->fire( |
|
| 179 | 373 | ExpressionBeforeCompile::EVENT_NAME, |
|
| 180 | 373 | new ExpressionBeforeCompile( |
|
| 181 | 373 | $expr, |
|
| 182 | 373 | $this->context |
|
| 183 | 373 | ) |
|
| 184 | 373 | ); |
|
| 185 | |||
| 186 | 373 | $className = get_class($expr); |
|
| 187 | switch ($className) { |
||
| 188 | 373 | case Node\Arg::class: |
|
| 189 | /** |
||
| 190 | * @todo Better compile |
||
| 191 | */ |
||
| 192 | return $this->compile($expr->value); |
||
| 193 | 373 | case Node\Expr\PropertyFetch::class: |
|
| 194 | return $this->passPropertyFetch($expr); |
||
| 195 | 373 | case Node\Stmt\Property::class: |
|
| 196 | return $this->passProperty($expr); |
||
| 197 | 373 | case Node\Expr\ClassConstFetch::class: |
|
| 198 | return $this->passConstFetch($expr); |
||
| 199 | 373 | case Node\Expr\Assign::class: |
|
| 200 | 5 | return $this->passSymbol($expr); |
|
| 201 | 373 | case Node\Expr\AssignRef::class: |
|
| 202 | 1 | return $this->passSymbolByRef($expr); |
|
| 203 | 373 | case Node\Expr\Variable::class: |
|
| 204 | 3 | return $this->passExprVariable($expr); |
|
| 205 | /** |
||
| 206 | * Cast operators |
||
| 207 | */ |
||
| 208 | 373 | case Node\Expr\Cast\Bool_::class: |
|
| 209 | return $this->passCastBoolean($expr); |
||
| 210 | 373 | case Node\Expr\Cast\Int_::class: |
|
| 211 | return $this->passCastInt($expr); |
||
| 212 | 373 | case Node\Expr\Cast\Double::class: |
|
| 213 | return $this->passCastFloat($expr); |
||
| 214 | 373 | case Node\Expr\Cast\String_::class: |
|
| 215 | return $this->passCastString($expr); |
||
| 216 | 373 | case Node\Expr\Cast\Unset_::class: |
|
| 217 | return $this->passCastUnset($expr); |
||
| 218 | /** |
||
| 219 | * Expressions |
||
| 220 | */ |
||
| 221 | 373 | case Node\Expr\Array_::class: |
|
| 222 | 13 | return $this->getArray($expr); |
|
| 223 | 372 | case Node\Expr\ConstFetch::class: |
|
| 224 | 3 | return $this->constFetch($expr); |
|
| 225 | 372 | case Node\Name::class: |
|
| 226 | 5 | return $this->getNodeName($expr); |
|
| 227 | 372 | case Node\Name\FullyQualified::class: |
|
| 228 | return $this->getFullyQualifiedNodeName($expr); |
||
| 229 | /** |
||
| 230 | * Simple Scalar(s) |
||
| 231 | */ |
||
| 232 | 372 | case \PHPSA\Node\Scalar\Nil::class: |
|
| 233 | 8 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 234 | 371 | case Node\Scalar\LNumber::class: |
|
| 235 | 266 | return new CompiledExpression(CompiledExpression::INTEGER, $expr->value); |
|
| 236 | 367 | case Node\Scalar\DNumber::class: |
|
| 237 | 131 | return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value); |
|
| 238 | 366 | case Node\Scalar\String_::class: |
|
| 239 | 10 | return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
|
| 240 | 364 | case \PHPSA\Node\Scalar\Boolean::class: |
|
| 241 | 60 | return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
|
| 242 | 362 | case \PHPSA\Node\Scalar\Fake::class: |
|
| 243 | 29 | return new CompiledExpression($expr->type, $expr->value); |
|
| 244 | } |
||
| 245 | |||
| 246 | 362 | $expressionCompiler = $this->factory($expr); |
|
| 247 | 362 | if (!$expressionCompiler) { |
|
| 248 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
||
| 249 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
||
| 250 | } |
||
| 251 | |||
| 252 | 362 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 253 | 362 | if (!$result instanceof CompiledExpression) { |
|
| 254 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 255 | } |
||
| 256 | |||
| 257 | 362 | return $result; |
|
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @todo Implement |
||
| 262 | * |
||
| 263 | * @param Node\Stmt\Property $st |
||
| 264 | * @return CompiledExpression |
||
| 265 | */ |
||
| 266 | public function passProperty(Node\Stmt\Property $st) |
||
| 267 | { |
||
| 268 | $docBlock = $st->getDocComment(); |
||
| 269 | if (!$docBlock) { |
||
| 270 | $this->context->notice( |
||
| 271 | 'missing-docblock', |
||
| 272 | 'Missing docblock for %s() property', |
||
| 273 | $st |
||
| 274 | ); |
||
| 275 | |||
| 276 | return new CompiledExpression(); |
||
| 277 | } |
||
| 278 | |||
| 279 | $phpdoc = new \phpDocumentor\Reflection\DocBlock($docBlock->getText()); |
||
| 280 | |||
| 281 | $varTags = $phpdoc->getTagsByName('var'); |
||
| 282 | if ($varTags) { |
||
| 283 | /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ |
||
| 284 | $varTag = current($varTags); |
||
| 285 | |||
| 286 | $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
||
| 287 | |||
| 288 | try { |
||
| 289 | $type = $typeResolver->resolve($varTag->getType()); |
||
| 290 | } catch (\InvalidArgumentException $e) { |
||
| 291 | return new CompiledExpression(); |
||
| 292 | } |
||
| 293 | |||
| 294 | if ($type) { |
||
| 295 | switch (get_class($type)) { |
||
| 296 | case \phpDocumentor\Reflection\Types\Object_::class: |
||
| 297 | return new CompiledExpression( |
||
| 298 | CompiledExpression::OBJECT |
||
| 299 | ); |
||
| 300 | case \phpDocumentor\Reflection\Types\Integer::class: |
||
| 301 | return new CompiledExpression( |
||
| 302 | CompiledExpression::INTEGER |
||
| 303 | ); |
||
| 304 | case \phpDocumentor\Reflection\Types\String_::class: |
||
| 305 | return new CompiledExpression( |
||
| 306 | CompiledExpression::STRING |
||
| 307 | ); |
||
| 308 | case \phpDocumentor\Reflection\Types\Float_::class: |
||
| 309 | return new CompiledExpression( |
||
| 310 | CompiledExpression::DOUBLE |
||
| 311 | ); |
||
| 312 | case \phpDocumentor\Reflection\Types\Null_::class: |
||
| 313 | return new CompiledExpression( |
||
| 314 | CompiledExpression::NULL |
||
| 315 | ); |
||
| 316 | case \phpDocumentor\Reflection\Types\Boolean::class: |
||
| 317 | return new CompiledExpression( |
||
| 318 | CompiledExpression::BOOLEAN |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | return new CompiledExpression(); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param Node\Expr\Variable $expr |
||
| 329 | * @return CompiledExpression |
||
| 330 | */ |
||
| 331 | public function declareVariable(Node\Expr\Variable $expr) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Node\Name\FullyQualified $expr |
||
| 344 | * @return CompiledExpression |
||
| 345 | */ |
||
| 346 | public function getFullyQualifiedNodeName(Node\Name\FullyQualified $expr) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param Node\Name $expr |
||
| 355 | * @return CompiledExpression |
||
| 356 | */ |
||
| 357 | 5 | public function getNodeName(Node\Name $expr) |
|
| 358 | { |
||
| 359 | 5 | if ($expr->toString() === 'null') { |
|
| 360 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 361 | } |
||
| 362 | |||
| 363 | 5 | if (in_array($expr, ['parent'], true)) { |
|
| 364 | /** @var ClassDefinition $scope */ |
||
| 365 | $scope = $this->context->scope; |
||
| 366 | assert($scope instanceof ClassDefinition); |
||
| 367 | |||
| 368 | if ($scope->getExtendsClass()) { |
||
| 369 | $definition = $scope->getExtendsClassDefinition(); |
||
| 370 | if ($definition) { |
||
| 371 | return new CompiledExpression(CompiledExpression::OBJECT, $definition); |
||
| 372 | } |
||
| 373 | } else { |
||
| 374 | $this->context->notice( |
||
| 375 | 'no-parent', |
||
| 376 | 'Cannot access parent:: when current class scope has no parent', |
||
| 377 | $expr |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | 5 | if (in_array($expr, ['self', 'static'], true)) { |
|
| 383 | return CompiledExpression::fromZvalValue($this->context->scope); |
||
| 384 | } |
||
| 385 | |||
| 386 | 5 | if (defined($expr)) { |
|
| 387 | return CompiledExpression::fromZvalValue(constant($expr)); |
||
| 388 | } |
||
| 389 | |||
| 390 | 5 | return new CompiledExpression(CompiledExpression::STRING, $expr->toString()); |
|
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * (bool) {$expr} |
||
| 395 | * |
||
| 396 | * @param Node\Expr\Cast\Bool_ $expr |
||
| 397 | * @return CompiledExpression |
||
| 398 | */ |
||
| 399 | protected function passCastBoolean(Node\Expr\Cast\Bool_ $expr) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * (int) {$expr} |
||
| 419 | * |
||
| 420 | * @param Node\Expr\Cast\Int_ $expr |
||
| 421 | * @return CompiledExpression |
||
| 422 | */ |
||
| 423 | protected function passCastInt(Node\Expr\Cast\Int_ $expr) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * (float) {$expr} |
||
| 443 | * |
||
| 444 | * @param Node\Expr\Cast\Double $expr |
||
| 445 | * @return CompiledExpression |
||
| 446 | */ |
||
| 447 | protected function passCastFloat(Node\Expr\Cast\Double $expr) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * (string) {$expr} |
||
| 467 | * |
||
| 468 | * @param Node\Expr\Cast\String_ $expr |
||
| 469 | * @return CompiledExpression |
||
| 470 | */ |
||
| 471 | protected function passCastString(Node\Expr\Cast\String_ $expr) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * (unset) {$expr} |
||
| 491 | * |
||
| 492 | * @param Node\Expr\Cast\Unset_ $expr |
||
| 493 | * @return CompiledExpression |
||
| 494 | */ |
||
| 495 | protected function passCastUnset(Node\Expr\Cast\Unset_ $expr) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param Node\Expr\PropertyFetch $expr |
||
| 510 | * @return CompiledExpression |
||
| 511 | */ |
||
| 512 | protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param Node\Expr\ClassConstFetch $expr |
||
| 556 | * @return CompiledExpression |
||
| 557 | */ |
||
| 558 | protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param Node\Expr\Assign $expr |
||
| 583 | * @return CompiledExpression |
||
| 584 | */ |
||
| 585 | 5 | protected function passSymbol(Node\Expr\Assign $expr) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * @param Node\Expr\AssignRef $expr |
||
| 674 | * @return CompiledExpression |
||
| 675 | */ |
||
| 676 | 1 | protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
|
| 715 | |||
| 716 | /** |
||
| 717 | * @param Node\Expr\Variable $expr |
||
| 718 | * @return CompiledExpression |
||
| 719 | */ |
||
| 720 | 3 | protected function passExprVariable(Node\Expr\Variable $expr) |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Compile Array_ expression to CompiledExpression |
||
| 739 | * |
||
| 740 | * @param Node\Expr\Array_ $expr |
||
| 741 | * @return CompiledExpression |
||
| 742 | */ |
||
| 743 | 13 | protected function getArray(Node\Expr\Array_ $expr) |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Convert const fetch expr to CompiledExpression |
||
| 778 | * |
||
| 779 | * @param Node\Expr\ConstFetch $expr |
||
| 780 | * @return CompiledExpression |
||
| 781 | */ |
||
| 782 | 3 | protected function constFetch(Node\Expr\ConstFetch $expr) |
|
| 799 | } |
||
| 800 |
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.