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 |
||
| 12 | class ControlFlowGraph |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $lastBlockId = 1; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Block |
||
| 21 | */ |
||
| 22 | protected $root; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Block[] |
||
| 26 | */ |
||
| 27 | protected $labels; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @todo |
||
| 31 | * |
||
| 32 | * @var \PhpParser\Node\Stmt\Goto_[] |
||
| 33 | */ |
||
| 34 | protected $unresolvedGotos; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Context |
||
| 38 | */ |
||
| 39 | protected $context; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param $statement |
||
| 43 | */ |
||
| 44 | 6 | public function __construct($statement, Context $context) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param \PhpParser\Node[] $nodes |
||
| 58 | * @param Block $block |
||
| 59 | */ |
||
| 60 | 5 | protected function passNodes(array $nodes, Block $block) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * If current block is not empty, lets create a new one |
||
| 117 | * |
||
| 118 | * @param Block $block |
||
| 119 | * @return Block |
||
| 120 | */ |
||
| 121 | protected function createNewBlockIfNeeded(Block $block) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param \PhpParser\Node\Expr $expr |
||
| 138 | * @return Node\AbstractNode |
||
| 139 | */ |
||
| 140 | 4 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 179 | * @param Block $block |
||
| 180 | * @return Block |
||
| 181 | */ |
||
| 182 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 216 | * @param Block $block |
||
| 217 | * @return Block |
||
| 218 | */ |
||
| 219 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 236 | * @param Block $block |
||
| 237 | * @return Block |
||
| 238 | */ |
||
| 239 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 260 | * @param Block $block |
||
| 261 | * @return Block |
||
| 262 | */ |
||
| 263 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 288 | * @param Block $block |
||
| 289 | */ |
||
| 290 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 297 | * @param Block $block |
||
| 298 | */ |
||
| 299 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 306 | * @param Block $block |
||
| 307 | */ |
||
| 308 | 4 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 327 | * @param Block $block |
||
| 328 | * @return Block |
||
| 329 | */ |
||
| 330 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return Block |
||
| 350 | */ |
||
| 351 | public function getRoot() |
||
| 355 | } |
||
| 356 |
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.