Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Repository extends AbstractRepository |
||
50 | { |
||
51 | const RESET_STAGED = 1; |
||
52 | const RESET_WORKING = 2; |
||
53 | const RESET_ALL = 3; |
||
54 | |||
55 | const BRANCHES_LOCAL = 1; |
||
56 | const BRANCHES_REMOTE = 2; |
||
57 | const BRANCHES_ALL = 3; |
||
58 | |||
59 | /** |
||
60 | * The Git binary |
||
61 | * |
||
62 | * @var Binary |
||
63 | */ |
||
64 | protected $git; |
||
65 | |||
66 | /** |
||
67 | * Opens a Git repository on the file system, optionally creates and initializes a new repository |
||
68 | * |
||
69 | * @param string $repositoryPath The full path to the repository |
||
70 | * @param Binary|string|null $git The Git binary |
||
71 | * @param boolean|integer $createIfNotExists False to fail on non-existing repositories, directory |
||
72 | * creation mode, such as 0755 if the command |
||
73 | * should create the directory and init the repository instead |
||
74 | * @param array|null $initArguments Arguments to be passed to git-init if initializing a |
||
75 | * repository |
||
76 | * @param boolean $findRepositoryRoot False to use the repository path as the root directory. |
||
77 | * |
||
78 | * @return Repository |
||
79 | * @throws \RuntimeException If the path cannot be created |
||
80 | * @throws \InvalidArgumentException If the path is not valid or if it's not a valid Git repository |
||
81 | */ |
||
82 | 103 | public static function open($repositoryPath, $git = null, $createIfNotExists = false, $initArguments = null, $findRepositoryRoot = true) |
|
83 | { |
||
84 | 103 | $git = Binary::ensure($git); |
|
85 | 103 | $repositoryRoot = null; |
|
86 | |||
87 | 103 | if (!is_string($repositoryPath)) { |
|
88 | throw new \InvalidArgumentException(sprintf( |
||
89 | '"%s" is not a valid path', $repositoryPath |
||
90 | )); |
||
91 | } |
||
92 | |||
93 | 103 | if ($findRepositoryRoot) { |
|
94 | 103 | $repositoryRoot = self::findRepositoryRoot($repositoryPath); |
|
95 | } |
||
96 | |||
97 | 103 | if ($repositoryRoot === null) { |
|
98 | 4 | if (!$createIfNotExists) { |
|
99 | 2 | throw new \InvalidArgumentException(sprintf( |
|
100 | 2 | '"%s" is not a valid path', $repositoryPath |
|
101 | )); |
||
102 | } else { |
||
103 | 2 | if (!file_exists($repositoryPath) && !mkdir($repositoryPath, $createIfNotExists, true)) { |
|
104 | throw new \RuntimeException(sprintf( |
||
105 | '"%s" cannot be created', $repositoryPath |
||
106 | )); |
||
107 | 2 | } else if (!is_dir($repositoryPath)) { |
|
108 | throw new \InvalidArgumentException(sprintf( |
||
109 | '"%s" is not a valid path', $repositoryPath |
||
110 | )); |
||
111 | } |
||
112 | 2 | self::initRepository($git, $repositoryPath, $initArguments); |
|
113 | 2 | $repositoryRoot = $repositoryPath; |
|
114 | } |
||
115 | } |
||
116 | |||
117 | 101 | if ($repositoryRoot === null) { |
|
118 | throw new \InvalidArgumentException(sprintf( |
||
119 | '"%s" is not a valid Git repository', $repositoryPath |
||
120 | )); |
||
121 | } |
||
122 | |||
123 | 101 | return new static($repositoryRoot, $git); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Initializes a path to be used as a Git repository |
||
128 | * |
||
129 | * @param Binary $git The Git binary |
||
130 | * @param string $path The repository path |
||
131 | * @param array $initArguments Arguments to pass to git-init when initializing the repository |
||
132 | */ |
||
133 | 2 | protected static function initRepository(Binary $git, $path, $initArguments = null) |
|
141 | |||
142 | /** |
||
143 | * Tries to find the root directory for a given repository path |
||
144 | * |
||
145 | * @param string $path The file system path |
||
146 | * @return string|null NULL if the root cannot be found, the root path otherwise |
||
147 | */ |
||
148 | 103 | public static function findRepositoryRoot($path) |
|
155 | |||
156 | /** |
||
157 | * Creates a new repository instance - use {@see open()} instead |
||
158 | * |
||
159 | * @param string $repositoryPath |
||
160 | * @param Binary $git |
||
161 | */ |
||
162 | 101 | protected function __construct($repositoryPath, Binary $git) |
|
167 | |||
168 | /** |
||
169 | * Returns the Git binary |
||
170 | * |
||
171 | * @return Binary |
||
172 | */ |
||
173 | 78 | public function getGit() |
|
177 | |||
178 | /** |
||
179 | * Returns the current commit hash |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | 53 | public function getCurrentCommit() |
|
193 | |||
194 | /** |
||
195 | * Commits the currently staged changes into the repository |
||
196 | * |
||
197 | * @param string $commitMsg The commit message |
||
198 | * @param array|null $file Restrict commit to the given files or NULL to commit all staged changes |
||
199 | * @param array $extraArgs Allow the user to pass extra args eg array('-i') |
||
200 | * @param string|null $author The author |
||
201 | */ |
||
202 | 54 | public function commit($commitMsg, array $file = null, $author = null, array $extraArgs = array()) |
|
225 | |||
226 | /** |
||
227 | * Resets the working directory and/or the staging area and discards all changes |
||
228 | * |
||
229 | * @param integer $what Bit mask to indicate which parts should be reset |
||
230 | */ |
||
231 | 2 | public function reset($what = self::RESET_ALL) |
|
250 | |||
251 | /** |
||
252 | * Adds one or more files to the staging area |
||
253 | * |
||
254 | * @param array $file The file(s) to be added or NULL to add all new and/or changed files to the staging area |
||
255 | * @param boolean $force |
||
256 | */ |
||
257 | 40 | public function add(array $file = null, $force = false) |
|
276 | |||
277 | /** |
||
278 | * Removes one or more files from the repository but does not commit the changes |
||
279 | * |
||
280 | * @param array $file The file(s) to be removed |
||
281 | * @param boolean $recursive True to recursively remove subdirectories |
||
282 | * @param boolean $force True to continue even though Git reports a possible conflict |
||
283 | */ |
||
284 | 12 | public function remove(array $file, $recursive = false, $force = false) |
|
302 | |||
303 | /** |
||
304 | * Renames a file but does not commit the changes |
||
305 | * |
||
306 | * @param string $fromPath The source path |
||
307 | * @param string $toPath The destination path |
||
308 | * @param boolean $force True to continue even though Git reports a possible conflict |
||
309 | */ |
||
310 | 8 | public function move($fromPath, $toPath, $force = false) |
|
325 | |||
326 | /** |
||
327 | * Writes data to a file and commit the changes immediately |
||
328 | * |
||
329 | * @param string $path The file path |
||
330 | * @param string|array $data The data to write to the file |
||
331 | * @param string|null $commitMsg The commit message used when committing the changes |
||
332 | * @param integer|null $fileMode The mode for creating the file |
||
333 | * @param integer|null $dirMode The mode for creating the intermediate directories |
||
334 | * @param boolean $recursive Create intermediate directories recursively if required |
||
335 | * @param string|null $author The author |
||
336 | * @return string The current commit hash |
||
337 | * @throws \RuntimeException If the file could not be written |
||
338 | */ |
||
339 | 24 | public function writeFile($path, $data, $commitMsg = null, $fileMode = null, |
|
373 | |||
374 | /** |
||
375 | * Writes data to a file and commit the changes immediately |
||
376 | * |
||
377 | * @param string $path The directory path |
||
378 | * @param string|null $commitMsg The commit message used when committing the changes |
||
379 | * @param integer|null $dirMode The mode for creating the intermediate directories |
||
380 | * @param boolean $recursive Create intermediate directories recursively if required |
||
381 | * @param string|null $author The author |
||
382 | * @return string The current commit hash |
||
383 | * @throws \RuntimeException If the directory could not be created |
||
384 | */ |
||
385 | 4 | public function createDirectory($path, $commitMsg = null, $dirMode = null, $recursive = true, $author = null) |
|
392 | |||
393 | /** |
||
394 | * Removes a file and commit the changes immediately |
||
395 | * |
||
396 | * @param string $path The file path |
||
397 | * @param string|null $commitMsg The commit message used when committing the changes |
||
398 | * @param boolean $recursive True to recursively remove subdirectories |
||
399 | * @param boolean $force True to continue even though Git reports a possible conflict |
||
400 | * @param string|null $author The author |
||
401 | * @return string The current commit hash |
||
402 | */ |
||
403 | 12 | public function removeFile($path, $commitMsg = null, $recursive = false, $force = false, $author = null) |
|
415 | |||
416 | /** |
||
417 | * Renames a file and commit the changes immediately |
||
418 | * |
||
419 | * @param string $fromPath The source path |
||
420 | * @param string $toPath The destination path |
||
421 | * @param string|null $commitMsg The commit message used when committing the changes |
||
422 | * @param boolean $force True to continue even though Git reports a possible conflict |
||
423 | * @param string|null $author The author |
||
424 | * @return string The current commit hash |
||
425 | */ |
||
426 | 8 | public function renameFile($fromPath, $toPath, $commitMsg = null, $force = false, $author = null) |
|
438 | |||
439 | /** |
||
440 | * Prepares a list of named arguments for use as command-line arguments. |
||
441 | * Preserves ordering, while prepending - and -- to argument names, then leaves value alone. |
||
442 | * |
||
443 | * @param array $namedArguments Named argument list to format |
||
444 | * @return array |
||
445 | **/ |
||
446 | 2 | protected function _prepareNamedArgumentsForCLI($namedArguments) { |
|
485 | |||
486 | /** |
||
487 | * _parseNamedArguments |
||
488 | * |
||
489 | * Takes a set of regular arguments and a set of extended/named arguments, combines them, and returns the results. |
||
490 | * |
||
491 | * The merging method is far from foolproof, but should take care of the vast majority of situations. Where it fails is function calls |
||
492 | * in which the an argument is regular-style, is an array, and only has keys which are present in the named arguments. |
||
493 | * |
||
494 | * The easy way to trigger it would be to pass an empty array in one of the arguments. |
||
495 | * |
||
496 | * There's a bunch of array_splices. Those are in place so that if named arguments have orders that they should be called in, |
||
497 | * they're not disturbed. So... calling with |
||
498 | * getLog(5, ['reverse', 'diff' => 'git', 'path/to/repo/file.txt'] |
||
499 | * will keep things in the order for the git call: |
||
500 | * git-log --limit=5 --skip=10 --reverse --diff=git path/to/to/repo/file.txt |
||
501 | * and will put defaults at the beginning of the call, as well. |
||
502 | * |
||
503 | * @param array $regularStyleArguments An ordered list of the names of regular-style arguments that should be accepted. |
||
504 | * @param array $namedStyleArguments An associative array of named arguments to their default value, |
||
505 | * or null where no default is desired. |
||
506 | * @param array $arguments The result of func_get_args() in the original function call we're helping. |
||
507 | * @param int $skipNamedTo Index to which array arguments should be assumed NOT to be named arguments. |
||
508 | * @return array A filtered associative array of the resulting arguments. |
||
509 | */ |
||
510 | 2 | protected function _parseNamedArguments($regularStyleArguments, $namedStyleArguments, $arguments, $skipNamedTo = 0) { |
|
557 | |||
558 | /** |
||
559 | * Returns the current repository log |
||
560 | * |
||
561 | * @param integer|null $limit The maximum number of log entries returned |
||
562 | * @param integer|null $skip Number of log entries that are skipped from the beginning |
||
563 | * @return array |
||
564 | */ |
||
565 | 2 | public function getLog($limit = null, $skip = null) |
|
566 | { |
||
567 | $regularStyleArguments = array( |
||
568 | 2 | 'limit', |
|
569 | 'skip' |
||
570 | ); |
||
571 | |||
572 | $namedStyleArguments = array( |
||
573 | 2 | 'abbrev' => null, |
|
574 | 'abbrev-commit' => null, |
||
575 | 'after' => null, |
||
576 | 'all' => null, |
||
577 | 'all-match' => null, |
||
578 | 'ancestry-path' => null, |
||
579 | 'author' => null, |
||
580 | 'basic-regexp' => null, |
||
581 | 'before' => null, |
||
582 | 'binary' => null, |
||
583 | 'bisect' => null, |
||
584 | 'boundary' => null, |
||
585 | 'branches' => null, |
||
586 | 'break-rewrites' => null, |
||
587 | 'cc' => null, |
||
588 | 'check' => null, |
||
589 | 'cherry' => null, |
||
590 | 'cherry-mark' => null, |
||
591 | 'cherry-pick' => null, |
||
592 | 'children' => null, |
||
593 | 'color' => null, |
||
594 | 'color-words' => null, |
||
595 | 'combined' => null, |
||
596 | 'committer' => null, |
||
597 | 'date' => null, |
||
598 | 'date-order' => null, |
||
599 | 'decorate' => null, |
||
600 | 'dense' => null, |
||
601 | 'diff-filter' => null, |
||
602 | 'dirstat' => null, |
||
603 | 'do-walk' => null, |
||
604 | 'dst-prefix' => null, |
||
605 | 'encoding' => null, |
||
606 | 'exit-code' => null, |
||
607 | 'ext-diff' => null, |
||
608 | 'extended-regexp' => null, |
||
609 | 'find-copies' => null, |
||
610 | 'find-copies-harder' => null, |
||
611 | 'find-renames' => null, |
||
612 | 'first-parent' => null, |
||
613 | 'fixed-strings' => null, |
||
614 | 'follow' => null, |
||
615 | 'format' => 'fuller', |
||
616 | 'full-diff' => null, |
||
617 | 'full-history' => null, |
||
618 | 'full-index' => null, |
||
619 | 'function-context' => null, |
||
620 | 'glob' => null, |
||
621 | 'graph' => null, |
||
622 | 'grep' => null, |
||
623 | 'grep-reflog' => null, |
||
624 | 'histogram' => null, |
||
625 | 'ignore-all-space' => null, |
||
626 | 'ignore-missing' => null, |
||
627 | 'ignore-space-at-eol' => null, |
||
628 | 'ignore-space-change' => null, |
||
629 | 'ignore-submodules' => null, |
||
630 | 'inter-hunk-context' => null, |
||
631 | 'irreversible-delete' => null, |
||
632 | 'left-only' => null, |
||
633 | 'left-right' => null, |
||
634 | 'log-size' => null, |
||
635 | 'max-count' => null, |
||
636 | 'max-parents' => null, |
||
637 | 'merge' => null, |
||
638 | 'merges' => null, |
||
639 | 'min-parents' => null, |
||
640 | 'minimal' => null, |
||
641 | 'name-only' => null, |
||
642 | 'name-status' => null, |
||
643 | 'no-abbrev' => null, |
||
644 | 'no-abbrev-commit' => null, |
||
645 | 'no-color' => null, |
||
646 | 'no-decorate' => null, |
||
647 | 'no-ext-diff' => null, |
||
648 | 'no-max-parents' => null, |
||
649 | 'no-merges' => null, |
||
650 | 'no-min-parents' => null, |
||
651 | 'no-notes' => null, |
||
652 | 'no-prefix' => null, |
||
653 | 'no-renames' => null, |
||
654 | 'no-textconv' => null, |
||
655 | 'no-walk' => null, |
||
656 | 'not' => null, |
||
657 | 'notes' => null, |
||
658 | 'numstat' => null, |
||
659 | 'objects' => null, |
||
660 | 'objects-edge' => null, |
||
661 | 'oneline' => null, |
||
662 | 'parents' => null, |
||
663 | 'patch' => null, |
||
664 | 'patch-with-raw' => null, |
||
665 | 'patch-with-stat' => null, |
||
666 | 'patience' => null, |
||
667 | 'perl-regexp' => null, |
||
668 | 'pickaxe-all' => null, |
||
669 | 'pickaxe-regex' => null, |
||
670 | 'pretty' => null, |
||
671 | 'raw' => null, |
||
672 | 'regexp-ignore-case' => null, |
||
673 | 'relative' => null, |
||
674 | 'relative-date' => null, |
||
675 | 'remotes' => null, |
||
676 | 'remove-empty' => null, |
||
677 | 'reverse' => null, |
||
678 | 'right-only' => null, |
||
679 | 'shortstat' => null, |
||
680 | 'show-notes' => null, |
||
681 | 'show-signature' => null, |
||
682 | 'simplify-by-decoration' => null, |
||
683 | 'simplify-merges' => null, |
||
684 | 'since' => null, |
||
685 | 'skip' => null, |
||
686 | 'source' => null, |
||
687 | 'sparse' => null, |
||
688 | 'src-prefix' => null, |
||
689 | 'stat' => null, |
||
690 | 'stat-count' => null, |
||
691 | 'stat-graph-width' => null, |
||
692 | 'stat-name-width' => null, |
||
693 | 'stat-width' => null, |
||
694 | 'stdin' => null, |
||
695 | 'submodule' => null, |
||
696 | 'summary' => true, |
||
697 | 'tags' => null, |
||
698 | 'text' => null, |
||
699 | 'textconv' => null, |
||
700 | 'topo-order' => null, |
||
701 | 'unified' => null, |
||
702 | 'unpacked' => null, |
||
703 | 'until' => null, |
||
704 | 'verify' => null, |
||
705 | 'walk-reflogs' => null, |
||
706 | 'word-diff' => null, |
||
707 | 'word-diff-regex' => null, |
||
708 | 'z' => true |
||
709 | ); |
||
710 | |||
711 | 2 | $arguments = func_get_args(); |
|
712 | |||
713 | 2 | $arguments = $this->_parseNamedArguments($regularStyleArguments, $namedStyleArguments, $arguments); |
|
714 | |||
715 | 2 | if (!empty($arguments['limit'])) { |
|
716 | 2 | $limit = '' . (int) $arguments['limit']; |
|
717 | |||
718 | 2 | unset($arguments['limit']); |
|
719 | 2 | array_unshift($arguments, $limit); |
|
720 | } |
||
721 | |||
722 | 2 | $arguments = $this->_prepareNamedArgumentsForCLI($arguments); |
|
723 | |||
724 | /** @var $result CallResult */ |
||
725 | 2 | $result = $this->getGit()->{'log'}($this->getRepositoryPath(), $arguments); |
|
726 | 2 | $result->assertSuccess(sprintf('Cannot retrieve log from "%s"', |
|
727 | 2 | $this->getRepositoryPath() |
|
728 | )); |
||
729 | |||
730 | 2 | $output = $result->getStdOut(); |
|
731 | $log = array_map(function($f) { |
||
732 | 2 | return trim($f); |
|
733 | 2 | }, explode("\x0", $output)); |
|
734 | |||
735 | 2 | return $log; |
|
736 | } |
||
737 | |||
738 | /** |
||
739 | * Returns a string containing information about the given commit |
||
740 | * |
||
741 | * @param string $hash The commit ref |
||
742 | * @return string |
||
743 | */ |
||
744 | 39 | public function showCommit($hash) |
|
757 | |||
758 | /** |
||
759 | * Returns the content of a file at a given version |
||
760 | * |
||
761 | * @param string $file The path to the file |
||
762 | * @param string $ref The version ref |
||
763 | * @return string |
||
764 | */ |
||
765 | 5 | public function showFile($file, $ref = 'HEAD') |
|
778 | |||
779 | /** |
||
780 | * Returns information about an object at a given version |
||
781 | * |
||
782 | * The information returned is an array with the following structure |
||
783 | * array( |
||
784 | * 'type' => blob|tree|commit, |
||
785 | * 'mode' => 0040000 for a tree, 0100000 for a blob, 0 otherwise, |
||
786 | * 'size' => the size |
||
787 | * ) |
||
788 | * |
||
789 | * @param string $path The path to the object |
||
790 | * @param string $ref The version ref |
||
791 | * @return array The object info |
||
792 | */ |
||
793 | 5 | public function getObjectInfo($path, $ref = 'HEAD') |
|
828 | |||
829 | /** |
||
830 | * List the directory at a given version |
||
831 | * |
||
832 | * @param string $directory The path ot the directory |
||
833 | * @param string $ref The version ref |
||
834 | * @return array |
||
835 | */ |
||
836 | 15 | public function listDirectory($directory = '.', $ref = 'HEAD') |
|
860 | |||
861 | /** |
||
862 | * Returns the current status of the working directory and the staging area |
||
863 | * |
||
864 | * The returned array structure is |
||
865 | * array( |
||
866 | * 'file' => '...', |
||
867 | * 'x' => '.', |
||
868 | * 'y' => '.', |
||
869 | * 'renamed' => null/'...' |
||
870 | * ) |
||
871 | * |
||
872 | * @return array |
||
873 | */ |
||
874 | 32 | public function getStatus() |
|
904 | |||
905 | /** |
||
906 | * Returns the diff of a file |
||
907 | * |
||
908 | * @param string[] $files The path to the file |
||
909 | * @param bool $staged Should the diff return for the staged file |
||
910 | * @return string[] |
||
911 | */ |
||
912 | 1 | public function getDiff(array $files = null, $staged = false) |
|
952 | |||
953 | /** |
||
954 | * Returns true if there are uncommitted changes in the working directory and/or the staging area |
||
955 | * |
||
956 | * @return boolean |
||
957 | */ |
||
958 | 32 | public function isDirty() |
|
963 | |||
964 | /** |
||
965 | * Returns the name of the current branch |
||
966 | * |
||
967 | * @return string |
||
968 | */ |
||
969 | 1 | public function getCurrentBranch() |
|
983 | |||
984 | /** |
||
985 | * Returns a list of the branches in the repository |
||
986 | * |
||
987 | * @param integer $which Which branches to retrieve (all, local or remote-tracking) |
||
988 | * @return array |
||
989 | */ |
||
990 | 1 | public function getBranches($which = self::BRANCHES_LOCAL) |
|
1027 | |||
1028 | /** |
||
1029 | * Returns the remote info |
||
1030 | * |
||
1031 | * @return array |
||
1032 | */ |
||
1033 | public function getCurrentRemote() |
||
1051 | |||
1052 | /** |
||
1053 | * Resolves an absolute path into a path relative to the repository path |
||
1054 | * |
||
1055 | * @param string|array $path A file system path (or an array of paths) |
||
1056 | * @return string|array Either a single path or an array of paths is returned |
||
1057 | */ |
||
1058 | 68 | public function resolveLocalPath($path) |
|
1066 | } |
||
1067 |