Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like GitWorkingCopy 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 GitWorkingCopy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class GitWorkingCopy |
||
14 | { |
||
15 | /** |
||
16 | * The GitWrapper object that likely instantiated this class. |
||
17 | * |
||
18 | * @var \GitWrapper\GitWrapper |
||
19 | */ |
||
20 | protected $wrapper; |
||
21 | |||
22 | /** |
||
23 | * Path to the directory containing the working copy. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $directory; |
||
28 | |||
29 | /** |
||
30 | * The output captured by the last run Git commnd(s). |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $output = ''; |
||
35 | |||
36 | /** |
||
37 | * A boolean flagging whether the repository is cloned. |
||
38 | * |
||
39 | * If the variable is null, the a rudimentary check will be performed to see |
||
40 | * if the directory looks like it is a working copy. |
||
41 | * |
||
42 | * @param bool|null |
||
43 | */ |
||
44 | protected $cloned; |
||
45 | |||
46 | /** |
||
47 | * Constructs a GitWorkingCopy object. |
||
48 | * |
||
49 | * @param \GitWrapper\GitWrapper $wrapper |
||
50 | * The GitWrapper object that likely instantiated this class. |
||
51 | * @param string $directory |
||
52 | * Path to the directory containing the working copy. |
||
53 | */ |
||
54 | 212 | public function __construct(GitWrapper $wrapper, $directory) |
|
55 | { |
||
56 | 212 | $this->wrapper = $wrapper; |
|
57 | 212 | $this->directory = $directory; |
|
58 | 212 | } |
|
59 | |||
60 | /** |
||
61 | * Returns the GitWrapper object that likely instantiated this class. |
||
62 | * |
||
63 | * @return \GitWrapper\GitWrapper |
||
64 | */ |
||
65 | 12 | public function getWrapper() |
|
66 | { |
||
67 | 12 | return $this->wrapper; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Gets the path to the directory containing the working copy. |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | 4 | public function getDirectory() |
|
76 | { |
||
77 | 4 | return $this->directory; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Gets the output captured by the last run Git commnd(s). |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 148 | public function getOutput() |
|
86 | { |
||
87 | 148 | $output = $this->output; |
|
88 | 148 | $this->output = ''; |
|
89 | 148 | return $output; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Clears the stored output captured by the last run Git command(s). |
||
94 | * |
||
95 | * @return \GitWrapper\GitWorkingCopy |
||
96 | */ |
||
97 | 200 | public function clearOutput() |
|
98 | { |
||
99 | 200 | $this->output = ''; |
|
100 | 200 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Manually sets the cloned flag. |
||
105 | * |
||
106 | * @param boolean $cloned |
||
107 | * Whether the repository is cloned into the directory or not. |
||
108 | * |
||
109 | * @return \GitWrapper\GitWorkingCopy |
||
110 | */ |
||
111 | 208 | public function setCloned($cloned) |
|
116 | |||
117 | /** |
||
118 | * Checks whether a repository has already been cloned to this directory. |
||
119 | * |
||
120 | * If the flag is not set, test if it looks like we're at a git directory. |
||
121 | * |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 4 | public function isCloned() |
|
135 | |||
136 | /** |
||
137 | * Runs a Git command and captures the output. |
||
138 | * |
||
139 | * @param array $args |
||
140 | * The arguments passed to the command method. |
||
141 | * @param boolean $setDirectory |
||
142 | * Set the working directory, defaults to true. |
||
143 | * |
||
144 | * @return \GitWrapper\GitWorkingCopy |
||
145 | * |
||
146 | * @throws \GitWrapper\GitException |
||
147 | * |
||
148 | * @see GitWrapper::run() |
||
149 | */ |
||
150 | 208 | public function run($args, $setDirectory = true) |
|
151 | { |
||
152 | 208 | $command = call_user_func_array(array('GitWrapper\GitCommand', 'getInstance'), $args); |
|
153 | 208 | if ($setDirectory) { |
|
154 | 204 | $command->setDirectory($this->directory); |
|
155 | 204 | } |
|
156 | 208 | $this->output .= $this->wrapper->run($command); |
|
157 | 208 | return $this; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @defgroup command_helpers Git Command Helpers |
||
162 | * |
||
163 | * Helper methods that wrap common Git commands. |
||
164 | * |
||
165 | * @{ |
||
166 | */ |
||
167 | |||
168 | /** |
||
169 | * Returns the output of a `git status -s` command. |
||
170 | * |
||
171 | * @return string |
||
172 | * |
||
173 | * @throws \GitWrapper\GitException |
||
174 | */ |
||
175 | 20 | public function getStatus() |
|
179 | |||
180 | /** |
||
181 | * Returns true if there are changes to commit. |
||
182 | * |
||
183 | * @return bool |
||
184 | * |
||
185 | * @throws \GitWrapper\GitException |
||
186 | */ |
||
187 | 12 | public function hasChanges() |
|
192 | |||
193 | /** |
||
194 | * Returns whether HEAD has a remote tracking branch. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 20 | public function isTracking() |
|
207 | |||
208 | /** |
||
209 | * Returns whether HEAD is up-to-date with its remote tracking branch. |
||
210 | * |
||
211 | * @return bool |
||
212 | * |
||
213 | * @throws \GitWrapper\GitException |
||
214 | * Thrown when HEAD does not have a remote tracking branch. |
||
215 | */ |
||
216 | 4 | public function isUpToDate() |
|
226 | |||
227 | /** |
||
228 | * Returns whether HEAD is ahead of its remote tracking branch. |
||
229 | * |
||
230 | * If this returns true it means that commits are present locally which have |
||
231 | * not yet been pushed to the remote. |
||
232 | * |
||
233 | * @return bool |
||
234 | * |
||
235 | * @throws \GitWrapper\GitException |
||
236 | * Thrown when HEAD does not have a remote tracking branch. |
||
237 | */ |
||
238 | 4 | View Code Duplication | public function isAhead() |
249 | |||
250 | /** |
||
251 | * Returns whether HEAD is behind its remote tracking branch. |
||
252 | * |
||
253 | * If this returns true it means that a pull is needed to bring the branch |
||
254 | * up-to-date with the remote. |
||
255 | * |
||
256 | * @return bool |
||
257 | * |
||
258 | * @throws \GitWrapper\GitException |
||
259 | * Thrown when HEAD does not have a remote tracking branch. |
||
260 | */ |
||
261 | 4 | View Code Duplication | public function isBehind() |
272 | |||
273 | /** |
||
274 | * Returns whether HEAD needs to be merged with its remote tracking branch. |
||
275 | * |
||
276 | * If this returns true it means that HEAD has diverged from its remote |
||
277 | * tracking branch; new commits are present locally as well as on the |
||
278 | * remote. |
||
279 | * |
||
280 | * @return bool |
||
281 | * true if HEAD needs to be merged with the remote, false otherwise. |
||
282 | * |
||
283 | * @throws \GitWrapper\GitException |
||
284 | * Thrown when HEAD does not have a remote tracking branch. |
||
285 | */ |
||
286 | 4 | View Code Duplication | public function needsMerge() |
297 | |||
298 | /** |
||
299 | * Returns a GitBranches object containing information on the repository's |
||
300 | * branches. |
||
301 | * |
||
302 | * @return GitBranches |
||
303 | */ |
||
304 | 28 | public function getBranches() |
|
308 | |||
309 | /** |
||
310 | * Helper method that pushes a tag to a repository. |
||
311 | * |
||
312 | * This is synonymous with `git push origin tag v1.2.3`. |
||
313 | * |
||
314 | * @param string $tag |
||
315 | * The tag being pushed. |
||
316 | * @param string $repository |
||
317 | * The destination of the push operation, which is either a URL or name of |
||
318 | * the remote. Defaults to "origin". |
||
319 | * @param array $options |
||
320 | * (optional) An associative array of command line options. |
||
321 | * |
||
322 | * @see GitWorkingCopy::push() |
||
323 | */ |
||
324 | 4 | public function pushTag($tag, $repository = 'origin', array $options = array()) |
|
328 | |||
329 | /** |
||
330 | * Helper method that pushes all tags to a repository. |
||
331 | * |
||
332 | * This is synonymous with `git push --tags origin`. |
||
333 | * |
||
334 | * @param string $repository |
||
335 | * The destination of the push operation, which is either a URL or name of |
||
336 | * the remote. Defaults to "origin". |
||
337 | * @param array $options |
||
338 | * (optional) An associative array of command line options. |
||
339 | * |
||
340 | * @see GitWorkingCopy::push() |
||
341 | */ |
||
342 | 200 | public function pushTags($repository = 'origin', array $options = array()) |
|
347 | |||
348 | /** |
||
349 | * Fetches all remotes. |
||
350 | * |
||
351 | * This is synonymous with `git fetch --all`. |
||
352 | * |
||
353 | * @param array $options |
||
354 | * (optional) An associative array of command line options. |
||
355 | * |
||
356 | * @see GitWorkingCopy::fetch() |
||
357 | */ |
||
358 | 4 | public function fetchAll(array $options = array()) |
|
363 | |||
364 | /** |
||
365 | * Create a new branch and check it out. |
||
366 | * |
||
367 | * This is synonymous with `git checkout -b`. |
||
368 | * |
||
369 | * @param string $branch |
||
370 | * The new branch being created. |
||
371 | * |
||
372 | * @see GitWorkingCopy::checkout() |
||
373 | */ |
||
374 | 200 | public function checkoutNewBranch($branch, array $options = array()) |
|
379 | |||
380 | /** |
||
381 | * Adds a remote to the repository. |
||
382 | * |
||
383 | * @param string $name |
||
384 | * The name of the remote to add. |
||
385 | * @param string $url |
||
386 | * The URL of the remote to add. |
||
387 | * @param array $options |
||
388 | * An associative array of options, with the following keys: |
||
389 | * - -f: Boolean, set to true to run git fetch immediately after the |
||
390 | * remote is set up. Defaults to false. |
||
391 | * - --tags: Boolean. By default only the tags from the fetched branches |
||
392 | * are imported when git fetch is run. Set this to true to import every |
||
393 | * tag from the remote repository. Defaults to false. |
||
394 | * - --no-tags: Boolean, when set to true, git fetch does not import tags |
||
395 | * from the remote repository. Defaults to false. |
||
396 | * - -t: Optional array of branch names to track. If left empty, all |
||
397 | * branches will be tracked. |
||
398 | * - -m: Optional name of the master branch to track. This will set up a |
||
399 | * symbolic ref 'refs/remotes/<name>/HEAD which points at the specified |
||
400 | * master branch on the remote. When omitted, no symbolic ref will be |
||
401 | * created. |
||
402 | * |
||
403 | * @return \GitWrapper\GitWorkingCopy |
||
404 | * |
||
405 | * @throws \GitWrapper\GitException |
||
406 | * Thrown when the name or URL are missing. |
||
407 | */ |
||
408 | 56 | public function addRemote($name, $url, $options = array()) { |
|
442 | |||
443 | /** |
||
444 | * Removes the given remote. |
||
445 | * |
||
446 | * @param string $name |
||
447 | * The name of the remote to remove. |
||
448 | * |
||
449 | * @return \GitWrapper\GitWorkingCopy |
||
450 | */ |
||
451 | 4 | public function removeRemote($name) { |
|
452 | 4 | return $this->remote('rm', $name); |
|
453 | } |
||
454 | |||
455 | /** |
||
456 | * Checks if the given remote exists. |
||
457 | * |
||
458 | * @param string $name |
||
459 | * The name of the remote to check. |
||
460 | * |
||
461 | * @return bool |
||
462 | */ |
||
463 | 36 | public function hasRemote($name) { |
|
466 | |||
467 | /** |
||
468 | * Returns the given remote. |
||
469 | * |
||
470 | * @param string $name |
||
471 | * The name of the remote. |
||
472 | * |
||
473 | * @return array |
||
474 | * An associative array with the following keys: |
||
475 | * - fetch: the fetch URL. |
||
476 | * - push: the push URL. |
||
477 | * |
||
478 | * @throws \GitWrapper\GitException |
||
479 | * Thrown when the remote does not exist. |
||
480 | */ |
||
481 | 4 | public function getRemote($name) { |
|
488 | |||
489 | /** |
||
490 | * Returns all existing remotes. |
||
491 | * |
||
492 | * @return array |
||
493 | * An associative array, keyed by remote name, containing an associative |
||
494 | * array with the following keys: |
||
495 | * - fetch: the fetch URL. |
||
496 | * - push: the push URL. |
||
497 | */ |
||
498 | 40 | public function getRemotes() { |
|
508 | |||
509 | /** |
||
510 | * Returns the fetch or push URL of a given remote. |
||
511 | * |
||
512 | * @param string $remote |
||
513 | * The name of the remote for which to return the fetch or push URL. |
||
514 | * @param string $operation |
||
515 | * The operation for which to return the remote. Can be either 'fetch' or |
||
516 | * 'push'. Defaults to 'fetch'. |
||
517 | * |
||
518 | * @return string |
||
519 | * The URL. |
||
520 | */ |
||
521 | 56 | public function getRemoteUrl($remote, $operation = 'fetch') { |
|
540 | |||
541 | /** |
||
542 | * @} End of "defgroup command_helpers". |
||
543 | */ |
||
544 | |||
545 | /** |
||
546 | * @defgroup commands Git Commands |
||
547 | * |
||
548 | * All methods in this group correspond with Git commands, for example |
||
549 | * "git add", "git commit", "git push", etc. |
||
550 | * |
||
551 | * @{ |
||
552 | */ |
||
553 | |||
554 | /** |
||
555 | * Executes a `git add` command. |
||
556 | * |
||
557 | * Add file contents to the index. |
||
558 | * |
||
559 | * @code |
||
560 | * $git->add('some/file.txt'); |
||
561 | * @endcode |
||
562 | * |
||
563 | * @param string $filepattern |
||
564 | * Files to add content from. Fileglobs (e.g. *.c) can be given to add |
||
565 | * all matching files. Also a leading directory name (e.g. dir to add |
||
566 | * dir/file1 and dir/file2) can be given to add all files in the |
||
567 | * directory, recursively. |
||
568 | * @param array $options |
||
569 | * An optional array of command line options. |
||
570 | * |
||
571 | * @return \GitWrapper\GitWorkingCopy |
||
572 | * |
||
573 | * @throws \GitWrapper\GitException |
||
574 | */ |
||
575 | 200 | public function add($filepattern, array $options = array()) |
|
576 | { |
||
577 | $args = array( |
||
578 | 200 | 'add', |
|
579 | 200 | $filepattern, |
|
580 | 200 | $options, |
|
581 | 200 | ); |
|
582 | 200 | return $this->run($args); |
|
583 | } |
||
584 | |||
585 | /** |
||
586 | * Executes a `git apply` command. |
||
587 | * |
||
588 | * Apply a patch to files and/or to the index |
||
589 | * |
||
590 | * @code |
||
591 | * $git->apply('the/file/to/read/the/patch/from'); |
||
592 | * @endcode |
||
593 | * |
||
594 | * @param string ... |
||
595 | * (optional) Additional command line arguments. |
||
596 | * @param array $options |
||
597 | * (optional) An associative array of command line options. |
||
598 | * |
||
599 | * @return GitWorkingCopy |
||
600 | * |
||
601 | * @throws GitException |
||
602 | */ |
||
603 | 4 | public function apply() |
|
609 | |||
610 | /** |
||
611 | * Executes a `git bisect` command. |
||
612 | * |
||
613 | * Find by binary search the change that introduced a bug. |
||
614 | * |
||
615 | * @code |
||
616 | * $git->bisect('good', '2.6.13-rc2'); |
||
617 | * $git->bisect('view', array('stat' => true)); |
||
618 | * @endcode |
||
619 | * |
||
620 | * @param string $sub_command |
||
621 | * The subcommand passed to `git bisect`. |
||
622 | * @param string ... |
||
623 | * (optional) Additional command line arguments. |
||
624 | * @param array $options |
||
625 | * (optional) An associative array of command line options. |
||
626 | * |
||
627 | * @return \GitWrapper\GitWorkingCopy |
||
628 | * |
||
629 | * @throws \GitWrapper\GitException |
||
630 | */ |
||
631 | 4 | public function bisect($sub_command) |
|
637 | |||
638 | /** |
||
639 | * Executes a `git branch` command. |
||
640 | * |
||
641 | * List, create, or delete branches. |
||
642 | * |
||
643 | * @code |
||
644 | * $git->branch('my2.6.14', 'v2.6.14'); |
||
645 | * $git->branch('origin/html', 'origin/man', array('d' => true, 'r' => 'origin/todo')); |
||
646 | * @endcode |
||
647 | * |
||
648 | * @param string ... |
||
649 | * (optional) Additional command line arguments. |
||
650 | * @param array $options |
||
651 | * (optional) An associative array of command line options. |
||
652 | * |
||
653 | * @return \GitWrapper\GitWorkingCopy |
||
654 | * |
||
655 | * @throws \GitWrapper\GitException |
||
656 | */ |
||
657 | 32 | public function branch() |
|
663 | |||
664 | /** |
||
665 | * Executes a `git checkout` command. |
||
666 | * |
||
667 | * Checkout a branch or paths to the working tree. |
||
668 | * |
||
669 | * @code |
||
670 | * $git->checkout('new-branch', array('b' => true)); |
||
671 | * @endcode |
||
672 | * |
||
673 | * @param string ... |
||
674 | * (optional) Additional command line arguments. |
||
675 | * @param array $options |
||
676 | * (optional) An associative array of command line options. |
||
677 | * |
||
678 | * @return \GitWrapper\GitWorkingCopy |
||
679 | * |
||
680 | * @throws \GitWrapper\GitException |
||
681 | */ |
||
682 | 200 | public function checkout() |
|
688 | |||
689 | /** |
||
690 | * Executes a `git clone` command. |
||
691 | * |
||
692 | * Clone a repository into a new directory. Use GitWorkingCopy::clone() |
||
693 | * instead for more readable code. |
||
694 | * |
||
695 | * @code |
||
696 | * $git->clone('git://github.com/cpliakas/git-wrapper.git'); |
||
697 | * @endcode |
||
698 | * |
||
699 | * @param string $repository |
||
700 | * The Git URL of the repository being cloned. |
||
701 | * @param array $options |
||
702 | * (optional) An associative array of command line options. |
||
703 | * |
||
704 | * @param string $repository |
||
705 | * The URL of the repository being cloned. |
||
706 | * |
||
707 | * @return \GitWrapper\GitWorkingCopy |
||
708 | * |
||
709 | * @throws \GitWrapper\GitException |
||
710 | */ |
||
711 | 204 | public function cloneRepository($repository, $options = array()) |
|
712 | { |
||
713 | $args = array( |
||
714 | 204 | 'clone', |
|
715 | 204 | $repository, |
|
716 | 204 | $this->directory, |
|
717 | 204 | $options, |
|
718 | 204 | ); |
|
719 | 204 | return $this->run($args, false); |
|
720 | } |
||
721 | |||
722 | /** |
||
723 | * Executes a `git commit` command. |
||
724 | * |
||
725 | * Record changes to the repository. If only one argument is passed, it is |
||
726 | * assumed to be the commit message. Therefore `$git->commit('Message');` |
||
727 | * yields a `git commit -am "Message"` command. |
||
728 | * |
||
729 | * @code |
||
730 | * $git->commit('My commit message'); |
||
731 | * $git->commit('Makefile', array('m' => 'My commit message')); |
||
732 | * @endcode |
||
733 | * |
||
734 | * @param string ... |
||
735 | * (optional) Additional command line arguments. |
||
736 | * @param array $options |
||
737 | * (optional) An associative array of command line options. |
||
738 | * |
||
739 | * @return \GitWrapper\GitWorkingCopy |
||
740 | * |
||
741 | * @throws \GitWrapper\GitException |
||
742 | */ |
||
743 | 204 | public function commit() |
|
744 | { |
||
745 | 204 | $args = func_get_args(); |
|
746 | 204 | if (isset($args[0]) && is_string($args[0]) && !isset($args[1])) { |
|
747 | 204 | $args[0] = array( |
|
748 | 204 | 'm' => $args[0], |
|
749 | 204 | 'a' => true, |
|
750 | ); |
||
751 | 204 | } |
|
752 | 204 | array_unshift($args, 'commit'); |
|
753 | 204 | return $this->run($args); |
|
754 | } |
||
755 | |||
756 | /** |
||
757 | * Executes a `git config` command. |
||
758 | * |
||
759 | * Get and set repository options. |
||
760 | * |
||
761 | * @code |
||
762 | * $git->config('user.email', '[email protected]'); |
||
763 | * $git->config('user.name', 'Chris Pliakas'); |
||
764 | * @endcode |
||
765 | * |
||
766 | * @param string ... |
||
767 | * (optional) Additional command line arguments. |
||
768 | * @param array $options |
||
769 | * (optional) An associative array of command line options. |
||
770 | * |
||
771 | * @return \GitWrapper\GitWorkingCopy |
||
772 | * |
||
773 | * @throws \GitWrapper\GitException |
||
774 | */ |
||
775 | 200 | public function config() |
|
781 | |||
782 | /** |
||
783 | * Executes a `git diff` command. |
||
784 | * |
||
785 | * Show changes between commits, commit and working tree, etc. |
||
786 | * |
||
787 | * @code |
||
788 | * $git->diff(); |
||
789 | * $git->diff('topic', 'master'); |
||
790 | * @endcode |
||
791 | * |
||
792 | * @param string ... |
||
793 | * (optional) Additional command line arguments. |
||
794 | * @param array $options |
||
795 | * (optional) An associative array of command line options. |
||
796 | * |
||
797 | * @return \GitWrapper\GitWorkingCopy |
||
798 | * |
||
799 | * @throws \GitWrapper\GitException |
||
800 | */ |
||
801 | 4 | public function diff() |
|
807 | |||
808 | /** |
||
809 | * Executes a `git fetch` command. |
||
810 | * |
||
811 | * Download objects and refs from another repository. |
||
812 | * |
||
813 | * @code |
||
814 | * $git->fetch('origin'); |
||
815 | * $git->fetch(array('all' => true)); |
||
816 | * @endcode |
||
817 | * |
||
818 | * @param string ... |
||
819 | * (optional) Additional command line arguments. |
||
820 | * @param array $options |
||
821 | * (optional) An associative array of command line options. |
||
822 | * |
||
823 | * @return \GitWrapper\GitWorkingCopy |
||
824 | * |
||
825 | * @throws \GitWrapper\GitException |
||
826 | */ |
||
827 | 4 | public function fetch() |
|
833 | |||
834 | /** |
||
835 | * Executes a `git grep` command. |
||
836 | * |
||
837 | * Print lines matching a pattern. |
||
838 | * |
||
839 | * @code |
||
840 | * $git->grep('time_t', '--', '*.[ch]'); |
||
841 | * @endcode |
||
842 | * |
||
843 | * @param string ... |
||
844 | * (optional) Additional command line arguments. |
||
845 | * @param array $options |
||
846 | * (optional) An associative array of command line options. |
||
847 | * |
||
848 | * @return \GitWrapper\GitWorkingCopy |
||
849 | * |
||
850 | * @throws \GitWrapper\GitException |
||
851 | */ |
||
852 | 4 | public function grep() |
|
858 | |||
859 | /** |
||
860 | * Executes a `git init` command. |
||
861 | * |
||
862 | * Create an empty git repository or reinitialize an existing one. |
||
863 | * |
||
864 | * @code |
||
865 | * $git->init(array('bare' => true)); |
||
866 | * @endcode |
||
867 | * |
||
868 | * @param array $options |
||
869 | * (optional) An associative array of command line options. |
||
870 | * |
||
871 | * @return \GitWrapper\GitWorkingCopy |
||
872 | * |
||
873 | * @throws \GitWrapper\GitException |
||
874 | */ |
||
875 | 204 | public function init(array $options = array()) |
|
876 | { |
||
877 | $args = array( |
||
878 | 204 | 'init', |
|
879 | 204 | $this->directory, |
|
880 | 204 | $options, |
|
881 | 204 | ); |
|
882 | 204 | return $this->run($args, false); |
|
883 | } |
||
884 | |||
885 | /** |
||
886 | * Executes a `git log` command. |
||
887 | * |
||
888 | * Show commit logs. |
||
889 | * |
||
890 | * @code |
||
891 | * $git->log(array('no-merges' => true)); |
||
892 | * $git->log('v2.6.12..', 'include/scsi', 'drivers/scsi'); |
||
893 | * @endcode |
||
894 | * |
||
895 | * @param string ... |
||
896 | * (optional) Additional command line arguments. |
||
897 | * @param array $options |
||
898 | * (optional) An associative array of command line options. |
||
899 | * |
||
900 | * @return \GitWrapper\GitWorkingCopy |
||
901 | * |
||
902 | * @throws \GitWrapper\GitException |
||
903 | */ |
||
904 | 8 | public function log() |
|
910 | |||
911 | /** |
||
912 | * Executes a `git merge` command. |
||
913 | * |
||
914 | * Join two or more development histories together. |
||
915 | * |
||
916 | * @code |
||
917 | * $git->merge('fixes', 'enhancements'); |
||
918 | * @endcode |
||
919 | * |
||
920 | * @param string ... |
||
921 | * (optional) Additional command line arguments. |
||
922 | * @param array $options |
||
923 | * (optional) An associative array of command line options. |
||
924 | * |
||
925 | * @return \GitWrapper\GitWorkingCopy |
||
926 | * |
||
927 | * @throws \GitWrapper\GitException |
||
928 | */ |
||
929 | 8 | public function merge() |
|
935 | |||
936 | /** |
||
937 | * Executes a `git mv` command. |
||
938 | * |
||
939 | * Move or rename a file, a directory, or a symlink. |
||
940 | * |
||
941 | * @code |
||
942 | * $git->mv('orig.txt', 'dest.txt'); |
||
943 | * @endcode |
||
944 | * |
||
945 | * @param string $source |
||
946 | * The file / directory being moved. |
||
947 | * @param string $destination |
||
948 | * The target file / directory that the source is being move to. |
||
949 | * @param array $options |
||
950 | * (optional) An associative array of command line options. |
||
951 | * |
||
952 | * @return \GitWrapper\GitWorkingCopy |
||
953 | * |
||
954 | * @throws \GitWrapper\GitException |
||
955 | */ |
||
956 | 4 | public function mv($source, $destination, array $options = array()) |
|
966 | |||
967 | /** |
||
968 | * Executes a `git pull` command. |
||
969 | * |
||
970 | * Fetch from and merge with another repository or a local branch. |
||
971 | * |
||
972 | * @code |
||
973 | * $git->pull('upstream', 'master'); |
||
974 | * @endcode |
||
975 | * |
||
976 | * @param string ... |
||
977 | * (optional) Additional command line arguments. |
||
978 | * @param array $options |
||
979 | * (optional) An associative array of command line options. |
||
980 | * |
||
981 | * @return \GitWrapper\GitWorkingCopy |
||
982 | * |
||
983 | * @throws \GitWrapper\GitException |
||
984 | */ |
||
985 | 4 | public function pull() |
|
991 | |||
992 | /** |
||
993 | * Executes a `git push` command. |
||
994 | * |
||
995 | * Update remote refs along with associated objects. |
||
996 | * |
||
997 | * @code |
||
998 | * $git->push('upstream', 'master'); |
||
999 | * @endcode |
||
1000 | * |
||
1001 | * @param string ... |
||
1002 | * (optional) Additional command line arguments. |
||
1003 | * @param array $options |
||
1004 | * (optional) An associative array of command line options. |
||
1005 | * |
||
1006 | * @return \GitWrapper\GitWorkingCopy |
||
1007 | * |
||
1008 | * @throws \GitWrapper\GitException |
||
1009 | */ |
||
1010 | 200 | public function push() |
|
1011 | { |
||
1012 | 200 | $args = func_get_args(); |
|
1013 | 200 | array_unshift($args, 'push'); |
|
1014 | 200 | return $this->run($args); |
|
1015 | } |
||
1016 | |||
1017 | /** |
||
1018 | * Executes a `git rebase` command. |
||
1019 | * |
||
1020 | * Forward-port local commits to the updated upstream head. |
||
1021 | * |
||
1022 | * @code |
||
1023 | * $git->rebase('subsystem@{1}', array('onto' => 'subsystem')); |
||
1024 | * @endcode |
||
1025 | * |
||
1026 | * @param string ... |
||
1027 | * (optional) Additional command line arguments. |
||
1028 | * @param array $options |
||
1029 | * (optional) An associative array of command line options. |
||
1030 | * |
||
1031 | * @return \GitWrapper\GitWorkingCopy |
||
1032 | * |
||
1033 | * @throws \GitWrapper\GitException |
||
1034 | */ |
||
1035 | 4 | public function rebase() |
|
1041 | |||
1042 | /** |
||
1043 | * Executes a `git remote` command. |
||
1044 | * |
||
1045 | * Manage the set of repositories ("remotes") whose branches you track. |
||
1046 | * |
||
1047 | * @code |
||
1048 | * $git->remote('add', 'upstream', 'git://github.com/cpliakas/git-wrapper.git'); |
||
1049 | * @endcode |
||
1050 | * |
||
1051 | * @param string ... |
||
1052 | * (optional) Additional command line arguments. |
||
1053 | * @param array $options |
||
1054 | * (optional) An associative array of command line options. |
||
1055 | * |
||
1056 | * @return \GitWrapper\GitWorkingCopy |
||
1057 | * |
||
1058 | * @throws \GitWrapper\GitException |
||
1059 | */ |
||
1060 | 60 | public function remote() |
|
1066 | |||
1067 | /** |
||
1068 | * Executes a `git reset` command. |
||
1069 | * |
||
1070 | * Reset current HEAD to the specified state. |
||
1071 | * |
||
1072 | * @code |
||
1073 | * $git->reset(array('hard' => true)); |
||
1074 | * @endcode |
||
1075 | * |
||
1076 | * @param string ... |
||
1077 | * (optional) Additional command line arguments. |
||
1078 | * @param array $options |
||
1079 | * (optional) An associative array of command line options. |
||
1080 | * |
||
1081 | * @return \GitWrapper\GitWorkingCopy |
||
1082 | * |
||
1083 | * @throws \GitWrapper\GitException |
||
1084 | */ |
||
1085 | 16 | public function reset() |
|
1091 | |||
1092 | /** |
||
1093 | * Executes a `git rm` command. |
||
1094 | * |
||
1095 | * Remove files from the working tree and from the index. |
||
1096 | * |
||
1097 | * @code |
||
1098 | * $git->rm('oldfile.txt'); |
||
1099 | * @endcode |
||
1100 | * |
||
1101 | * @param string $filepattern |
||
1102 | * Files to remove from version control. Fileglobs (e.g. *.c) can be |
||
1103 | * given to add all matching files. Also a leading directory name (e.g. |
||
1104 | * dir to add dir/file1 and dir/file2) can be given to add all files in |
||
1105 | * the directory, recursively. |
||
1106 | * @param array $options |
||
1107 | * (optional) An associative array of command line options. |
||
1108 | * |
||
1109 | * @return \GitWrapper\GitWorkingCopy |
||
1110 | * |
||
1111 | * @throws \GitWrapper\GitException |
||
1112 | */ |
||
1113 | 4 | public function rm($filepattern, array $options = array()) |
|
1122 | |||
1123 | /** |
||
1124 | * Executes a `git show` command. |
||
1125 | * |
||
1126 | * Show various types of objects. |
||
1127 | * |
||
1128 | * @code |
||
1129 | * $git->show('v1.0.0'); |
||
1130 | * @endcode |
||
1131 | * |
||
1132 | * @param string $object |
||
1133 | * The names of objects to show. For a more complete list of ways to spell |
||
1134 | * object names, see "SPECIFYING REVISIONS" section in gitrevisions(7). |
||
1135 | * @param array $options |
||
1136 | * (optional) An associative array of command line options. |
||
1137 | * |
||
1138 | * @return \GitWrapper\GitWorkingCopy |
||
1139 | * |
||
1140 | * @throws \GitWrapper\GitException |
||
1141 | */ |
||
1142 | 4 | public function show($object, array $options = array()) |
|
1147 | |||
1148 | /** |
||
1149 | * Executes a `git status` command. |
||
1150 | * |
||
1151 | * Show the working tree status. |
||
1152 | * |
||
1153 | * @code |
||
1154 | * $git->status(array('s' => true)); |
||
1155 | * @endcode |
||
1156 | * |
||
1157 | * @param string ... |
||
1158 | * (optional) Additional command line arguments. |
||
1159 | * @param array $options |
||
1160 | * (optional) An associative array of command line options. |
||
1161 | * |
||
1162 | * @return \GitWrapper\GitWorkingCopy |
||
1163 | * |
||
1164 | * @throws \GitWrapper\GitException |
||
1165 | */ |
||
1166 | 20 | public function status() |
|
1172 | |||
1173 | /** |
||
1174 | * Executes a `git tag` command. |
||
1175 | * |
||
1176 | * Create, list, delete or verify a tag object signed with GPG. |
||
1177 | |||
1178 | * @code |
||
1179 | * $git->tag('v1.0.0'); |
||
1180 | * @endcode |
||
1181 | * |
||
1182 | * @param string ... |
||
1183 | * (optional) Additional command line arguments. |
||
1184 | * @param array $options |
||
1185 | * (optional) An associative array of command line options. |
||
1186 | * |
||
1187 | * @return \GitWrapper\GitWorkingCopy |
||
1188 | * |
||
1189 | * @throws \GitWrapper\GitException |
||
1190 | */ |
||
1191 | 200 | public function tag() |
|
1197 | |||
1198 | /** |
||
1199 | * Executes a `git clean` command. |
||
1200 | * |
||
1201 | * Remove untracked files from the working tree |
||
1202 | * |
||
1203 | * @code |
||
1204 | * $git->clean('-d', '-f'); |
||
1205 | * @endcode |
||
1206 | * |
||
1207 | * @param string ... |
||
1208 | * (optional) Additional command line arguments. |
||
1209 | * @param array $options |
||
1210 | * (optional) An associative array of command line options. |
||
1211 | * |
||
1212 | * @return \GitWrapper\GitWorkingCopy |
||
1213 | * |
||
1214 | * @throws \GitWrapper\GitException |
||
1215 | */ |
||
1216 | 4 | public function clean() |
|
1222 | |||
1223 | /** |
||
1224 | * Executes a `git archive` command. |
||
1225 | * |
||
1226 | * Create an archive of files from a named tree |
||
1227 | * |
||
1228 | * @code |
||
1229 | * $git->archive('HEAD', array('o' => '/path/to/archive')); |
||
1230 | * @endcode |
||
1231 | * |
||
1232 | * @param string ... |
||
1233 | * (optional) Additional command line arguments. |
||
1234 | * @param array $options |
||
1235 | * (optional) An associative array of command line options. |
||
1236 | * |
||
1237 | * @return \GitWrapper\GitWorkingCopy |
||
1238 | * |
||
1239 | * @throws \GitWrapper\GitException |
||
1240 | */ |
||
1241 | 4 | public function archive() |
|
1247 | |||
1248 | /** |
||
1249 | * @} End of "defgroup command". |
||
1250 | */ |
||
1251 | |||
1252 | /** |
||
1253 | * Hackish, allows us to use "clone" as a method name. |
||
1254 | * |
||
1255 | * $throws \BadMethodCallException |
||
1256 | * @throws \GitWrapper\GitException |
||
1257 | */ |
||
1258 | 204 | View Code Duplication | public function __call($method, $args) |
1259 | { |
||
1260 | 204 | if ('clone' == $method) { |
|
1261 | 204 | return call_user_func_array(array($this, 'cloneRepository'), $args); |
|
1262 | } else { |
||
1263 | 4 | $class = get_called_class(); |
|
1264 | 4 | $message = "Call to undefined method $class::$method()"; |
|
1265 | 4 | throw new \BadMethodCallException($message); |
|
1266 | } |
||
1267 | } |
||
1268 | |||
1269 | /** |
||
1270 | * Gets the output captured by the last run Git commnd(s). |
||
1271 | * |
||
1272 | * @return string |
||
1273 | * |
||
1274 | * @see GitWorkingCopy::getOutput() |
||
1275 | */ |
||
1276 | 112 | public function __toString() |
|
1280 | } |
||
1281 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.