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 | protected function passExpr(\PhpParser\Node\Expr $expr) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 157 | * @param Block $block |
||
| 158 | * @return Block |
||
| 159 | */ |
||
| 160 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 194 | * @param Block $block |
||
| 195 | * @return Block |
||
| 196 | */ |
||
| 197 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 214 | * @param Block $block |
||
| 215 | * @return Block |
||
| 216 | */ |
||
| 217 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 238 | * @param Block $block |
||
| 239 | * @return Block |
||
| 240 | */ |
||
| 241 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 266 | * @param Block $block |
||
| 267 | */ |
||
| 268 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 275 | * @param Block $block |
||
| 276 | */ |
||
| 277 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param \PhpParser\Node\Stmt\Return_ $return_ |
||
| 284 | * @param Block $block |
||
| 285 | */ |
||
| 286 | 4 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return_, Block $block) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 293 | * @param Block $block |
||
| 294 | * @return Block |
||
| 295 | */ |
||
| 296 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return Block |
||
| 316 | */ |
||
| 317 | public function getRoot() |
||
| 321 | } |
||
| 322 |
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.