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 | 377 | public function __construct(Context $context, EventManager $eventManager) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param $expr |
||
| 43 | * @return ExpressionCompilerInterface|AbstractExpressionCompiler |
||
| 44 | */ |
||
| 45 | 365 | protected function factory($expr) |
|
| 46 | { |
||
| 47 | 365 | switch (get_class($expr)) { |
|
| 48 | /** |
||
| 49 | * Call(s) |
||
| 50 | */ |
||
| 51 | 365 | case Node\Expr\MethodCall::class: |
|
| 52 | return new Expression\MethodCall(); |
||
| 53 | 365 | case Node\Expr\FuncCall::class: |
|
| 54 | 8 | return new Expression\FunctionCall(); |
|
| 55 | 359 | case Node\Expr\StaticCall::class: |
|
| 56 | return new Expression\StaticCall(); |
||
| 57 | /** |
||
| 58 | * Operators |
||
| 59 | */ |
||
| 60 | 359 | case Node\Expr\New_::class: |
|
| 61 | return new Expression\Operators\NewOp(); |
||
| 62 | 359 | case Node\Expr\Instanceof_::class: |
|
| 63 | return new Expression\Operators\InstanceOfOp(); |
||
| 64 | /** |
||
| 65 | * Assign |
||
| 66 | */ |
||
| 67 | 359 | case Node\Expr\AssignOp\Pow::class: |
|
| 68 | return new Expression\AssignOp\Pow(); |
||
| 69 | 359 | case Node\Expr\AssignOp\Plus::class: |
|
| 70 | return new Expression\AssignOp\Plus(); |
||
| 71 | 359 | case Node\Expr\AssignOp\Minus::class: |
|
| 72 | return new Expression\AssignOp\Minus(); |
||
| 73 | 359 | case Node\Expr\AssignOp\Mod::class: |
|
| 74 | return new Expression\AssignOp\Mod(); |
||
| 75 | 359 | case Node\Expr\AssignOp\BitwiseOr::class: |
|
| 76 | return new Expression\AssignOp\BitwiseOr(); |
||
| 77 | 359 | case Node\Expr\AssignOp\BitwiseAnd::class: |
|
| 78 | return new Expression\AssignOp\BitwiseAnd(); |
||
| 79 | /** |
||
| 80 | * BinaryOp |
||
| 81 | */ |
||
| 82 | 359 | case Node\Expr\BinaryOp\Identical::class: |
|
| 83 | 28 | return new Expression\BinaryOp\Identical(); |
|
| 84 | 331 | case Node\Expr\BinaryOp\Concat::class: |
|
| 85 | 1 | return new Expression\Operators\Concat(); |
|
| 86 | 330 | case Node\Expr\BinaryOp\NotIdentical::class: |
|
| 87 | 14 | return new Expression\BinaryOp\NotIdentical(); |
|
| 88 | 316 | case Node\Expr\BinaryOp\Equal::class: |
|
| 89 | 34 | return new Expression\BinaryOp\Equal(); |
|
| 90 | 282 | 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 | 265 | case Node\Expr\PostInc::class: |
|
| 96 | 4 | return new Expression\Operators\PostInc(); |
|
| 97 | 261 | case Node\Expr\PostDec::class: |
|
| 98 | 4 | return new Expression\Operators\PostDec(); |
|
| 99 | /** |
||
| 100 | * Arithmetical |
||
| 101 | */ |
||
| 102 | 257 | case Node\Expr\BinaryOp\Div::class: |
|
| 103 | 37 | return new Expression\Operators\Arithmetical\Div(); |
|
| 104 | 220 | case Node\Expr\BinaryOp\Plus::class: |
|
| 105 | 45 | return new Expression\Operators\Arithmetical\Plus(); |
|
| 106 | 175 | case Node\Expr\BinaryOp\Minus::class: |
|
| 107 | 18 | return new Expression\Operators\Arithmetical\Minus(); |
|
| 108 | 157 | case Node\Expr\BinaryOp\Mul::class: |
|
| 109 | 35 | return new Expression\Operators\Arithmetical\Mul(); |
|
| 110 | 122 | 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 | 87 | case Node\Expr\BinaryOp\BitwiseOr::class: |
|
| 117 | return new Expression\Operators\Bitwise\BitwiseOr(); |
||
| 118 | 87 | case Node\Expr\BinaryOp\BitwiseXor::class: |
|
| 119 | return new Expression\Operators\Bitwise\BitwiseXor(); |
||
| 120 | 87 | case Node\Expr\BinaryOp\BitwiseAnd::class: |
|
| 121 | return new Expression\Operators\Bitwise\BitwiseAnd(); |
||
| 122 | 87 | case Node\Expr\BinaryOp\ShiftRight::class: |
|
| 123 | return new Expression\Operators\Bitwise\ShiftRight(); |
||
| 124 | 87 | case Node\Expr\BinaryOp\ShiftLeft::class: |
|
| 125 | return new Expression\Operators\Bitwise\ShiftLeft(); |
||
| 126 | 87 | case Node\Expr\BitwiseNot::class: |
|
| 127 | return new Expression\Operators\Bitwise\BitwiseNot(); |
||
| 128 | /** |
||
| 129 | * Logical |
||
| 130 | */ |
||
| 131 | 87 | case Node\Expr\BinaryOp\BooleanOr::class: |
|
| 132 | 10 | return new Expression\Operators\Logical\BooleanOr(); |
|
| 133 | 77 | case Node\Expr\BinaryOp\BooleanAnd::class: |
|
| 134 | 5 | return new Expression\Operators\Logical\BooleanAnd(); |
|
| 135 | 72 | case Node\Expr\BooleanNot::class: |
|
| 136 | 5 | return new Expression\Operators\Logical\BooleanNot(); |
|
| 137 | /** |
||
| 138 | * Comparison |
||
| 139 | */ |
||
| 140 | 67 | case Node\Expr\BinaryOp\Greater::class: |
|
| 141 | 12 | return new Expression\Operators\Comparison\Greater(); |
|
| 142 | 55 | case Node\Expr\BinaryOp\GreaterOrEqual::class: |
|
| 143 | 12 | return new Expression\Operators\Comparison\GreaterOrEqual(); |
|
| 144 | 43 | case Node\Expr\BinaryOp\Smaller::class: |
|
| 145 | 12 | return new Expression\Operators\Comparison\Smaller(); |
|
| 146 | 31 | case Node\Expr\BinaryOp\SmallerOrEqual::class: |
|
| 147 | 12 | return new Expression\Operators\Comparison\SmallerOrEqual(); |
|
| 148 | /** |
||
| 149 | * Another |
||
| 150 | */ |
||
| 151 | 19 | case Node\Expr\Closure::class: |
|
| 152 | return new Expression\Closure(); |
||
| 153 | 19 | case Node\Expr\UnaryMinus::class: |
|
| 154 | 9 | return new Expression\Operators\UnaryMinus(); |
|
| 155 | 10 | case Node\Expr\UnaryPlus::class: |
|
| 156 | 9 | return new Expression\Operators\UnaryPlus(); |
|
| 157 | 1 | } |
|
| 158 | |||
| 159 | 1 | return false; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param object|string $expr |
||
| 164 | * @return CompiledExpression |
||
| 165 | */ |
||
| 166 | 377 | public function compile($expr) |
|
| 167 | { |
||
| 168 | 377 | if (is_string($expr)) { |
|
| 169 | return new CompiledExpression(CompiledExpression::STRING, $expr); |
||
| 170 | } |
||
| 171 | |||
| 172 | 377 | if (is_null($expr)) { |
|
| 173 | 1 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 174 | } |
||
| 175 | |||
| 176 | 377 | if (!is_object($expr)) { |
|
| 177 | throw new InvalidArgumentException('$expr must be string/object/null'); |
||
| 178 | } |
||
| 179 | |||
| 180 | 377 | $this->eventManager->fire( |
|
| 181 | 377 | ExpressionBeforeCompile::EVENT_NAME, |
|
| 182 | 377 | new ExpressionBeforeCompile( |
|
| 183 | 377 | $expr, |
|
| 184 | 377 | $this->context |
|
| 185 | 377 | ) |
|
| 186 | 377 | ); |
|
| 187 | |||
| 188 | 377 | $className = get_class($expr); |
|
| 189 | switch ($className) { |
||
| 190 | 377 | case Node\Arg::class: |
|
| 191 | /** |
||
| 192 | * @todo Better compile |
||
| 193 | */ |
||
| 194 | return $this->compile($expr->value); |
||
| 195 | 377 | case Node\Expr\PropertyFetch::class: |
|
| 196 | return $this->passPropertyFetch($expr); |
||
| 197 | 377 | case Node\Stmt\Property::class: |
|
| 198 | return $this->passProperty($expr); |
||
| 199 | 377 | case Node\Expr\ClassConstFetch::class: |
|
| 200 | return $this->passConstFetch($expr); |
||
| 201 | 377 | case Node\Expr\Assign::class: |
|
| 202 | 6 | return $this->passSymbol($expr); |
|
| 203 | 377 | case Node\Expr\AssignRef::class: |
|
| 204 | 1 | return $this->passSymbolByRef($expr); |
|
| 205 | 377 | case Node\Expr\Variable::class: |
|
| 206 | 4 | return $this->passExprVariable($expr); |
|
| 207 | /** |
||
| 208 | * Cast operators |
||
| 209 | */ |
||
| 210 | 377 | case Node\Expr\Cast\Bool_::class: |
|
| 211 | return $this->passCastBoolean($expr); |
||
| 212 | 377 | case Node\Expr\Cast\Int_::class: |
|
| 213 | return $this->passCastInt($expr); |
||
| 214 | 377 | case Node\Expr\Cast\Double::class: |
|
| 215 | return $this->passCastFloat($expr); |
||
| 216 | 377 | case Node\Expr\Cast\String_::class: |
|
| 217 | return $this->passCastString($expr); |
||
| 218 | 377 | case Node\Expr\Cast\Unset_::class: |
|
| 219 | return $this->passCastUnset($expr); |
||
| 220 | /** |
||
| 221 | * Expressions |
||
| 222 | */ |
||
| 223 | 377 | case Node\Expr\Array_::class: |
|
| 224 | 14 | return $this->getArray($expr); |
|
| 225 | 376 | case Node\Expr\ConstFetch::class: |
|
| 226 | 4 | return $this->constFetch($expr); |
|
| 227 | 376 | case Node\Name::class: |
|
| 228 | 8 | return $this->getNodeName($expr); |
|
| 229 | 376 | case Node\Name\FullyQualified::class: |
|
| 230 | return $this->getFullyQualifiedNodeName($expr); |
||
| 231 | /** |
||
| 232 | * Simple Scalar(s) |
||
| 233 | */ |
||
| 234 | 376 | case \PHPSA\Node\Scalar\Nil::class: |
|
| 235 | 8 | return new CompiledExpression(CompiledExpression::NULL); |
|
| 236 | 375 | case Node\Scalar\LNumber::class: |
|
| 237 | 268 | return new CompiledExpression(CompiledExpression::INTEGER, $expr->value); |
|
| 238 | 371 | case Node\Scalar\DNumber::class: |
|
| 239 | 131 | return new CompiledExpression(CompiledExpression::DOUBLE, $expr->value); |
|
| 240 | 370 | case Node\Scalar\String_::class: |
|
| 241 | 12 | return new CompiledExpression(CompiledExpression::STRING, $expr->value); |
|
| 242 | 367 | case \PHPSA\Node\Scalar\Boolean::class: |
|
| 243 | 60 | return new CompiledExpression(CompiledExpression::BOOLEAN, $expr->value); |
|
| 244 | 365 | case \PHPSA\Node\Scalar\Fake::class: |
|
| 245 | 29 | return new CompiledExpression($expr->type, $expr->value); |
|
| 246 | } |
||
| 247 | |||
| 248 | 365 | $expressionCompiler = $this->factory($expr); |
|
| 249 | 365 | if (!$expressionCompiler) { |
|
| 250 | 1 | $this->context->debug("Expression compiler is not implemented for {$className}"); |
|
| 251 | 1 | return new CompiledExpression(CompiledExpression::UNIMPLEMENTED); |
|
| 252 | } |
||
| 253 | |||
| 254 | 365 | $result = $expressionCompiler->pass($expr, $this->context); |
|
| 255 | 365 | if (!$result instanceof CompiledExpression) { |
|
| 256 | throw new RuntimeException('Please return CompiledExpression from ' . get_class($expressionCompiler)); |
||
| 257 | } |
||
| 258 | |||
| 259 | 365 | return $result; |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @todo Implement |
||
| 264 | * |
||
| 265 | * @param Node\Stmt\Property $st |
||
| 266 | * @return CompiledExpression |
||
| 267 | */ |
||
| 268 | public function passProperty(Node\Stmt\Property $st) |
||
| 269 | { |
||
| 270 | $docBlock = $st->getDocComment(); |
||
| 271 | if (!$docBlock) { |
||
| 272 | $this->context->notice( |
||
| 273 | 'missing-docblock', |
||
| 274 | 'Missing docblock for %s() property', |
||
| 275 | $st |
||
| 276 | ); |
||
| 277 | |||
| 278 | return new CompiledExpression(); |
||
| 279 | } |
||
| 280 | |||
| 281 | $phpdoc = new \phpDocumentor\Reflection\DocBlock($docBlock->getText()); |
||
| 282 | |||
| 283 | $varTags = $phpdoc->getTagsByName('var'); |
||
| 284 | if ($varTags) { |
||
| 285 | /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ |
||
| 286 | $varTag = current($varTags); |
||
| 287 | |||
| 288 | $typeResolver = new \phpDocumentor\Reflection\TypeResolver(); |
||
| 289 | |||
| 290 | try { |
||
| 291 | $type = $typeResolver->resolve($varTag->getType()); |
||
| 292 | } catch (\InvalidArgumentException $e) { |
||
| 293 | return new CompiledExpression(); |
||
| 294 | } |
||
| 295 | |||
| 296 | if ($type) { |
||
| 297 | switch (get_class($type)) { |
||
| 298 | case \phpDocumentor\Reflection\Types\Object_::class: |
||
| 299 | return new CompiledExpression( |
||
| 300 | CompiledExpression::OBJECT |
||
| 301 | ); |
||
| 302 | case \phpDocumentor\Reflection\Types\Integer::class: |
||
| 303 | return new CompiledExpression( |
||
| 304 | CompiledExpression::INTEGER |
||
| 305 | ); |
||
| 306 | case \phpDocumentor\Reflection\Types\String_::class: |
||
| 307 | return new CompiledExpression( |
||
| 308 | CompiledExpression::STRING |
||
| 309 | ); |
||
| 310 | case \phpDocumentor\Reflection\Types\Float_::class: |
||
| 311 | return new CompiledExpression( |
||
| 312 | CompiledExpression::DOUBLE |
||
| 313 | ); |
||
| 314 | case \phpDocumentor\Reflection\Types\Null_::class: |
||
| 315 | return new CompiledExpression( |
||
| 316 | CompiledExpression::NULL |
||
| 317 | ); |
||
| 318 | case \phpDocumentor\Reflection\Types\Boolean::class: |
||
| 319 | return new CompiledExpression( |
||
| 320 | CompiledExpression::BOOLEAN |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | return new CompiledExpression(); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param Node\Expr\Variable $expr |
||
| 331 | * @return CompiledExpression |
||
| 332 | */ |
||
| 333 | public function declareVariable(Node\Expr\Variable $expr) |
||
| 334 | { |
||
| 335 | $variable = $this->context->getSymbol($expr->name); |
||
| 336 | if (!$variable) { |
||
| 337 | $variable = new Variable($expr->name, null, CompiledExpression::UNKNOWN, $this->context->getCurrentBranch()); |
||
| 338 | $this->context->addVariable($variable); |
||
| 339 | } |
||
| 340 | |||
| 341 | return new CompiledExpression($variable->getType(), $variable->getValue(), $variable); |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param Node\Name\FullyQualified $expr |
||
| 346 | * @return CompiledExpression |
||
| 347 | */ |
||
| 348 | public function getFullyQualifiedNodeName(Node\Name\FullyQualified $expr) |
||
| 349 | { |
||
| 350 | $this->context->debug('Unimplemented FullyQualified', $expr); |
||
| 351 | |||
| 352 | return new CompiledExpression; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param Node\Name $expr |
||
| 357 | * @return CompiledExpression |
||
| 358 | */ |
||
| 359 | 8 | public function getNodeName(Node\Name $expr) |
|
| 360 | { |
||
| 361 | 8 | $nodeString = $expr->toString(); |
|
| 362 | 8 | if ($nodeString === 'null') { |
|
| 363 | return new CompiledExpression(CompiledExpression::NULL); |
||
| 364 | } |
||
| 365 | |||
| 366 | 8 | if (in_array($nodeString, ['parent'], true)) { |
|
| 367 | /** @var ClassDefinition $scope */ |
||
| 368 | $scope = $this->context->scope; |
||
| 369 | assert($scope instanceof ClassDefinition); |
||
| 370 | |||
| 371 | if ($scope->getExtendsClass()) { |
||
| 372 | $definition = $scope->getExtendsClassDefinition(); |
||
| 373 | if ($definition) { |
||
| 374 | return new CompiledExpression(CompiledExpression::OBJECT, $definition); |
||
| 375 | } |
||
| 376 | } else { |
||
| 377 | $this->context->notice( |
||
| 378 | 'no-parent', |
||
| 379 | 'Cannot access parent:: when current class scope has no parent', |
||
| 380 | $expr |
||
| 381 | ); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | 8 | if (in_array($nodeString, ['self', 'static'], true)) { |
|
| 386 | return CompiledExpression::fromZvalValue($this->context->scope); |
||
| 387 | } |
||
| 388 | |||
| 389 | 8 | if (defined($nodeString)) { |
|
| 390 | 1 | return CompiledExpression::fromZvalValue(constant($expr)); |
|
| 391 | } |
||
| 392 | |||
| 393 | 8 | return new CompiledExpression(CompiledExpression::STRING, $expr->toString()); |
|
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * (bool) {$expr} |
||
| 398 | * |
||
| 399 | * @param Node\Expr\Cast\Bool_ $expr |
||
| 400 | * @return CompiledExpression |
||
| 401 | */ |
||
| 402 | protected function passCastBoolean(Node\Expr\Cast\Bool_ $expr) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * (int) {$expr} |
||
| 422 | * |
||
| 423 | * @param Node\Expr\Cast\Int_ $expr |
||
| 424 | * @return CompiledExpression |
||
| 425 | */ |
||
| 426 | protected function passCastInt(Node\Expr\Cast\Int_ $expr) |
||
| 427 | { |
||
| 428 | $compiledExpression = $this->compile($expr->expr); |
||
| 429 | |||
| 430 | switch ($compiledExpression->getType()) { |
||
| 431 | case CompiledExpression::INTEGER: |
||
| 432 | $this->context->notice('stupid-cast', "You are trying to cast 'int' to 'int'", $expr); |
||
| 433 | return $compiledExpression; |
||
| 434 | case CompiledExpression::BOOLEAN: |
||
| 435 | case CompiledExpression::DOUBLE: |
||
| 436 | case CompiledExpression::NUMBER: |
||
| 437 | case CompiledExpression::STRING: |
||
| 438 | return new CompiledExpression(CompiledExpression::INTEGER, (int) $compiledExpression->getValue()); |
||
| 439 | } |
||
| 440 | |||
| 441 | return new CompiledExpression(); |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * (float) {$expr} |
||
| 446 | * |
||
| 447 | * @param Node\Expr\Cast\Double $expr |
||
| 448 | * @return CompiledExpression |
||
| 449 | */ |
||
| 450 | protected function passCastFloat(Node\Expr\Cast\Double $expr) |
||
| 451 | { |
||
| 452 | $compiledExpression = $this->compile($expr->expr); |
||
| 453 | |||
| 454 | switch ($compiledExpression->getType()) { |
||
| 455 | case CompiledExpression::DOUBLE: |
||
| 456 | $this->context->notice('stupid-cast', "You are trying to cast 'float' to 'float'", $expr); |
||
| 457 | return $compiledExpression; |
||
| 458 | case CompiledExpression::BOOLEAN: |
||
| 459 | case CompiledExpression::INTEGER: |
||
| 460 | case CompiledExpression::NUMBER: |
||
| 461 | case CompiledExpression::STRING: |
||
| 462 | return new CompiledExpression(CompiledExpression::DOUBLE, (float) $compiledExpression->getValue()); |
||
| 463 | } |
||
| 464 | |||
| 465 | return new CompiledExpression(); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * (string) {$expr} |
||
| 470 | * |
||
| 471 | * @param Node\Expr\Cast\String_ $expr |
||
| 472 | * @return CompiledExpression |
||
| 473 | */ |
||
| 474 | protected function passCastString(Node\Expr\Cast\String_ $expr) |
||
| 475 | { |
||
| 476 | $compiledExpression = $this->compile($expr->expr); |
||
| 477 | |||
| 478 | switch ($compiledExpression->getType()) { |
||
| 479 | case CompiledExpression::STRING: |
||
| 480 | $this->context->notice('stupid-cast', "You are trying to cast 'string' to 'string'", $expr); |
||
| 481 | return $compiledExpression; |
||
| 482 | case CompiledExpression::BOOLEAN: |
||
| 483 | case CompiledExpression::INTEGER: |
||
| 484 | case CompiledExpression::NUMBER: |
||
| 485 | case CompiledExpression::DOUBLE: |
||
| 486 | return new CompiledExpression(CompiledExpression::DOUBLE, (string) $compiledExpression->getValue()); |
||
| 487 | } |
||
| 488 | |||
| 489 | return new CompiledExpression(); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * (unset) {$expr} |
||
| 494 | * |
||
| 495 | * @param Node\Expr\Cast\Unset_ $expr |
||
| 496 | * @return CompiledExpression |
||
| 497 | */ |
||
| 498 | protected function passCastUnset(Node\Expr\Cast\Unset_ $expr) |
||
| 499 | { |
||
| 500 | $compiledExpression = $this->compile($expr->expr); |
||
| 501 | |||
| 502 | switch ($compiledExpression->getType()) { |
||
| 503 | case CompiledExpression::NULL: |
||
| 504 | $this->context->notice('stupid-cast', "You are trying to cast 'unset' to 'null'", $expr); |
||
| 505 | return $compiledExpression; |
||
| 506 | } |
||
| 507 | |||
| 508 | return new CompiledExpression(CompiledExpression::NULL, null); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param Node\Expr\PropertyFetch $expr |
||
| 513 | * @return CompiledExpression |
||
| 514 | */ |
||
| 515 | protected function passPropertyFetch(Node\Expr\PropertyFetch $expr) |
||
| 516 | { |
||
| 517 | $propertNameCE = $this->compile($expr->name); |
||
| 518 | |||
| 519 | $scopeExpression = $this->compile($expr->var); |
||
| 520 | if ($scopeExpression->isObject()) { |
||
| 521 | $scopeExpressionValue = $scopeExpression->getValue(); |
||
| 522 | if ($scopeExpressionValue instanceof ClassDefinition) { |
||
| 523 | $propertyName = $propertNameCE->isString() ? $propertNameCE->getValue() : false; |
||
| 524 | if ($propertyName) { |
||
| 525 | if ($scopeExpressionValue->hasProperty($propertyName, true)) { |
||
| 526 | $property = $scopeExpressionValue->getProperty($propertyName, true); |
||
| 527 | return $this->compile($property); |
||
| 528 | } else { |
||
| 529 | $this->context->notice( |
||
| 530 | 'undefined-property', |
||
| 531 | sprintf( |
||
| 532 | 'Property %s does not exist in %s scope', |
||
| 533 | $propertyName, |
||
| 534 | $scopeExpressionValue->getName() |
||
| 535 | ), |
||
| 536 | $expr |
||
| 537 | ); |
||
| 538 | } |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | return new CompiledExpression(CompiledExpression::UNKNOWN); |
||
| 543 | } elseif (!$scopeExpression->canBeObject()) { |
||
| 544 | return new CompiledExpression(CompiledExpression::UNKNOWN); |
||
| 545 | } |
||
| 546 | |||
| 547 | $this->context->notice( |
||
| 548 | 'property-fetch-on-non-object', |
||
| 549 | "It's not possible to fetch property on not object", |
||
| 550 | $expr, |
||
| 551 | Check::CHECK_BETA |
||
| 552 | ); |
||
| 553 | |||
| 554 | return new CompiledExpression(CompiledExpression::UNKNOWN); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param Node\Expr\ClassConstFetch $expr |
||
| 559 | * @return CompiledExpression |
||
| 560 | */ |
||
| 561 | protected function passConstFetch(Node\Expr\ClassConstFetch $expr) |
||
| 562 | { |
||
| 563 | $leftCE = $this->compile($expr->class); |
||
| 564 | if ($leftCE->isObject()) { |
||
| 565 | $leftCEValue = $leftCE->getValue(); |
||
| 566 | if ($leftCEValue instanceof ClassDefinition) { |
||
| 567 | if (!$leftCEValue->hasConst($expr->name)) { |
||
| 568 | $this->context->notice( |
||
| 569 | 'undefined-const', |
||
| 570 | sprintf('Constant %s does not exist in %s scope', $expr->name, $expr->class), |
||
| 571 | $expr |
||
| 572 | ); |
||
| 573 | return new CompiledExpression(CompiledExpression::UNKNOWN); |
||
| 574 | } |
||
| 575 | |||
| 576 | return new CompiledExpression(); |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | $this->context->debug('Unknown const fetch', $expr); |
||
| 581 | return new CompiledExpression(); |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param Node\Expr\Assign $expr |
||
| 586 | * @return CompiledExpression |
||
| 587 | */ |
||
| 588 | 6 | protected function passSymbol(Node\Expr\Assign $expr) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * @param Node\Expr\AssignRef $expr |
||
| 677 | * @return CompiledExpression |
||
| 678 | */ |
||
| 679 | 1 | protected function passSymbolByRef(Node\Expr\AssignRef $expr) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * @param Node\Expr\Variable $expr |
||
| 721 | * @return CompiledExpression |
||
| 722 | */ |
||
| 723 | 4 | protected function passExprVariable(Node\Expr\Variable $expr) |
|
| 724 | { |
||
| 725 | 4 | $variable = $this->context->getSymbol($expr->name); |
|
| 726 | 4 | if ($variable) { |
|
| 727 | 4 | $variable->incGets(); |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Compile Array_ expression to CompiledExpression |
||
| 742 | * |
||
| 743 | * @param Node\Expr\Array_ $expr |
||
| 744 | * @return CompiledExpression |
||
| 745 | */ |
||
| 746 | 14 | protected function getArray(Node\Expr\Array_ $expr) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Convert const fetch expr to CompiledExpression |
||
| 781 | * |
||
| 782 | * @param Node\Expr\ConstFetch $expr |
||
| 783 | * @return CompiledExpression |
||
| 784 | */ |
||
| 785 | 4 | protected function constFetch(Node\Expr\ConstFetch $expr) |
|
| 802 | } |
||
| 803 |
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.