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 |
||
| 16 | final class GitWorkingCopy |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * A boolean flagging whether the repository is cloned. |
||
| 20 | * |
||
| 21 | * If the variable is null, the a rudimentary check will be performed to see |
||
| 22 | * if the directory looks like it is a working copy. |
||
| 23 | * |
||
| 24 | * @var bool|null |
||
| 25 | */ |
||
| 26 | private $cloned; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Path to the directory containing the working copy. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $directory; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The GitWrapper object that likely instantiated this class. |
||
| 37 | * |
||
| 38 | * @var GitWrapper |
||
| 39 | */ |
||
| 40 | private $gitWrapper; |
||
| 41 | |||
| 42 | public function __construct(GitWrapper $gitWrapper, string $directory) |
||
| 47 | |||
| 48 | public function getWrapper(): GitWrapper |
||
| 52 | |||
| 53 | public function getDirectory(): string |
||
| 57 | |||
| 58 | public function setCloned(bool $cloned): void |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Checks whether a repository has already been cloned to this directory. |
||
| 65 | * |
||
| 66 | * If the flag is not set, test if it looks like we're at a git directory. |
||
| 67 | */ |
||
| 68 | public function isCloned(): bool |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Runs a Git command and returns the output. |
||
| 84 | * |
||
| 85 | * @param mixed[] $argsAndOptions |
||
| 86 | */ |
||
| 87 | public function run(string $command, array $argsAndOptions = [], bool $setDirectory = true): string |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the output of a `git status -s` command. |
||
| 99 | */ |
||
| 100 | public function getStatus(): string |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns true if there are changes to commit. |
||
| 107 | */ |
||
| 108 | public function hasChanges(): bool |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns whether HEAD has a remote tracking branch. |
||
| 116 | */ |
||
| 117 | public function isTracking(): bool |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns whether HEAD is up-to-date with its remote tracking branch. |
||
| 130 | */ |
||
| 131 | public function isUpToDate(): bool |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Returns whether HEAD is ahead of its remote tracking branch. |
||
| 146 | * |
||
| 147 | * If this returns true it means that commits are present locally which have |
||
| 148 | * not yet been pushed to the remote. |
||
| 149 | */ |
||
| 150 | public function isAhead(): bool |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns whether HEAD is behind its remote tracking branch. |
||
| 164 | * |
||
| 165 | * If this returns true it means that a pull is needed to bring the branch |
||
| 166 | * up-to-date with the remote. |
||
| 167 | */ |
||
| 168 | public function isBehind(): bool |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Returns whether HEAD needs to be merged with its remote tracking branch. |
||
| 182 | * |
||
| 183 | * If this returns true it means that HEAD has diverged from its remote |
||
| 184 | * tracking branch; new commits are present locally as well as on the |
||
| 185 | * remote. |
||
| 186 | */ |
||
| 187 | public function needsMerge(): bool |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns a GitBranches object containing information on the repository's |
||
| 201 | * branches. |
||
| 202 | */ |
||
| 203 | public function getBranches(): GitBranches |
||
| 207 | |||
| 208 | /** |
||
| 209 | * This is synonymous with `git push origin tag v1.2.3`. |
||
| 210 | * |
||
| 211 | * @param string $repository The destination of the push operation, which is either a URL or name of |
||
| 212 | * the remote. Defaults to "origin". |
||
| 213 | * @param mixed[] $options |
||
| 214 | */ |
||
| 215 | public function pushTag(string $tag, string $repository = 'origin', array $options = []): string |
||
| 219 | |||
| 220 | /** |
||
| 221 | * This is synonymous with `git push --tags origin`. |
||
| 222 | * |
||
| 223 | * @param string $repository The destination of the push operation, which is either a URL or name of the remote. |
||
| 224 | * @param mixed[] $options |
||
| 225 | */ |
||
| 226 | public function pushTags(string $repository = 'origin', array $options = []): string |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Fetches all remotes. |
||
| 234 | * |
||
| 235 | * This is synonymous with `git fetch --all`. |
||
| 236 | * |
||
| 237 | * @param mixed[] $options |
||
| 238 | */ |
||
| 239 | public function fetchAll(array $options = []): string |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Create a new branch and check it out. |
||
| 247 | * |
||
| 248 | * This is synonymous with `git checkout -b`. |
||
| 249 | * |
||
| 250 | * @param mixed[] $options |
||
| 251 | */ |
||
| 252 | public function checkoutNewBranch(string $branch, array $options = []): string |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds a remote to the repository. |
||
| 260 | * |
||
| 261 | * @param mixed[] $options An associative array of options, with the following keys: |
||
| 262 | * - -f: Boolean, set to true to run git fetch immediately after the |
||
| 263 | * remote is set up. Defaults to false. |
||
| 264 | * - --tags: Boolean. By default only the tags from the fetched branches |
||
| 265 | * are imported when git fetch is run. Set this to true to import every |
||
| 266 | * tag from the remote repository. Defaults to false. |
||
| 267 | * - --no-tags: Boolean, when set to true, git fetch does not import tags |
||
| 268 | * from the remote repository. Defaults to false. |
||
| 269 | * - -t: Optional array of branch names to track. If left empty, all |
||
| 270 | * branches will be tracked. |
||
| 271 | * - -m: Optional name of the master branch to track. This will set up a |
||
| 272 | * symbolic ref 'refs/remotes/<name>/HEAD which points at the specified |
||
| 273 | * master branch on the remote. When omitted, no symbolic ref will be |
||
| 274 | * created. |
||
| 275 | */ |
||
| 276 | public function addRemote(string $name, string $url, array $options = []): string |
||
| 309 | |||
| 310 | public function removeRemote(string $name): string |
||
| 314 | |||
| 315 | public function hasRemote(string $name): bool |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return string[] An associative array with the following keys: |
||
| 322 | * - fetch: the fetch URL. |
||
| 323 | * - push: the push URL. |
||
| 324 | */ |
||
| 325 | public function getRemote(string $name): array |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @return string[][] An associative array, keyed by remote name, containing an associative array with keys |
||
| 336 | * - fetch: the fetch URL. |
||
| 337 | * - push: the push URL. |
||
| 338 | */ |
||
| 339 | public function getRemotes(): array |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the fetch or push URL of a given remote. |
||
| 357 | * |
||
| 358 | * @param string $operation The operation for which to return the remote. Can be either 'fetch' or 'push'. |
||
| 359 | */ |
||
| 360 | public function getRemoteUrl(string $remote, string $operation = 'fetch'): string |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @code $git->add('some/file.txt'); |
||
| 373 | * |
||
| 374 | * @param mixed[] $options |
||
| 375 | */ |
||
| 376 | public function add(string $filepattern, array $options = []): string |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @code $git->apply('the/file/to/read/the/patch/from'); |
||
| 383 | * |
||
| 384 | * @param mixed ...$argsAndOptions |
||
| 385 | */ |
||
| 386 | public function apply(...$argsAndOptions): string |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Find by binary search the change that introduced a bug. |
||
| 393 | * |
||
| 394 | * @code $git->bisect('good', '2.6.13-rc2'); |
||
| 395 | * $git->bisect('view', ['stat' => true]); |
||
| 396 | * |
||
| 397 | * @param mixed ...$argsAndOptions |
||
| 398 | */ |
||
| 399 | public function bisect(...$argsAndOptions): string |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @code $git->branch('my2.6.14', 'v2.6.14'); |
||
| 406 | * $git->branch('origin/html', 'origin/man', ['d' => true, 'r' => 'origin/todo']); |
||
| 407 | * |
||
| 408 | * @param mixed ...$argsAndOptions |
||
| 409 | */ |
||
| 410 | public function branch(...$argsAndOptions): string |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @code $git->checkout('new-branch', ['b' => true]); |
||
| 417 | * |
||
| 418 | * @param mixed ...$argsAndOptions |
||
| 419 | */ |
||
| 420 | public function checkout(...$argsAndOptions): string |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Executes a `git clone` command. |
||
| 427 | * |
||
| 428 | * @code $git->cloneRepository('git://github.com/cpliakas/git-wrapper.git'); |
||
| 429 | * |
||
| 430 | * @param mixed[] $options |
||
| 431 | */ |
||
| 432 | public function cloneRepository(string $repository, array $options = []): string |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Record changes to the repository. If only one argument is passed, it is assumed to be the commit message. |
||
| 440 | * Therefore `$git->commit('Message');` yields a `git commit -am "Message"` command. |
||
| 441 | * |
||
| 442 | * @code $git->commit('My commit message'); |
||
| 443 | * $git->commit('Makefile', ['m' => 'My commit message']); |
||
| 444 | * |
||
| 445 | * @param mixed ...$argsAndOptions |
||
| 446 | */ |
||
| 447 | public function commit(...$argsAndOptions): string |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @code $git->config('user.email', '[email protected]'); |
||
| 461 | * $git->config('user.name', 'Chris Pliakas'); |
||
| 462 | * |
||
| 463 | * @param mixed ...$argsAndOptions |
||
| 464 | */ |
||
| 465 | public function config(...$argsAndOptions): string |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @code $git->diff(); |
||
| 472 | * $git->diff('topic', 'master'); |
||
| 473 | * |
||
| 474 | * @param mixed ...$argsAndOptions |
||
| 475 | */ |
||
| 476 | public function diff(...$argsAndOptions): string |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @code $git->fetch('origin'); |
||
| 483 | * $git->fetch(['all' => true]); |
||
| 484 | * |
||
| 485 | * @api |
||
| 486 | * @param mixed ...$argsAndOptions |
||
| 487 | */ |
||
| 488 | public function fetch(...$argsAndOptions): string |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Print lines matching a pattern. |
||
| 495 | * |
||
| 496 | * @code $git->grep('time_t', '--', '*.[ch]'); |
||
| 497 | * |
||
| 498 | * @param mixed ...$argsAndOptions |
||
| 499 | */ |
||
| 500 | public function grep(...$argsAndOptions): string |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Create an empty git repository or reinitialize an existing one. |
||
| 507 | * |
||
| 508 | * @code $git->init(['bare' => true]); |
||
| 509 | * |
||
| 510 | * @param mixed[] $options |
||
| 511 | */ |
||
| 512 | public function init(array $options = []): string |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @code $git->log(['no-merges' => true]); |
||
| 520 | * $git->log('v2.6.12..', 'include/scsi', 'drivers/scsi'); |
||
| 521 | * |
||
| 522 | * @param mixed ...$argsAndOptions |
||
| 523 | */ |
||
| 524 | public function log(...$argsAndOptions): string |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @code $git->merge('fixes', 'enhancements'); |
||
| 531 | * |
||
| 532 | * @param mixed ...$argsAndOptions |
||
| 533 | */ |
||
| 534 | public function merge(...$argsAndOptions): string |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @code $git->mv('orig.txt', 'dest.txt'); |
||
| 541 | * |
||
| 542 | * @param mixed[] $options |
||
| 543 | */ |
||
| 544 | public function mv(string $source, string $destination, array $options = []): string |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @code $git->pull('upstream', 'master'); |
||
| 552 | * |
||
| 553 | * @param mixed ...$argsAndOptions |
||
| 554 | */ |
||
| 555 | public function pull(...$argsAndOptions): string |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @code $git->push('upstream', 'master'); |
||
| 562 | * |
||
| 563 | * @param mixed ...$argsAndOptions |
||
| 564 | */ |
||
| 565 | public function push(...$argsAndOptions): string |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @code $git->rebase('subsystem@{1}', ['onto' => 'subsystem']); |
||
| 572 | * |
||
| 573 | * @param mixed ...$argsAndOptions |
||
| 574 | */ |
||
| 575 | public function rebase(...$argsAndOptions): string |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @code $git->remote('add', 'upstream', 'git://github.com/cpliakas/git-wrapper.git'); |
||
| 582 | * |
||
| 583 | * @param mixed ...$argsAndOptions |
||
| 584 | */ |
||
| 585 | public function remote(...$argsAndOptions): string |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @code $git->reset(['hard' => true]); |
||
| 592 | * |
||
| 593 | * @param mixed ...$argsAndOptions |
||
| 594 | */ |
||
| 595 | public function reset(...$argsAndOptions): string |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @code $git->rm('oldfile.txt'); |
||
| 602 | * |
||
| 603 | * @param mixed[] $options |
||
| 604 | */ |
||
| 605 | public function rm(string $filepattern, array $options = []): string |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @code $git->show('v1.0.0'); |
||
| 613 | * |
||
| 614 | * @param mixed[] $options |
||
| 615 | */ |
||
| 616 | public function show(string $object, array $options = []): string |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @code $git->status(['s' => true]); |
||
| 624 | * |
||
| 625 | * @param mixed ...$argsAndOptions |
||
| 626 | */ |
||
| 627 | public function status(...$argsAndOptions): string |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @code $git->tag('v1.0.0'); |
||
| 634 | * |
||
| 635 | * @param mixed ...$argsAndOptions |
||
| 636 | */ |
||
| 637 | public function tag(...$argsAndOptions): string |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @code $git->clean('-d', '-f'); |
||
| 644 | * |
||
| 645 | * @param mixed ...$argsAndOptions |
||
| 646 | */ |
||
| 647 | public function clean(...$argsAndOptions): string |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @code $git->archive('HEAD', ['o' => '/path/to/archive']); |
||
| 654 | * |
||
| 655 | * @param mixed ...$argsAndOptions |
||
| 656 | */ |
||
| 657 | public function archive(...$argsAndOptions): string |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @api |
||
| 664 | * Returns a GitTags object containing information on the repository's tags. |
||
| 665 | */ |
||
| 666 | public function tags(): GitTags |
||
| 670 | |||
| 671 | private function ensureAddRemoteArgsAreValid(string $name, string $url): void |
||
| 681 | |||
| 682 | private function splitByNewline(string $string): array |
||
| 686 | } |
||
| 687 |