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 |
||
| 11 | class ControlFlowGraph |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | protected $lastBlockId = 1; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Block |
||
| 20 | */ |
||
| 21 | protected $root; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Block[] |
||
| 25 | */ |
||
| 26 | protected $labels; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @todo |
||
| 30 | * |
||
| 31 | * @var \PhpParser\Node\Stmt\Goto_[] |
||
| 32 | */ |
||
| 33 | protected $unresolvedGotos; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param $statement |
||
| 37 | */ |
||
| 38 | 6 | public function __construct($statement) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param \PhpParser\Node[] $nodes |
||
| 51 | * @param Block $block |
||
| 52 | */ |
||
| 53 | 5 | protected function passNodes(array $nodes, Block $block) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * If current block is not empty, lets create a new one |
||
| 110 | * |
||
| 111 | * @param Block $block |
||
| 112 | * @return Block |
||
| 113 | */ |
||
| 114 | protected function createNewBlockIfNeeded(Block $block) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param \PhpParser\Node\Expr $expr |
||
| 131 | * @return Node\AbstractNode |
||
| 132 | */ |
||
| 133 | 4 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 172 | * @param Block $block |
||
| 173 | * @return Block |
||
| 174 | */ |
||
| 175 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 209 | * @param Block $block |
||
| 210 | * @return Block |
||
| 211 | */ |
||
| 212 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 229 | * @param Block $block |
||
| 230 | * @return Block |
||
| 231 | */ |
||
| 232 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 253 | * @param Block $block |
||
| 254 | * @return Block |
||
| 255 | */ |
||
| 256 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 281 | * @param Block $block |
||
| 282 | */ |
||
| 283 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 290 | * @param Block $block |
||
| 291 | */ |
||
| 292 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 299 | * @param Block $block |
||
| 300 | */ |
||
| 301 | 4 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 320 | * @param Block $block |
||
| 321 | * @return Block |
||
| 322 | */ |
||
| 323 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return Block |
||
| 343 | */ |
||
| 344 | public function getRoot() |
||
| 348 | } |
||
| 349 |
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.