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 NodeObject 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 NodeObject |
||
52 | */ |
||
53 | private $subject; |
||
54 | |||
55 | /** |
||
56 | * tree children |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $children = []; |
||
61 | |||
62 | /** |
||
63 | * tree path children |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $pathChildren = []; |
||
68 | |||
69 | /** |
||
70 | * the blob of the actual tree |
||
71 | * |
||
72 | * @var \GitElephant\Objects\NodeObject |
||
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\Tree |
||
83 | */ |
||
84 | public static function createFromOutputLines(Repository $repository, array $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 NodeObject $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', NodeObject $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(array $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(array $outputLines) |
|
269 | |||
270 | /** |
||
271 | * Reorder children of the tree |
||
272 | * Tree first (alphabetically) and then blobs (alphabetically) |
||
273 | * |
||
274 | * @param \GitElephant\Objects\NodeObject $a the first object |
||
275 | * @param \GitElephant\Objects\NodeObject $b the second object |
||
276 | * |
||
277 | * @return int |
||
278 | */ |
||
279 | 7 | private function sortChildren(NodeObject $a, NodeObject $b) |
|
290 | |||
291 | /** |
||
292 | * Parse a single line into pieces |
||
293 | * |
||
294 | * @param string $line a single line output from the git binary |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | 15 | private function parseLine($line) |
|
299 | { |
||
300 | 15 | if ($line == '') { |
|
301 | return; |
||
302 | } |
||
303 | |||
304 | 15 | $slices = NodeObject::getLineSlices($line); |
|
305 | 15 | if ($this->isBlob()) { |
|
306 | $this->pathChildren[] = $this->blob->getName(); |
||
307 | } else { |
||
308 | 15 | if ($this->isRoot()) { |
|
309 | // if is root check for first children |
||
310 | 9 | $pattern = '/(\w+)\/(.*)/'; |
|
311 | 9 | $replacement = '$1'; |
|
312 | } else { |
||
313 | // filter by the children of the path |
||
314 | 10 | $actualPath = $this->subject->getFullPath(); |
|
315 | 10 | if (!preg_match(sprintf('/^%s\/(\w*)/', preg_quote($actualPath, '/')), $slices['fullPath'])) { |
|
316 | 7 | return; |
|
317 | } |
||
318 | 5 | $pattern = sprintf('/^%s\/(\w*)/', preg_quote($actualPath, '/')); |
|
319 | 5 | $replacement = '$1'; |
|
320 | } |
||
321 | |||
322 | 10 | $name = preg_replace($pattern, $replacement, $slices['fullPath']); |
|
323 | 10 | if (strpos($name, '/') !== false) { |
|
324 | return; |
||
325 | } |
||
326 | |||
327 | 10 | if (!in_array($name, $this->pathChildren)) { |
|
328 | 10 | $path = rtrim(rtrim($slices['fullPath'], $name), '/'); |
|
329 | 10 | $treeObject = new TreeObject( |
|
330 | 10 | $this->repository, |
|
331 | 10 | $slices['permissions'], |
|
332 | 10 | $slices['type'], |
|
333 | 10 | $slices['sha'], |
|
334 | 10 | $slices['size'], |
|
335 | $name, |
||
336 | $path |
||
337 | ); |
||
338 | 10 | $this->children[] = $treeObject; |
|
339 | 10 | $this->pathChildren[] = $name; |
|
340 | } |
||
341 | } |
||
342 | 10 | } |
|
343 | |||
344 | /** |
||
345 | * get the last commit message for this tree |
||
346 | * |
||
347 | * @param string $ref |
||
348 | * |
||
349 | * @throws \RuntimeException |
||
350 | * @return Commit\Message |
||
351 | */ |
||
352 | public function getLastCommitMessage($ref = 'master') |
||
356 | |||
357 | /** |
||
358 | * get author of the last commit |
||
359 | * |
||
360 | * @param string $ref |
||
361 | * |
||
362 | * @throws \RuntimeException |
||
363 | * @return Author |
||
364 | */ |
||
365 | public function getLastCommitAuthor($ref = 'master') |
||
369 | |||
370 | /** |
||
371 | * get the last commit for a given treeish, for the actual tree |
||
372 | * |
||
373 | * @param string $ref |
||
374 | * |
||
375 | * @throws \RuntimeException |
||
376 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
377 | * @return Commit |
||
378 | */ |
||
379 | public function getLastCommit($ref = 'master') |
||
388 | |||
389 | /** |
||
390 | * get the tree object for this tree |
||
391 | * |
||
392 | * @return \GitElephant\Objects\NodeObject |
||
393 | */ |
||
394 | 1 | public function getObject() |
|
398 | |||
399 | /** |
||
400 | * Blob getter |
||
401 | * |
||
402 | * @return \GitElephant\Objects\NodeObject |
||
403 | */ |
||
404 | 5 | public function getBlob() |
|
408 | |||
409 | /** |
||
410 | * Get Subject |
||
411 | * |
||
412 | * @return \GitElephant\Objects\NodeObject |
||
413 | */ |
||
414 | 1 | public function getSubject() |
|
418 | |||
419 | /** |
||
420 | * Get Ref |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function getRef() |
||
428 | |||
429 | /** |
||
430 | * ArrayAccess interface |
||
431 | * |
||
432 | * @param int $offset offset |
||
433 | * |
||
434 | * @return bool |
||
435 | */ |
||
436 | public function offsetExists($offset) |
||
440 | |||
441 | |||
442 | /** |
||
443 | * ArrayAccess interface |
||
444 | * |
||
445 | * @param int $offset offset |
||
446 | * |
||
447 | * @return null |
||
448 | */ |
||
449 | 8 | public function offsetGet($offset) |
|
453 | |||
454 | /** |
||
455 | * ArrayAccess interface |
||
456 | * |
||
457 | * @param int $offset offset |
||
458 | * @param mixed $value value |
||
459 | */ |
||
460 | public function offsetSet($offset, $value) |
||
468 | |||
469 | /** |
||
470 | * ArrayAccess interface |
||
471 | * |
||
472 | * @param int $offset offset |
||
473 | */ |
||
474 | public function offsetUnset($offset) |
||
478 | |||
479 | /** |
||
480 | * Countable interface |
||
481 | * |
||
482 | * @return int |
||
483 | */ |
||
484 | 4 | public function count() |
|
488 | |||
489 | /** |
||
490 | * Iterator interface |
||
491 | * |
||
492 | * @return mixed |
||
493 | */ |
||
494 | 1 | public function current() |
|
498 | |||
499 | /** |
||
500 | * Iterator interface |
||
501 | */ |
||
502 | 1 | public function next() |
|
506 | |||
507 | /** |
||
508 | * Iterator interface |
||
509 | * |
||
510 | * @return int |
||
511 | */ |
||
512 | public function key() |
||
516 | |||
517 | /** |
||
518 | * Iterator interface |
||
519 | * |
||
520 | * @return bool |
||
521 | */ |
||
522 | 1 | public function valid() |
|
526 | |||
527 | /** |
||
528 | * Iterator interface |
||
529 | */ |
||
530 | 1 | public function rewind() |
|
534 | } |
||
535 |