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 |
||
| 36 | class Tree extends NodeObject implements \ArrayAccess, \Countable, \Iterator |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var string|null |
||
| 40 | */ |
||
| 41 | private $ref; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * the cursor position |
||
| 45 | * |
||
| 46 | * @var int|null |
||
| 47 | */ |
||
| 48 | private $position; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * the tree subject |
||
| 52 | * |
||
| 53 | * @var NodeObject|null |
||
| 54 | */ |
||
| 55 | private $subject; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * tree children |
||
| 59 | * |
||
| 60 | * @var array<TreeObject> |
||
| 61 | */ |
||
| 62 | private $children = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * tree path children |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $pathChildren = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * the blob of the actual tree |
||
| 73 | * |
||
| 74 | * @var \GitElephant\Objects\NodeObject|null |
||
| 75 | */ |
||
| 76 | private $blob; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * static method to generate standalone log |
||
| 80 | * |
||
| 81 | * @param \GitElephant\Repository $repository repo |
||
| 82 | * @param array $outputLines output lines from command.log |
||
| 83 | * |
||
| 84 | * @return \GitElephant\Objects\Tree |
||
| 85 | */ |
||
| 86 | public static function createFromOutputLines(Repository $repository, array $outputLines): \GitElephant\Objects\Tree |
||
| 93 | |||
| 94 | /** |
||
| 95 | * get the commit properties from command |
||
| 96 | * |
||
| 97 | * @see LsTreeCommand::tree |
||
| 98 | */ |
||
| 99 | 15 | private function createFromCommand(): void |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Some path examples: |
||
| 108 | * empty string for root |
||
| 109 | * folder1/folder2 |
||
| 110 | * folder1/folder2/filename |
||
| 111 | * |
||
| 112 | * @param \GitElephant\Repository $repository the repository |
||
| 113 | * @param string $ref a treeish reference |
||
| 114 | * @param NodeObject $subject the subject |
||
| 115 | * |
||
| 116 | * @throws \RuntimeException |
||
| 117 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 118 | * @internal param \GitElephant\Objects\Object|string $treeObject Object instance |
||
| 119 | */ |
||
| 120 | 15 | public function __construct(Repository $repository, $ref = 'HEAD', NodeObject $subject = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * parse the output of a git command showing a ls-tree |
||
| 131 | * |
||
| 132 | * @param array $outputLines output lines |
||
| 133 | */ |
||
| 134 | 15 | private function parseOutputLines(array $outputLines): void |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @return CallerInterface |
||
| 147 | */ |
||
| 148 | 15 | private function getCaller(): CallerInterface |
|
| 152 | |||
| 153 | /** |
||
| 154 | * get the current tree parent, null if root |
||
| 155 | * |
||
| 156 | * @return null|string |
||
| 157 | */ |
||
| 158 | public function getParent(): ?string |
||
| 166 | |||
| 167 | /** |
||
| 168 | * tell if the tree created is the root of the repository |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 15 | public function isRoot(): bool |
|
| 176 | |||
| 177 | /** |
||
| 178 | * tell if the path given is a blob path |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 15 | public function isBlob(): bool |
|
| 186 | |||
| 187 | /** |
||
| 188 | * the current tree path is a binary file |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function isBinary(): bool |
||
| 196 | |||
| 197 | /** |
||
| 198 | * get binary data |
||
| 199 | * |
||
| 200 | * @throws \RuntimeException |
||
| 201 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
| 202 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
| 203 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getBinaryData(): string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Return an array like this |
||
| 215 | * 0 => array( |
||
| 216 | * 'path' => the path to the current element |
||
| 217 | * 'label' => the name of the current element |
||
| 218 | * ), |
||
| 219 | * 1 => array(), |
||
| 220 | * ... |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public function getBreadcrumb(): array |
||
| 245 | |||
| 246 | /** |
||
| 247 | * check if the path is equals to a fullPath |
||
| 248 | * to tell if it's a blob |
||
| 249 | * |
||
| 250 | * @param array $outputLines output lines |
||
| 251 | * |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | 15 | private function scanPathsForBlob(array $outputLines): void |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Reorder children of the tree |
||
| 276 | * Tree first (alphabetically) and then blobs (alphabetically) |
||
| 277 | * |
||
| 278 | * @param \GitElephant\Objects\NodeObject $a the first object |
||
| 279 | * @param \GitElephant\Objects\NodeObject $b the second object |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | 7 | private static function sortChildren(NodeObject $a, NodeObject $b): int |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Parse a single line into pieces |
||
| 297 | * |
||
| 298 | * @param string $line a single line output from the git binary |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | 15 | private function parseLine(string $line): void |
|
| 303 | { |
||
| 304 | 15 | if ($line === '') { |
|
| 305 | return; |
||
| 306 | } |
||
| 307 | |||
| 308 | 15 | $slices = NodeObject::getLineSlices($line); |
|
| 309 | 15 | if ($this->isBlob()) { |
|
| 310 | $this->pathChildren[] = $this->blob->getName(); |
||
| 311 | } else { |
||
| 312 | 15 | if ($this->isRoot()) { |
|
| 313 | // if is root check for first children |
||
| 314 | 9 | $pattern = '/(\w+)\/(.*)/'; |
|
| 315 | 9 | $replacement = '$1'; |
|
| 316 | } else { |
||
| 317 | // filter by the children of the path |
||
| 318 | 10 | $actualPath = $this->subject->getFullPath(); |
|
| 319 | 10 | if (!preg_match(sprintf('/^%s\/(\w*)/', preg_quote($actualPath, '/')), $slices['fullPath'])) { |
|
| 320 | 7 | return; |
|
| 321 | } |
||
| 322 | 5 | $pattern = sprintf('/^%s\/(\w*)/', preg_quote($actualPath, '/')); |
|
| 323 | 5 | $replacement = '$1'; |
|
| 324 | } |
||
| 325 | |||
| 326 | 10 | $name = preg_replace($pattern, $replacement, $slices['fullPath']); |
|
| 327 | 10 | if (strpos($name, '/') !== false) { |
|
| 328 | return; |
||
| 329 | } |
||
| 330 | |||
| 331 | 10 | if (!in_array($name, $this->pathChildren)) { |
|
| 332 | 10 | $path = rtrim(rtrim($slices['fullPath'], $name), '/'); |
|
| 333 | 10 | $treeObject = new TreeObject( |
|
| 334 | 10 | $this->repository, |
|
| 335 | 10 | $slices['permissions'], |
|
| 336 | 10 | $slices['type'], |
|
| 337 | 10 | $slices['sha'], |
|
| 338 | 10 | $slices['size'], |
|
| 339 | 10 | $name, |
|
| 340 | 10 | $path |
|
| 341 | ); |
||
| 342 | 10 | $this->children[] = $treeObject; |
|
| 343 | 10 | $this->pathChildren[] = $name; |
|
| 344 | } |
||
| 345 | } |
||
| 346 | 10 | } |
|
| 347 | |||
| 348 | /** |
||
| 349 | * get the last commit message for this tree |
||
| 350 | * |
||
| 351 | * @param string $ref |
||
| 352 | * |
||
| 353 | * @throws \RuntimeException |
||
| 354 | * @return Commit\Message |
||
| 355 | */ |
||
| 356 | public function getLastCommitMessage($ref = 'master'): \GitElephant\Objects\Commit\Message |
||
| 360 | |||
| 361 | /** |
||
| 362 | * get author of the last commit |
||
| 363 | * |
||
| 364 | * @param string $ref |
||
| 365 | * |
||
| 366 | * @throws \RuntimeException |
||
| 367 | * @return Author |
||
| 368 | */ |
||
| 369 | public function getLastCommitAuthor($ref = 'master'): \GitElephant\Objects\Author |
||
| 373 | |||
| 374 | /** |
||
| 375 | * get the last commit for a given treeish, for the actual tree |
||
| 376 | * |
||
| 377 | * @param string $ref |
||
| 378 | * |
||
| 379 | * @throws \RuntimeException |
||
| 380 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
| 381 | * @return Commit |
||
| 382 | */ |
||
| 383 | public function getLastCommit($ref = 'master'): ?\GitElephant\Objects\Commit |
||
| 392 | |||
| 393 | /** |
||
| 394 | * get the tree object for this tree |
||
| 395 | * |
||
| 396 | * @return \GitElephant\Objects\NodeObject|null |
||
| 397 | */ |
||
| 398 | 1 | public function getObject(): ?\GitElephant\Objects\NodeObject |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Blob getter |
||
| 405 | * |
||
| 406 | * @return \GitElephant\Objects\NodeObject|null |
||
| 407 | */ |
||
| 408 | 5 | public function getBlob(): ?\GitElephant\Objects\NodeObject |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get Subject |
||
| 415 | * |
||
| 416 | * @return \GitElephant\Objects\NodeObject|null |
||
| 417 | */ |
||
| 418 | 1 | public function getSubject(): ?\GitElephant\Objects\NodeObject |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Get Ref |
||
| 425 | * |
||
| 426 | * @return string|null |
||
| 427 | */ |
||
| 428 | public function getRef(): ?string |
||
| 432 | |||
| 433 | /** |
||
| 434 | * ArrayAccess interface |
||
| 435 | * |
||
| 436 | * @param int $offset offset |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | public function offsetExists($offset): bool |
||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * ArrayAccess interface |
||
| 448 | * |
||
| 449 | * @param int $offset offset |
||
| 450 | * |
||
| 451 | * @return NodeObject|null |
||
| 452 | */ |
||
| 453 | 8 | public function offsetGet($offset) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * ArrayAccess interface |
||
| 460 | * |
||
| 461 | * @param int|null $offset offset |
||
| 462 | * @param TreeObject $value value |
||
| 463 | */ |
||
| 464 | public function offsetSet($offset, $value): void |
||
| 472 | |||
| 473 | /** |
||
| 474 | * ArrayAccess interface |
||
| 475 | * |
||
| 476 | * @param int $offset offset |
||
| 477 | */ |
||
| 478 | public function offsetUnset($offset): void |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Countable interface |
||
| 485 | * |
||
| 486 | * @return int |
||
| 487 | */ |
||
| 488 | 4 | public function count(): int |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Iterator interface |
||
| 495 | * |
||
| 496 | * @return TreeObject|null |
||
| 497 | */ |
||
| 498 | 1 | public function current(): ?TreeObject |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Iterator interface |
||
| 505 | */ |
||
| 506 | 1 | public function next(): void |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Iterator interface |
||
| 513 | * |
||
| 514 | * @return int |
||
| 515 | */ |
||
| 516 | public function key(): int |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Iterator interface |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | 1 | public function valid(): bool |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Iterator interface |
||
| 533 | */ |
||
| 534 | 1 | public function rewind(): void |
|
| 538 | } |
||
| 539 |
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: