Complex classes like ControlFlowGraph 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 ControlFlowGraph, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ControlFlowGraph |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | protected $lastBlockId = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Block |
||
| 22 | */ |
||
| 23 | protected $root; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Block[] |
||
| 27 | */ |
||
| 28 | protected $labels; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @todo |
||
| 32 | * |
||
| 33 | * @var \PhpParser\Node\Stmt\Goto_[] |
||
| 34 | */ |
||
| 35 | protected $unresolvedGotos; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Context |
||
| 39 | */ |
||
| 40 | protected $context; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $statement |
||
| 44 | */ |
||
| 45 | 52 | public function __construct($statement, Context $context) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param \PhpParser\Node[] $nodes |
||
| 65 | * @param Block $block |
||
| 66 | */ |
||
| 67 | 51 | protected function passNodes(array $nodes, Block $block) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * If current block is not empty, lets create a new one |
||
| 127 | * |
||
| 128 | * @param Block $block |
||
| 129 | * @return Block |
||
| 130 | */ |
||
| 131 | 4 | protected function createNewBlockIfNeeded(Block $block) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param \PhpParser\Node\Expr $expr |
||
| 148 | * @return Node\AbstractNode |
||
| 149 | */ |
||
| 150 | 38 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 189 | * @param Block $block |
||
| 190 | * @return Block |
||
| 191 | */ |
||
| 192 | 6 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param \PhpParser\Node\Stmt\Foreach_ $foreach |
||
| 226 | * @param Block $block |
||
| 227 | * @return Block |
||
| 228 | */ |
||
| 229 | 2 | protected function passForeach(\PhpParser\Node\Stmt\Foreach_ $foreach, Block $block) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 252 | * @param Block $block |
||
| 253 | * @return Block |
||
| 254 | */ |
||
| 255 | 4 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 272 | * @param Block $block |
||
| 273 | * @return Block |
||
| 274 | */ |
||
| 275 | 3 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 296 | * @param Block $cond |
||
| 297 | * @return Block |
||
| 298 | */ |
||
| 299 | 3 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $cond) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 319 | * @param Block $block |
||
| 320 | */ |
||
| 321 | 1 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 328 | * @param Block $block |
||
| 329 | */ |
||
| 330 | 26 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 337 | * @param Block $block |
||
| 338 | */ |
||
| 339 | 37 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 363 | * @param Block $block |
||
| 364 | * @return Block |
||
| 365 | */ |
||
| 366 | 2 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @return Block |
||
| 386 | */ |
||
| 387 | public function getRoot() |
||
| 391 | } |
||
| 392 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.