Complex classes like GitRepo 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 GitRepo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class GitRepo |
||
| 18 | { |
||
| 19 | |||
| 20 | protected $bare = false; |
||
| 21 | protected $envopts = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $repositoryPath = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Create a new git repository |
||
| 30 | * |
||
| 31 | * Accepts a creation path, and, optionally, a source path |
||
| 32 | * |
||
| 33 | * @access public |
||
| 34 | * @param string repository path |
||
| 35 | * @param string directory to source |
||
| 36 | * @param string reference path |
||
| 37 | * @return GitRepo |
||
| 38 | */ |
||
| 39 | public static function &create_new($repositoryPath, $source = null, $remote_source = false, $reference = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Constructor |
||
| 68 | * |
||
| 69 | * Accepts a repository path |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | * @param string repository path |
||
| 73 | * @param bool create if not exists? |
||
| 74 | * @return void |
||
|
|
|||
| 75 | */ |
||
| 76 | public function __construct($repositoryPath = null, $create_new = false, $_init = true) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Set the repository's path |
||
| 85 | * |
||
| 86 | * Accepts the repository path |
||
| 87 | * |
||
| 88 | * @access public |
||
| 89 | * @param string repository path |
||
| 90 | * @param bool create if not exists? |
||
| 91 | * @param bool initialize new Git repo if not exists? |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function set_repositoryPath($repositoryPath, $create_new = false, $_init = true) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the path to the git repo directory (eg. the ".git" directory) |
||
| 143 | * |
||
| 144 | * @access public |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function gitDirectoryPath() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the path to the git repo directory (eg. the ".git" directory) |
||
| 154 | * |
||
| 155 | * @access public |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function git_directory_path() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Tests if git is installed |
||
| 179 | * |
||
| 180 | * @access public |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function test_git() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Run a command in the git repository |
||
| 204 | * |
||
| 205 | * Accepts a shell command to run |
||
| 206 | * |
||
| 207 | * @access protected |
||
| 208 | * @param string command to run |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function run_command($command) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Run a git command in the git repository |
||
| 254 | * |
||
| 255 | * Accepts a git command to run |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @param string command to run |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function run($command) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Runs a 'git status' call |
||
| 268 | * |
||
| 269 | * Accept a convert to HTML bool |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * @param bool return string with <br /> |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function status($html = false) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Runs a `git add` call |
||
| 286 | * |
||
| 287 | * Accepts a list of files to add |
||
| 288 | * |
||
| 289 | * @access public |
||
| 290 | * @param mixed files to add |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function add($files = "*") |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Runs a `git rm` call |
||
| 303 | * |
||
| 304 | * Accepts a list of files to remove |
||
| 305 | * |
||
| 306 | * @access public |
||
| 307 | * @param mixed files to remove |
||
| 308 | * @param Boolean use the --cached flag? |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function rm($files = "*", $cached = false) |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * Runs a `git commit` call |
||
| 322 | * |
||
| 323 | * Accepts a commit message string |
||
| 324 | * |
||
| 325 | * @access public |
||
| 326 | * @param string commit message |
||
| 327 | * @param boolean should all files be committed automatically (-a flag) |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function commit($message = "", $commit_all = true) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Runs a `git clone` call to clone the current repository |
||
| 338 | * into a different directory |
||
| 339 | * |
||
| 340 | * Accepts a target directory |
||
| 341 | * |
||
| 342 | * @access public |
||
| 343 | * @param string target directory |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function clone_to($target) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Runs a `git clone` call to clone a different repository |
||
| 353 | * into the current repository |
||
| 354 | * |
||
| 355 | * Accepts a source directory |
||
| 356 | * |
||
| 357 | * @access public |
||
| 358 | * @param string source directory |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function clone_from($source) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Runs a `git clone` call to clone a remote repository |
||
| 368 | * into the current repository |
||
| 369 | * |
||
| 370 | * Accepts a source url |
||
| 371 | * |
||
| 372 | * @access public |
||
| 373 | * @param string source url |
||
| 374 | * @param string reference path |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function clone_remote($source, $reference) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Runs a `git clean` call |
||
| 384 | * |
||
| 385 | * Accepts a remove directories flag |
||
| 386 | * |
||
| 387 | * @access public |
||
| 388 | * @param bool delete directories? |
||
| 389 | * @param bool force clean? |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function clean($dirs = false, $force = false) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Runs a `git branch` call |
||
| 399 | * |
||
| 400 | * Accepts a name for the branch |
||
| 401 | * |
||
| 402 | * @access public |
||
| 403 | * @param string branch name |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function create_branch($branch) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Runs a `git branch -[d|D]` call |
||
| 413 | * |
||
| 414 | * Accepts a name for the branch |
||
| 415 | * |
||
| 416 | * @access public |
||
| 417 | * @param string branch name |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function delete_branch($branch, $force = false) |
||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Runs a `git branch` call |
||
| 428 | * |
||
| 429 | * @access public |
||
| 430 | * @param bool keep asterisk mark on active branch |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | public function listBranches($keep_asterisk = false) |
||
| 437 | /** |
||
| 438 | * Runs a `git branch` call |
||
| 439 | * |
||
| 440 | * @access public |
||
| 441 | * @param bool keep asterisk mark on active branch |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | public function list_branches($keep_asterisk = false) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Lists remote branches (using `git branch -r`). |
||
| 461 | * |
||
| 462 | * Also strips out the HEAD reference (e.g. "origin/HEAD -> origin/master"). |
||
| 463 | * |
||
| 464 | * @access public |
||
| 465 | * @return array |
||
| 466 | */ |
||
| 467 | public function list_remote_branches() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns name of active branch |
||
| 481 | * |
||
| 482 | * @access public |
||
| 483 | * @param bool keep asterisk mark on branch name |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | public function active_branch($keep_asterisk = false) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Runs a `git checkout` call |
||
| 500 | * |
||
| 501 | * Accepts a name for the branch |
||
| 502 | * |
||
| 503 | * @access public |
||
| 504 | * @param string branch name |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function checkout($branch) |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Runs a `git merge` call |
||
| 515 | * |
||
| 516 | * Accepts a name for the branch to be merged |
||
| 517 | * |
||
| 518 | * @access public |
||
| 519 | * @param string $branch |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function merge($branch) |
||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Runs a git fetch on the current branch |
||
| 530 | * |
||
| 531 | * @access public |
||
| 532 | * @return string |
||
| 533 | */ |
||
| 534 | public function fetch() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Add a new tag on the current position |
||
| 541 | * |
||
| 542 | * Accepts the name for the tag and the message |
||
| 543 | * |
||
| 544 | * @param string $tag |
||
| 545 | * @param string $message |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | public function add_tag($tag, $message = null) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * List all the available repository tags. |
||
| 558 | * |
||
| 559 | * Optionally, accept a shell wildcard pattern and return only tags matching it. |
||
| 560 | * |
||
| 561 | * @access public |
||
| 562 | * @param string $pattern Shell wildcard pattern to match tags against. |
||
| 563 | * @return array Available repository tags. |
||
| 564 | */ |
||
| 565 | public function list_tags($pattern = null) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Push specific branch (or all branches) to a remote |
||
| 580 | * |
||
| 581 | * Accepts the name of the remote and local branch. |
||
| 582 | * If omitted, the command will be "git push", and therefore will take |
||
| 583 | * on the behavior of your "push.defualt" configuration setting. |
||
| 584 | * |
||
| 585 | * @param string $remote |
||
| 586 | * @param string $branch |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | public function push($remote = "", $branch = "") |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Pull specific branch from remote |
||
| 597 | * |
||
| 598 | * Accepts the name of the remote and local branch. |
||
| 599 | * If omitted, the command will be "git pull", and therefore will take on the |
||
| 600 | * behavior as-configured in your clone / environment. |
||
| 601 | * |
||
| 602 | * @param string $remote |
||
| 603 | * @param string $branch |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function pull($remote = "", $branch = "") |
||
| 610 | |||
| 611 | /** |
||
| 612 | * List log entries. |
||
| 613 | * |
||
| 614 | * @param strgin $format |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | public function log($format = null, $fulldiff=false, $filepath=null, $follow=false) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Sets the project description. |
||
| 639 | * |
||
| 640 | * @param string $new |
||
| 641 | */ |
||
| 642 | public function set_description($new) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Gets the project description. |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | public function get_description() |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Sets custom environment options for calling Git |
||
| 661 | * |
||
| 662 | * @param string key |
||
| 663 | * @param string value |
||
| 664 | */ |
||
| 665 | public function setenv($key, $value) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @param string $revision |
||
| 672 | */ |
||
| 673 | public function checkoutForce($revision) |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @return string |
||
| 682 | */ |
||
| 683 | public function getCurrentBranch() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * @param string $from |
||
| 692 | * @param string $to |
||
| 693 | * @return string |
||
| 694 | */ |
||
| 695 | public function getDiff($from, $to) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @return array |
||
| 706 | */ |
||
| 707 | public function getRevisions() |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | public function isWorkingCopyClean() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @param string $command |
||
| 755 | * |
||
| 756 | * @return string |
||
| 757 | * |
||
| 758 | * @throws RuntimeException |
||
| 759 | */ |
||
| 760 | protected function execute($command) |
||
| 776 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.