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 |
||
| 15 | class GitRepo |
||
| 16 | { |
||
| 17 | |||
| 18 | protected $bare = false; |
||
| 19 | protected $envopts = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $repositoryPath = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Create a new git repository |
||
| 28 | * |
||
| 29 | * Accepts a creation path, and, optionally, a source path |
||
| 30 | * |
||
| 31 | * @access public |
||
| 32 | * @param string repository path |
||
| 33 | * @param string directory to source |
||
| 34 | * @param string reference path |
||
| 35 | * @return GitRepo |
||
| 36 | */ |
||
| 37 | public static function &create_new($repositoryPath, $source = null, $remote_source = false, $reference = null) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor |
||
| 66 | * |
||
| 67 | * Accepts a repository path |
||
| 68 | * |
||
| 69 | * @access public |
||
| 70 | * @param string repository path |
||
| 71 | * @param bool create if not exists? |
||
| 72 | * @return void |
||
|
|
|||
| 73 | */ |
||
| 74 | public function __construct($repositoryPath = null, $create_new = false, $_init = true) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the repository's path |
||
| 83 | * |
||
| 84 | * Accepts the repository path |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | * @param string repository path |
||
| 88 | * @param bool create if not exists? |
||
| 89 | * @param bool initialize new Git repo if not exists? |
||
| 90 | * @return void |
||
| 91 | */ |
||
| 92 | public function set_repositoryPath($repositoryPath, $create_new = false, $_init = true) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the path to the git repo directory (eg. the ".git" directory) |
||
| 141 | * |
||
| 142 | * @access public |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function git_directory_path() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Tests if git is installed |
||
| 165 | * |
||
| 166 | * @access public |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function test_git() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Run a command in the git repository |
||
| 190 | * |
||
| 191 | * Accepts a shell command to run |
||
| 192 | * |
||
| 193 | * @access protected |
||
| 194 | * @param string command to run |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function run_command($command) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Run a git command in the git repository |
||
| 240 | * |
||
| 241 | * Accepts a git command to run |
||
| 242 | * |
||
| 243 | * @access public |
||
| 244 | * @param string command to run |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function run($command) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Runs a 'git status' call |
||
| 254 | * |
||
| 255 | * Accept a convert to HTML bool |
||
| 256 | * |
||
| 257 | * @access public |
||
| 258 | * @param bool return string with <br /> |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function status($html = false) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Runs a `git add` call |
||
| 272 | * |
||
| 273 | * Accepts a list of files to add |
||
| 274 | * |
||
| 275 | * @access public |
||
| 276 | * @param mixed files to add |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function add($files = "*") |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Runs a `git rm` call |
||
| 289 | * |
||
| 290 | * Accepts a list of files to remove |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * @param mixed files to remove |
||
| 294 | * @param Boolean use the --cached flag? |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function rm($files = "*", $cached = false) |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * Runs a `git commit` call |
||
| 308 | * |
||
| 309 | * Accepts a commit message string |
||
| 310 | * |
||
| 311 | * @access public |
||
| 312 | * @param string commit message |
||
| 313 | * @param boolean should all files be committed automatically (-a flag) |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function commit($message = "", $commit_all = true) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Runs a `git clone` call to clone the current repository |
||
| 324 | * into a different directory |
||
| 325 | * |
||
| 326 | * Accepts a target directory |
||
| 327 | * |
||
| 328 | * @access public |
||
| 329 | * @param string target directory |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function clone_to($target) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Runs a `git clone` call to clone a different repository |
||
| 339 | * into the current repository |
||
| 340 | * |
||
| 341 | * Accepts a source directory |
||
| 342 | * |
||
| 343 | * @access public |
||
| 344 | * @param string source directory |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function clone_from($source) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Runs a `git clone` call to clone a remote repository |
||
| 354 | * into the current repository |
||
| 355 | * |
||
| 356 | * Accepts a source url |
||
| 357 | * |
||
| 358 | * @access public |
||
| 359 | * @param string source url |
||
| 360 | * @param string reference path |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function clone_remote($source, $reference) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Runs a `git clean` call |
||
| 370 | * |
||
| 371 | * Accepts a remove directories flag |
||
| 372 | * |
||
| 373 | * @access public |
||
| 374 | * @param bool delete directories? |
||
| 375 | * @param bool force clean? |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function clean($dirs = false, $force = false) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Runs a `git branch` call |
||
| 385 | * |
||
| 386 | * Accepts a name for the branch |
||
| 387 | * |
||
| 388 | * @access public |
||
| 389 | * @param string branch name |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function create_branch($branch) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Runs a `git branch -[d|D]` 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 delete_branch($branch, $force = false) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Runs a `git branch` call |
||
| 413 | * |
||
| 414 | * @access public |
||
| 415 | * @param bool keep asterisk mark on active branch |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | public function list_branches($keep_asterisk = false) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Lists remote branches (using `git branch -r`). |
||
| 435 | * |
||
| 436 | * Also strips out the HEAD reference (e.g. "origin/HEAD -> origin/master"). |
||
| 437 | * |
||
| 438 | * @access public |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function list_remote_branches() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns name of active branch |
||
| 455 | * |
||
| 456 | * @access public |
||
| 457 | * @param bool keep asterisk mark on branch name |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function active_branch($keep_asterisk = false) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Runs a `git checkout` call |
||
| 474 | * |
||
| 475 | * Accepts a name for the branch |
||
| 476 | * |
||
| 477 | * @access public |
||
| 478 | * @param string branch name |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function checkout($branch) |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Runs a `git merge` call |
||
| 489 | * |
||
| 490 | * Accepts a name for the branch to be merged |
||
| 491 | * |
||
| 492 | * @access public |
||
| 493 | * @param string $branch |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function merge($branch) |
||
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * Runs a git fetch on the current branch |
||
| 504 | * |
||
| 505 | * @access public |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | public function fetch() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Add a new tag on the current position |
||
| 515 | * |
||
| 516 | * Accepts the name for the tag and the message |
||
| 517 | * |
||
| 518 | * @param string $tag |
||
| 519 | * @param string $message |
||
| 520 | * @return string |
||
| 521 | */ |
||
| 522 | public function add_tag($tag, $message = null) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * List all the available repository tags. |
||
| 532 | * |
||
| 533 | * Optionally, accept a shell wildcard pattern and return only tags matching it. |
||
| 534 | * |
||
| 535 | * @access public |
||
| 536 | * @param string $pattern Shell wildcard pattern to match tags against. |
||
| 537 | * @return array Available repository tags. |
||
| 538 | */ |
||
| 539 | public function list_tags($pattern = null) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Push specific branch (or all branches) to a remote |
||
| 554 | * |
||
| 555 | * Accepts the name of the remote and local branch. |
||
| 556 | * If omitted, the command will be "git push", and therefore will take |
||
| 557 | * on the behavior of your "push.defualt" configuration setting. |
||
| 558 | * |
||
| 559 | * @param string $remote |
||
| 560 | * @param string $branch |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function push($remote = "", $branch = "") |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Pull specific branch from remote |
||
| 571 | * |
||
| 572 | * Accepts the name of the remote and local branch. |
||
| 573 | * If omitted, the command will be "git pull", and therefore will take on the |
||
| 574 | * behavior as-configured in your clone / environment. |
||
| 575 | * |
||
| 576 | * @param string $remote |
||
| 577 | * @param string $branch |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | public function pull($remote = "", $branch = "") |
||
| 584 | |||
| 585 | /** |
||
| 586 | * List log entries. |
||
| 587 | * |
||
| 588 | * @param strgin $format |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | public function log($format = null, $fulldiff=false, $filepath=null, $follow=false) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Sets the project description. |
||
| 613 | * |
||
| 614 | * @param string $new |
||
| 615 | */ |
||
| 616 | public function set_description($new) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Gets the project description. |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function get_description() |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Sets custom environment options for calling Git |
||
| 635 | * |
||
| 636 | * @param string key |
||
| 637 | * @param string value |
||
| 638 | */ |
||
| 639 | public function setenv($key, $value) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param string $revision |
||
| 646 | */ |
||
| 647 | public function checkoutForce($revision) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | public function getCurrentBranch() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param string $from |
||
| 666 | * @param string $to |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | public function getDiff($from, $to) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | public function getRevisions() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @return bool |
||
| 718 | */ |
||
| 719 | public function isWorkingCopyClean() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @param string $command |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | * |
||
| 732 | * @throws RuntimeException |
||
| 733 | */ |
||
| 734 | protected function execute($command) |
||
| 750 | } |
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.