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 | 881 | public function __construct(Context $context, EventManager $eventManager) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param $expr |
||
| 43 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 44 | */ |
||
| 45 | 869 | protected function factory($expr) |
|
| 46 | { |
||
| 47 | 869 | switch (get_class($expr)) { |
|
| 48 | /** |
||
| 49 | * Call(s) |
||
| 50 | */ |
||
| 51 | 869 | case Node\Expr\MethodCall::class: |
|
| 52 | 2 | return new Expression\MethodCall(); |
|
| 53 | 869 | case Node\Expr\FuncCall::class: |
|
| 54 | 13 | return new Expression\FunctionCall(); |
|
| 55 | 864 | case Node\Expr\StaticCall::class: |
|
| 56 | 1 | return new Expression\StaticCall(); |
|
| 57 | /** |
||
| 58 | * Operators |
||
| 59 | */ |
||
| 60 | 863 | case Node\Expr\New_::class: |
|
| 61 | 5 | return new Expression\Operators\NewOp(); |
|
| 62 | 863 | case Node\Expr\Instanceof_::class: |
|
| 63 | return new Expression\Operators\InstanceOfOp(); |
||
| 64 | /** |
||
| 65 | * AssignOp |
||
| 66 | */ |
||
| 67 | 863 | case Node\Expr\AssignOp\Pow::class: |
|
| 68 | 21 | return new Expression\AssignOp\Pow(); |
|
| 69 | 842 | case Node\Expr\AssignOp\Plus::class: |
|
| 70 | 17 | return new Expression\AssignOp\Plus(); |
|
| 71 | 825 | case Node\Expr\AssignOp\Minus::class: |
|
| 72 | 20 | return new Expression\AssignOp\Minus(); |
|
| 73 | 805 | case Node\Expr\AssignOp\Mul::class: |
|
| 74 | 20 | return new Expression\AssignOp\Mul(); |
|
| 75 | 785 | case Node\Expr\AssignOp\Div::class: |
|
| 76 | 20 | return new Expression\AssignOp\Div(); |
|
| 77 | 767 | case Node\Expr\AssignOp\Mod::class: |
|
| 78 | 13 | return new Expression\AssignOp\Mod(); |
|
| 79 | 756 | case Node\Expr\AssignOp\BitwiseOr::class: |
|
| 80 | 12 | return new Expression\AssignOp\BitwiseOr(); |
|
| 81 | 744 | case Node\Expr\AssignOp\BitwiseAnd::class: |
|
| 82 | 12 | return new Expression\AssignOp\BitwiseAnd(); |
|
| 83 | 732 | case Node\Expr\AssignOp\BitwiseXor::class: |
|
| 84 | 12 | return new Expression\AssignOp\BitwiseXor(); |
|
| 85 | 720 | case Node\Expr\AssignOp\Concat::class: |
|
| 86 | 14 | return new Expression\AssignOp\Concat(); |
|
| 87 | 706 | case Node\Expr\AssignOp\ShiftLeft::class: |
|
| 88 | 12 | return new Expression\AssignOp\ShiftLeft(); |
|
| 89 | 694 | case Node\Expr\AssignOp\ShiftRight::class: |
|
| 90 | 12 | return new Expression\AssignOp\ShiftRight(); |
|
| 91 | |||
| 92 | /** |
||
| 93 | * BinaryOp |
||
| 94 | */ |
||
| 95 | 682 | case Node\Expr\BinaryOp\Identical::class: |
|
| 96 | 30 | return new Expression\BinaryOp\Identical(); |
|
| 97 | 654 | case Node\Expr\BinaryOp\Concat::class: |
|
| 98 | 14 | return new Expression\Operators\Concat(); |
|
| 99 | 640 | case Node\Expr\BinaryOp\NotIdentical::class: |
|
| 100 | 16 | return new Expression\BinaryOp\NotIdentical(); |
|
| 101 | 626 | case Node\Expr\BinaryOp\Equal::class: |
|
| 102 | 46 | return new Expression\BinaryOp\Equal(); |
|
| 103 | 597 | case Node\Expr\BinaryOp\NotEqual::class: |
|
| 104 | 19 | return new Expression\BinaryOp\NotEqual(); |
|
| 105 | 580 | case Node\Expr\BinaryOp\Spaceship::class: |
|
| 106 | 11 | return new Expression\BinaryOp\SpaceShip(); |
|
| 107 | 571 | case Node\Expr\BinaryOp\Coalesce::class: |
|
| 108 | 3 | return new Expression\BinaryOp\Coalesce(); |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @link http://php.net/manual/en/language.operators.increment.php |
||
| 112 | */ |
||
| 113 | 569 | case Node\Expr\PostInc::class: |
|
| 114 | 11 | return new Expression\Operators\PostInc(); |
|
| 115 | 561 | case Node\Expr\PostDec::class: |
|
| 116 | 8 | return new Expression\Operators\PostDec(); |
|
| 117 | 553 | case Node\Expr\PreInc::class: |
|
| 118 | 10 | return new Expression\Operators\PreInc(); |
|
| 119 | 545 | case Node\Expr\PreDec::class: |
|
| 120 | 9 | return new Expression\Operators\PreDec(); |
|
| 121 | /** |
||
| 122 | * Arithmetical |
||
| 123 | */ |
||
| 124 | 537 | case Node\Expr\BinaryOp\Div::class: |
|
| 125 | 39 | return new Expression\Operators\Arithmetical\Div(); |
|
| 126 | 500 | case Node\Expr\BinaryOp\Plus::class: |
|
| 127 | 45 | return new Expression\Operators\Arithmetical\Plus(); |
|
| 128 | 459 | case Node\Expr\BinaryOp\Minus::class: |
|
| 129 | 24 | return new Expression\Operators\Arithmetical\Minus(); |
|
| 130 | 435 | case Node\Expr\BinaryOp\Mul::class: |
|
| 131 | 37 | return new Expression\Operators\Arithmetical\Mul(); |
|
| 132 | 400 | case Node\Expr\BinaryOp\Mod::class: |
|
| 133 | 38 | return new Expression\Operators\Arithmetical\Mod(); |
|
| 134 | 365 | case Node\Expr\BinaryOp\Pow::class: |
|
| 135 | 21 | return new Expression\Operators\Arithmetical\Pow(); |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Bitwise |
||
| 139 | * @link http://php.net/manual/ru/language.operators.bitwise.php |
||
| 140 | */ |
||
| 141 | 344 | case Node\Expr\BinaryOp\BitwiseOr::class: |
|
| 142 | 12 | return new Expression\Operators\Bitwise\BitwiseOr(); |
|
| 143 | 332 | case Node\Expr\BinaryOp\BitwiseXor::class: |
|
| 144 | 12 | return new Expression\Operators\Bitwise\BitwiseXor(); |
|
| 145 | 320 | case Node\Expr\BinaryOp\BitwiseAnd::class: |
|
| 146 | 12 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
|
| 147 | 308 | case Node\Expr\BinaryOp\ShiftRight::class: |
|
| 148 | 12 | return new Expression\Operators\Bitwise\ShiftRight(); |
|
| 149 | 296 | case Node\Expr\BinaryOp\ShiftLeft::class: |
|
| 150 | 12 | return new Expression\Operators\Bitwise\ShiftLeft(); |
|
| 151 | 284 | case Node\Expr\BitwiseNot::class: |
|
| 152 | 6 | return new Expression\Operators\Bitwise\BitwiseNot(); |
|
| 153 | /** |
||
| 154 | * Logical |
||
| 155 | */ |
||
| 156 | 279 | case Node\Expr\BinaryOp\BooleanOr::class: |
|
| 157 | 17 | return new Expression\Operators\Logical\BooleanOr(); |
|
| 158 | 264 | case Node\Expr\BinaryOp\BooleanAnd::class: |
|
| 159 | 16 | return new Expression\Operators\Logical\BooleanAnd(); |
|
| 160 | 251 | case Node\Expr\BooleanNot::class: |
|
| 161 | 12 | return new Expression\Operators\Logical\BooleanNot(); |
|
| 162 | 243 | case Node\Expr\BinaryOp\LogicalAnd::class: |
|
| 163 | 15 | return new Expression\Operators\Logical\LogicalAnd(); |
|
| 164 | 230 | case Node\Expr\BinaryOp\LogicalOr::class: |
|
| 165 | 17 | return new Expression\Operators\Logical\LogicalOr(); |
|
| 166 | 215 | case Node\Expr\BinaryOp\LogicalXor::class: |
|
| 167 | 17 | return new Expression\Operators\Logical\LogicalXor(); |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Comparison |
||
| 171 | */ |
||
| 172 | 200 | case Node\Expr\BinaryOp\Greater::class: |
|
| 173 | 14 | return new Expression\Operators\Comparison\Greater(); |
|
| 174 | 188 | case Node\Expr\BinaryOp\GreaterOrEqual::class: |
|
| 175 | 13 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
|
| 176 | 176 | case Node\Expr\BinaryOp\Smaller::class: |
|
| 177 | 15 | return new Expression\Operators\Comparison\Smaller(); |
|
| 178 | 164 | case Node\Expr\BinaryOp\SmallerOrEqual::class: |
|
| 179 | 13 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Casts |
||
| 183 | */ |
||
| 184 | 152 | case Node\Expr\Cast\Array_::class: |
|
| 185 | 3 | return new Expression\Casts\ArrayCast(); |
|
| 186 | 151 | case Node\Expr\Cast\Bool_::class: |
|
| 187 | 8 | return new Expression\Casts\BoolCast(); |
|
| 188 | 145 | case Node\Expr\Cast\Int_::class: |
|
| 189 | 8 | return new Expression\Casts\IntCast(); |
|
| 190 | 139 | case Node\Expr\Cast\Double::class: |
|
| 191 | 8 | return new Expression\Casts\DoubleCast(); |
|
| 192 | 133 | case Node\Expr\Cast\Object_::class: |
|
| 193 | 11 | return new Expression\Casts\ObjectCast(); |
|
| 194 | 123 | case Node\Expr\Cast\String_::class: |
|
| 195 | 7 | return new Expression\Casts\StringCast(); |
|
| 196 | 117 | case Node\Expr\Cast\Unset_::class: |
|
| 197 | 2 | return new Expression\Casts\UnsetCast(); |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Other |
||
| 202 | */ |
||
| 203 | 116 | case Node\Expr\Array_::class: |
|
| 204 | 43 | return new Expression\ArrayOp(); |
|
| 205 | 87 | case Node\Expr\Assign::class: |
|
| 206 | 43 | return new Expression\Assign(); |
|
| 207 | 67 | case Node\Expr\AssignRef::class: |
|
| 208 | 1 | return new Expression\AssignRef(); |
|
| 209 | 67 | case Node\Expr\Closure::class: |
|
| 210 | 1 | return new Expression\Closure(); |
|
| 211 | 67 | case Node\Expr\ConstFetch::class: |
|
| 212 | 15 | return new Expression\ConstFetch(); |
|
| 213 | 60 | case Node\Expr\ClassConstFetch::class: |
|
| 214 | return new Expression\ClassConstFetch(); |
||
| 215 | 60 | case Node\Expr\PropertyFetch::class: |
|
| 216 | return new Expression\PropertyFetch(); |
||
| 217 | 60 | case Node\Expr\StaticPropertyFetch::class: |
|
| 218 | return new Expression\StaticPropertyFetch(); |
||
| 219 | 60 | case Node\Expr\ArrayDimFetch::class: |
|
| 220 | return new Expression\ArrayDimFetch(); |
||
| 221 | 60 | case Node\Expr\UnaryMinus::class: |
|
| 222 | 10 | return new Expression\Operators\UnaryMinus(); |
|
| 223 | 51 | case Node\Expr\UnaryPlus::class: |
|
| 224 | 11 | return new Expression\Operators\UnaryPlus(); |
|
| 225 | 40 | case Node\Expr\Exit_::class: |
|
| 226 | 2 | return new Expression\ExitOp(); |
|
| 227 | 38 | case Node\Expr\Isset_::class: |
|
| 228 | return new Expression\IssetOp(); |
||
| 229 | 38 | case Node\Expr\Print_::class: |
|
| 230 | 2 | return new Expression\PrintOp(); |
|
| 231 | 37 | case Node\Expr\Empty_::class: |
|
| 232 | 5 | return new Expression\EmptyOp(); |
|
| 233 | 32 | case Node\Expr\Eval_::class: |
|
| 234 | 2 | return new Expression\EvalOp(); |
|
| 235 | 30 | case Node\Expr\ShellExec::class: |
|
| 236 | 1 | return new Expression\ShellExec(); |
|
| 237 | 29 | case Node\Expr\ErrorSuppress::class: |
|
| 238 | 2 | return new Expression\ErrorSuppress(); |
|
| 239 | 27 | case Node\Expr\Include_::class: |
|
| 240 | return new Expression\IncludeOp(); |
||
| 241 | 27 | case Node\Expr\Clone_::class: |
|
| 242 | 1 | return new Expression\CloneOp(); |
|
| 243 | 26 | case Node\Expr\Ternary::class: |
|
| 244 | 3 | return new Expression\Ternary(); |
|
| 245 | 24 | case Node\Expr\Yield_::class: |
|
| 246 | 1 | return new Expression\YieldOp(); |
|
| 247 | 24 | case Node\Expr\YieldFrom::class: |
|
| 248 | 1 | return new Expression\YieldFrom(); |
|
| 249 | 24 | case Node\Expr\Variable::class: |
|
| 250 | 24 | return new Expression\Variable(); |
|
| 251 | } |
||
| 252 | |||
| 253 | return false; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param object|string $expr |
||
| 258 | * @throws InvalidArgumentException when $expr is not string/object/null |
||
| 259 | * @throws RuntimeException when compiler class does not return a CompiledExpression |
||
| 260 | * @return CompiledExpression |
||
| 261 | */ |
||
| 262 | 881 | public function compile($expr) |
|
| 263 | { |
||
| 264 | 881 | if (is_string($expr)) { |
|
| 265 | 31 | return new CompiledExpression(CompiledExpression::STRING, $expr); |
|
| 266 | } |
||
| 267 | |||
| 268 | 881 | if (is_null($expr)) { |
|
| 269 | 12 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 270 | } |
||
| 271 | |||
| 272 | 881 | if (!is_object($expr)) { |
|
| 273 | throw new InvalidArgumentException('$expr must be string/object/null'); |
||
| 274 | } |
||
| 275 | |||
| 276 | 881 | if ($expr instanceof Node\Scalar) { |
|
| 277 | 819 | $scalar = new \PHPSA\Compiler\Scalar($this->context, $this->eventManager); |
|
| 278 | 819 | return $scalar->compile($expr); |
|
| 279 | } |
||
| 280 | |||
| 281 | 870 | $this->eventManager->fire( |
|
| 282 | 870 | ExpressionBeforeCompile::EVENT_NAME, |
|
| 283 | 870 | new ExpressionBeforeCompile( |
|
| 284 | 870 | $expr, |
|
| 285 | 870 | $this->context |
|
| 286 | 870 | ) |
|
| 287 | 870 | ); |
|
| 288 | |||
| 289 | 870 | $className = get_class($expr); |
|
| 290 | switch ($className) { |
||
| 291 | 870 | case Node\Arg::class: |
|
| 292 | /** |
||
| 293 | * @todo Better compile |
||
| 294 | */ |
||
| 295 | 2 | return $this->compile($expr->value); |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Names |
||
| 299 | */ |
||
| 300 | 870 | case Node\Name::class: |
|
| 301 | 31 | return $this->getNodeName($expr); |
|
| 302 | 869 | case Node\Name\FullyQualified::class: |
|
| 303 | 5 | return $this->getFullyQualifiedNodeName($expr); |
|
| 304 | } |
||
| 305 | |||
| 306 | 869 | $expressionCompiler = $this->factory($expr); |
|
| 307 | 869 | if (!$expressionCompiler) { |
|
| 308 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
||
| 309 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
||
| 310 | } |
||
| 311 | |||
| 312 | 869 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 313 | 869 | if (!$result instanceof CompiledExpression) { |
|
| 314 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 315 | } |
||
| 316 | |||
| 317 | 869 | return $result; |
|
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @todo Implement - does not belong in this file |
||
| 322 | * |
||
| 323 | * @param Node\Stmt\Property $st |
||
| 324 | public function passProperty(Node\Stmt\Property $st) |
||
| 325 | { |
||
| 326 | $docBlock = $st->getDocComment(); |
||
| 327 | if (!$docBlock) { |
||
| 328 | return new CompiledExpression(); |
||
| 329 | } |
||
| 330 | |||
| 331 | $phpdoc = DocBlockFactory::createInstance()->create($docBlock->getText()); |
||
| 332 | |||
| 333 | $varTags = $phpdoc->getTagsByName('var'); |
||
| 334 | if ($varTags) { |
||
| 335 | $varTag = current($varTags); |
||
| 336 | |||
| 337 | $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
||
| 338 | |||
| 339 | try { |
||
| 340 | $type = $typeResolver->resolve($varTag->getType()); |
||
| 341 | } catch (\InvalidArgumentException $e) { |
||
| 342 | return new CompiledExpression(); |
||
| 343 | } |
||
| 344 | |||
| 345 | if ($type) { |
||
| 346 | switch (get_class($type)) { |
||
| 347 | case \phpDocumentor\Reflection\Types\Object_::class: |
||
| 348 | return new CompiledExpression( |
||
| 349 | CompiledExpression::OBJECT |
||
| 350 | ); |
||
| 351 | case \phpDocumentor\Reflection\Types\Integer::class: |
||
| 352 | return new CompiledExpression( |
||
| 353 | CompiledExpression::INTEGER |
||
| 354 | ); |
||
| 355 | case \phpDocumentor\Reflection\Types\String_::class: |
||
| 356 | return new CompiledExpression( |
||
| 357 | CompiledExpression::STRING |
||
| 358 | ); |
||
| 359 | case \phpDocumentor\Reflection\Types\Float_::class: |
||
| 360 | return new CompiledExpression( |
||
| 361 | CompiledExpression::DOUBLE |
||
| 362 | ); |
||
| 363 | case \phpDocumentor\Reflection\Types\Null_::class: |
||
| 364 | return new CompiledExpression( |
||
| 365 | CompiledExpression::NULL |
||
| 366 | ); |
||
| 367 | case \phpDocumentor\Reflection\Types\Boolean::class: |
||
| 368 | return new CompiledExpression( |
||
| 369 | CompiledExpression::BOOLEAN |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | return new CompiledExpression(); |
||
| 376 | } |
||
| 377 | */ |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * @param Node\Expr\Variable $expr |
||
| 382 | * @param mixed $value |
||
| 383 | * @param int $type |
||
| 384 | * @return CompiledExpression |
||
| 385 | */ |
||
| 386 | 2 | public function declareVariable(Node\Expr\Variable $expr, $value = null, $type = CompiledExpression::UNKNOWN) |
|
| 387 | { |
||
| 388 | 2 | $variable = $this->context->getSymbol($expr->name); |
|
| 389 | 2 | if ($variable) { |
|
| 390 | 1 | $variable->modify($type, $value); |
|
| 391 | 1 | } else { |
|
| 392 | 2 | $variable = new Variable($expr->name, $value, $type, $this->context->getCurrentBranch()); |
|
| 393 | 2 | $this->context->addVariable($variable); |
|
| 394 | } |
||
| 395 | |||
| 396 | 2 | return new CompiledExpression($variable->getType(), $variable->getValue(), $variable); |
|
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param Node\Name\FullyQualified $expr |
||
| 401 | * @return CompiledExpression |
||
| 402 | */ |
||
| 403 | 5 | public function getFullyQualifiedNodeName(Node\Name\FullyQualified $expr) |
|
| 404 | { |
||
| 405 | 5 | $compiler = $this->context->getApplication()->compiler; |
|
| 406 | |||
| 407 | 5 | $classDefinition = $compiler->getClass($expr->toString()); |
|
| 408 | 5 | if ($classDefinition) { |
|
| 409 | return new CompiledExpression( |
||
| 410 | CompiledExpression::OBJECT, |
||
| 411 | $classDefinition |
||
| 412 | ); |
||
| 413 | } |
||
| 414 | |||
| 415 | 5 | $this->context->debug('Unimplemented FullyQualified', $expr); |
|
| 416 | 5 | return new CompiledExpression(); |
|
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param Node\Name $expr |
||
| 421 | * @return CompiledExpression |
||
| 422 | */ |
||
| 423 | 31 | public function getNodeName(Node\Name $expr) |
|
| 459 | } |
||
| 460 |
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.