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) |
|
| 128 | { |
||
| 129 | 4 | switch (get_class($expr)) { |
|
| 130 | 4 | case \PhpParser\Node\Expr\BinaryOp\Equal::class: |
|
| 131 | return new Node\Expr\BinaryOp\Equal(); |
||
| 132 | |||
| 133 | 4 | case \PhpParser\Node\Expr\BinaryOp\Smaller::class: |
|
| 134 | return new Node\Expr\BinaryOp\Smaller(); |
||
| 135 | |||
| 136 | 4 | case \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual::class: |
|
| 137 | return new Node\Expr\BinaryOp\SmallerOrEqual(); |
||
| 138 | |||
| 139 | 4 | case \PhpParser\Node\Expr\BinaryOp\Greater::class: |
|
| 140 | return new Node\Expr\BinaryOp\Greater(); |
||
| 141 | |||
| 142 | 4 | case \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual::class: |
|
| 143 | return new Node\Expr\BinaryOp\GreaterOrEqual(); |
||
| 144 | |||
| 145 | 4 | case \PhpParser\Node\Expr\Instanceof_::class: |
|
| 146 | return new Node\Expr\InstanceOfExpr(); |
||
| 147 | |||
| 148 | 4 | default: |
|
| 149 | 4 | echo 'Unimplemented ' . get_class($expr) . PHP_EOL; |
|
| 150 | 4 | } |
|
| 151 | |||
| 152 | 4 | return new Node\UnknownNode(); |
|
| 153 | } |
||
| 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) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param \PhpParser\Node\Stmt\TryCatch $stmt |
||
| 305 | * @param Block $block |
||
| 306 | * @return Block |
||
| 307 | */ |
||
| 308 | protected function passTryCatch(\PhpParser\Node\Stmt\TryCatch $stmt, Block $block) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return Block |
||
| 328 | */ |
||
| 329 | public function getRoot() |
||
| 333 | } |
||
| 334 |
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.