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 |
||
| 13 | class ControlFlowGraph |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | protected $lastBlockId = 1; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Block |
||
| 22 | */ |
||
| 23 | protected $root; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Block[] |
||
| 27 | */ |
||
| 28 | protected $labels; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @todo |
||
| 32 | * |
||
| 33 | * @var \PhpParser\Node\Stmt\Goto_[] |
||
| 34 | */ |
||
| 35 | protected $unresolvedGotos; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Context |
||
| 39 | */ |
||
| 40 | protected $context; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param $statement |
||
| 44 | */ |
||
| 45 | 49 | public function __construct($statement, Context $context) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param \PhpParser\Node[] $nodes |
||
| 65 | * @param Block $block |
||
| 66 | */ |
||
| 67 | 48 | protected function passNodes(array $nodes, Block $block) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * If current block is not empty, lets create a new one |
||
| 124 | * |
||
| 125 | * @param Block $block |
||
| 126 | * @return Block |
||
| 127 | */ |
||
| 128 | 1 | protected function createNewBlockIfNeeded(Block $block) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param \PhpParser\Node\Expr $expr |
||
| 145 | * @return Node\AbstractNode |
||
| 146 | */ |
||
| 147 | 36 | protected function passExpr(\PhpParser\Node\Expr $expr) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param \PhpParser\Node\Stmt\If_ $if |
||
| 186 | * @param Block $block |
||
| 187 | * @return Block |
||
| 188 | */ |
||
| 189 | 6 | protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param \PhpParser\Node\Stmt\For_ $for |
||
| 223 | * @param Block $block |
||
| 224 | * @return Block |
||
| 225 | */ |
||
| 226 | 4 | protected function passFor(\PhpParser\Node\Stmt\For_ $for, Block $block) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param \PhpParser\Node\Stmt\Do_ $do |
||
| 243 | * @param Block $block |
||
| 244 | * @return Block |
||
| 245 | */ |
||
| 246 | 3 | protected function passDo(\PhpParser\Node\Stmt\Do_ $do, Block $block) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param \PhpParser\Node\Stmt\While_ $while |
||
| 267 | * @param Block $block |
||
| 268 | * @return Block |
||
| 269 | */ |
||
| 270 | 3 | protected function passWhile(\PhpParser\Node\Stmt\While_ $while, Block $block) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param \PhpParser\Node\Stmt\Throw_ $throw_ |
||
| 295 | * @param Block $block |
||
| 296 | */ |
||
| 297 | 1 | protected function passThrow(\PhpParser\Node\Stmt\Throw_ $throw_, Block $block) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param \PhpParser\Node\Expr\Assign $assign |
||
| 304 | * @param Block $block |
||
| 305 | */ |
||
| 306 | 25 | protected function passAssign(\PhpParser\Node\Expr\Assign $assign, Block $block) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param \PhpParser\Node\Stmt\Return_ $return |
||
| 313 | * @param Block $block |
||
| 314 | */ |
||
| 315 | 35 | protected function passReturn(\PhpParser\Node\Stmt\Return_ $return, Block $block) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 334 | * @param Block $block |
||
| 335 | * @return Block |
||
| 336 | */ |
||
| 337 | 2 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @return Block |
||
| 357 | */ |
||
| 358 | public function getRoot() |
||
| 362 | } |
||
| 363 |
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.