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 array $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) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param \PhpParser\Node\Expr $expr |
||
| 127 | * @return Node\AbstractNode |
||
| 128 | */ |
||
| 129 | 4 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 168 | * @param Block $block |
||
| 169 | * @return Block |
||
| 170 | */ |
||
| 171 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 205 | * @param Block $block |
||
| 206 | * @return Block |
||
| 207 | */ |
||
| 208 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 225 | * @param Block $block |
||
| 226 | * @return Block |
||
| 227 | */ |
||
| 228 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 249 | * @param Block $block |
||
| 250 | * @return Block |
||
| 251 | */ |
||
| 252 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 277 | * @param Block $block |
||
| 278 | */ |
||
| 279 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 286 | * @param Block $block |
||
| 287 | */ |
||
| 288 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 295 | * @param Block $block |
||
| 296 | */ |
||
| 297 | 4 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 316 | * @param Block $block |
||
| 317 | * @return Block |
||
| 318 | */ |
||
| 319 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return Block |
||
| 339 | */ |
||
| 340 | public function getRoot() |
||
| 344 | } |
||
| 345 |
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.