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 array |
||
| 25 | */ |
||
| 26 | protected $labels; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $unresolvedGotos; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param $statement |
||
| 35 | */ |
||
| 36 | 6 | public function __construct($statement) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $nodes |
||
| 49 | * @param Block $block |
||
| 50 | */ |
||
| 51 | 5 | protected function passNodes(array $nodes, Block $block) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * If current block is not empty, lets create a new one |
||
| 108 | * |
||
| 109 | * @param Block $block |
||
| 110 | * @return Block |
||
| 111 | */ |
||
| 112 | protected function createNewBlockIfNeeded(Block $block) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param \PhpParser\Node\Expr $expr |
||
| 125 | * @return Node\AbstractNode |
||
| 126 | */ |
||
| 127 | 4 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 166 | * @param Block $block |
||
| 167 | * @return Block |
||
| 168 | */ |
||
| 169 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 203 | * @param Block $block |
||
| 204 | * @return Block |
||
| 205 | */ |
||
| 206 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 223 | * @param Block $block |
||
| 224 | * @return Block |
||
| 225 | */ |
||
| 226 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 247 | * @param Block $block |
||
| 248 | * @return Block |
||
| 249 | */ |
||
| 250 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 275 | * @param Block $block |
||
| 276 | */ |
||
| 277 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 284 | * @param Block $block |
||
| 285 | */ |
||
| 286 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 293 | * @param Block $block |
||
| 294 | */ |
||
| 295 | 4 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 314 | * @param Block $block |
||
| 315 | * @return Block |
||
| 316 | */ |
||
| 317 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return Block |
||
| 337 | */ |
||
| 338 | public function getRoot() |
||
| 342 | } |
||
| 343 |
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.