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) |
|
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 | 54 | 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 | 55 | 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 | 13 | 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 | 9 | 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 | 13 | 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 | 9 | 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) |
|
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 | 6 | 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 | 33 | public function getStatus() |
|
904 | |||
905 | /** |
||
906 | * Returns the diff of a file |
||
907 | * |
||
908 | * @param string $file 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($file, $staged=false) |
|
927 | |||
928 | /** |
||
929 | * Returns true if there are uncommitted changes in the working directory and/or the staging area |
||
930 | * |
||
931 | * @return boolean |
||
932 | */ |
||
933 | 33 | public function isDirty() |
|
938 | |||
939 | /** |
||
940 | * Returns the name of the current branch |
||
941 | * |
||
942 | * @return string |
||
943 | */ |
||
944 | 1 | public function getCurrentBranch() |
|
957 | |||
958 | /** |
||
959 | * Returns a list of the branches in the repository |
||
960 | * |
||
961 | * @param integer $which Which branches to retrieve (all, local or remote-tracking) |
||
962 | * @return array |
||
963 | */ |
||
964 | 1 | public function getBranches($which = self::BRANCHES_LOCAL) |
|
1001 | |||
1002 | /** |
||
1003 | * Returns the remote info |
||
1004 | * |
||
1005 | * @return array |
||
1006 | */ |
||
1007 | public function getCurrentRemote() |
||
1025 | } |
||
1026 |