Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Tree extends Object implements \ArrayAccess, \Countable, \Iterator |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $ref; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * the cursor position |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $position; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * the tree subject |
||
| 50 | * |
||
| 51 | * @var Object |
||
| 52 | */ |
||
| 53 | private $subject; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * tree children |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $children = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * tree path children |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $pathChildren = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * the blob of the actual tree |
||
| 71 | * |
||
| 72 | * @var \GitElephant\Objects\Object |
||
| 73 | */ |
||
| 74 | private $blob; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * static method to generate standalone log |
||
| 78 | * |
||
| 79 | * @param \GitElephant\Repository $repository repo |
||
| 80 | * @param array $outputLines output lines from command.log |
||
| 81 | * |
||
| 82 | * @return \GitElephant\Objects\Log |
||
| 83 | */ |
||
| 84 | public static function createFromOutputLines(Repository $repository, $outputLines) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * get the commit properties from command |
||
| 94 | * |
||
| 95 | * @see LsTreeCommand::tree |
||
| 96 | */ |
||
| 97 | 15 | private function createFromCommand() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Some path examples: |
||
| 106 | * empty string for root |
||
| 107 | * folder1/folder2 |
||
| 108 | * folder1/folder2/filename |
||
| 109 | * |
||
| 110 | * @param \GitElephant\Repository $repository the repository |
||
| 111 | * @param string $ref a treeish reference |
||
| 112 | * @param Object $subject the subject |
||
| 113 | * |
||
| 114 | * @throws \RuntimeException |
||
| 115 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 116 | * @internal param \GitElephant\Objects\Object|string $treeObject Object instance |
||
| 117 | */ |
||
| 118 | 15 | public function __construct(Repository $repository, $ref = 'HEAD', $subject = null) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * parse the output of a git command showing a ls-tree |
||
| 129 | * |
||
| 130 | * @param array $outputLines output lines |
||
| 131 | */ |
||
| 132 | 15 | private function parseOutputLines($outputLines) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @return \GitElephant\Command\Caller\Caller |
||
| 143 | */ |
||
| 144 | 15 | private function getCaller() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * get the current tree parent, null if root |
||
| 151 | * |
||
| 152 | * @return null|string |
||
| 153 | */ |
||
| 154 | public function getParent() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * tell if the tree created is the root of the repository |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 15 | public function isRoot() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * tell if the path given is a blob path |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | 15 | public function isBlob() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * the current tree path is a binary file |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isBinary() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * get binary data |
||
| 195 | * |
||
| 196 | * @throws \RuntimeException |
||
| 197 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 198 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 199 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getBinaryData() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Return an array like this |
||
| 211 | * 0 => array( |
||
| 212 | * 'path' => the path to the current element |
||
| 213 | * 'label' => the name of the current element |
||
| 214 | * ), |
||
| 215 | * 1 => array(), |
||
| 216 | * ... |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function getBreadcrumb() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * check if the path is equals to a fullPath |
||
| 244 | * to tell if it's a blob |
||
| 245 | * |
||
| 246 | * @param array $outputLines output lines |
||
| 247 | * |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 15 | private function scanPathsForBlob($outputLines) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Reorder children of the tree |
||
| 270 | * Tree first (alphabetically) and then blobs (alphabetically) |
||
| 271 | * |
||
| 272 | * @param \GitElephant\Objects\Object $a the first object |
||
| 273 | * @param \GitElephant\Objects\Object $b the second object |
||
| 274 | * |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | 7 | private function sortChildren(Object $a, Object $b) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Parse a single line into pieces |
||
| 291 | * |
||
| 292 | * @param string $line a single line output from the git binary |
||
| 293 | * |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | 15 | private function parseLine($line) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * get the last commit message for this tree |
||
| 341 | * |
||
| 342 | * @param string $ref |
||
| 343 | * |
||
| 344 | * @throws \RuntimeException |
||
| 345 | * @return Commit\Message |
||
| 346 | */ |
||
| 347 | public function getLastCommitMessage($ref = 'master') |
||
| 351 | |||
| 352 | /** |
||
| 353 | * get author of the last commit |
||
| 354 | * |
||
| 355 | * @param string $ref |
||
| 356 | * |
||
| 357 | * @throws \RuntimeException |
||
| 358 | * @return Author |
||
| 359 | */ |
||
| 360 | public function getLastCommitAuthor($ref = 'master') |
||
| 364 | |||
| 365 | /** |
||
| 366 | * get the last commit for a given treeish, for the actual tree |
||
| 367 | * |
||
| 368 | * @param string $ref |
||
| 369 | * |
||
| 370 | * @throws \RuntimeException |
||
| 371 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 372 | * @return Commit |
||
| 373 | */ |
||
| 374 | public function getLastCommit($ref = 'master') |
||
| 383 | |||
| 384 | /** |
||
| 385 | * get the tree object for this tree |
||
| 386 | * |
||
| 387 | * @return \GitElephant\Objects\Object |
||
| 388 | */ |
||
| 389 | 1 | public function getObject() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Blob getter |
||
| 400 | * |
||
| 401 | * @return \GitElephant\Objects\Object |
||
| 402 | */ |
||
| 403 | 5 | public function getBlob() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get Subject |
||
| 410 | * |
||
| 411 | * @return \GitElephant\Objects\Object |
||
| 412 | */ |
||
| 413 | 1 | public function getSubject() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get Ref |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | public function getRef() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * ArrayAccess interface |
||
| 430 | * |
||
| 431 | * @param int $offset offset |
||
| 432 | * |
||
| 433 | * @return bool |
||
| 434 | */ |
||
| 435 | public function offsetExists($offset) |
||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * ArrayAccess interface |
||
| 443 | * |
||
| 444 | * @param int $offset offset |
||
| 445 | * |
||
| 446 | * @return null |
||
| 447 | */ |
||
| 448 | 8 | public function offsetGet($offset) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * ArrayAccess interface |
||
| 455 | * |
||
| 456 | * @param int $offset offset |
||
| 457 | * @param mixed $value value |
||
| 458 | */ |
||
| 459 | public function offsetSet($offset, $value) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * ArrayAccess interface |
||
| 470 | * |
||
| 471 | * @param int $offset offset |
||
| 472 | */ |
||
| 473 | public function offsetUnset($offset) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Countable interface |
||
| 480 | * |
||
| 481 | * @return int|void |
||
| 482 | */ |
||
| 483 | 4 | public function count() |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Iterator interface |
||
| 490 | * |
||
| 491 | * @return mixed |
||
| 492 | */ |
||
| 493 | 1 | public function current() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Iterator interface |
||
| 500 | */ |
||
| 501 | 1 | public function next() |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Iterator interface |
||
| 508 | * |
||
| 509 | * @return int |
||
| 510 | */ |
||
| 511 | public function key() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Iterator interface |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | 1 | public function valid() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Iterator interface |
||
| 528 | */ |
||
| 529 | 1 | public function rewind() |
|
| 533 | } |
||
| 534 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: