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 declare(strict_types=1); |
||
| 11 | final class GitWorkingCopy |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The GitWrapper object that likely instantiated this class. |
||
| 15 | * |
||
| 16 | * @var GitWrapper |
||
| 17 | */ |
||
| 18 | private $gitWrapper; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Path to the directory containing the working copy. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $directory; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * A boolean flagging whether the repository is cloned. |
||
| 29 | * |
||
| 30 | * If the variable is null, the a rudimentary check will be performed to see |
||
| 31 | * if the directory looks like it is a working copy. |
||
| 32 | * |
||
| 33 | * @var bool|null |
||
| 34 | */ |
||
| 35 | private $cloned; |
||
| 36 | |||
| 37 | public function __construct(GitWrapper $gitWrapper, string $directory) |
||
| 42 | |||
| 43 | public function getWrapper(): GitWrapper |
||
| 47 | |||
| 48 | public function getDirectory(): string |
||
| 52 | |||
| 53 | public function setCloned(bool $cloned): void |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Checks whether a repository has already been cloned to this directory. |
||
| 60 | * |
||
| 61 | * If the flag is not set, test if it looks like we're at a git directory. |
||
| 62 | */ |
||
| 63 | public function isCloned(): bool |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Runs a Git command and returns the output. |
||
| 79 | * |
||
| 80 | * @param mixed[] $argsAndOptions |
||
| 81 | */ |
||
| 82 | public function run(string $command, array $argsAndOptions = [], bool $setDirectory = true, ?string $input = null): string |
||
| 83 | { |
||
| 84 | $command = new GitCommand($command, ...$argsAndOptions); |
||
| 85 | if ($setDirectory) { |
||
| 86 | $command->setDirectory($this->directory); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $this->gitWrapper->run($command, null, $input); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the output of a `git status -s` command. |
||
| 94 | */ |
||
| 95 | public function getStatus(): string |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns true if there are changes to commit. |
||
| 102 | */ |
||
| 103 | public function hasChanges(): bool |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns whether HEAD has a remote tracking branch. |
||
| 111 | */ |
||
| 112 | public function isTracking(): bool |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns whether HEAD is up-to-date with its remote tracking branch. |
||
| 125 | */ |
||
| 126 | public function isUpToDate(): bool |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns whether HEAD is ahead of its remote tracking branch. |
||
| 141 | * |
||
| 142 | * If this returns true it means that commits are present locally which have |
||
| 143 | * not yet been pushed to the remote. |
||
| 144 | */ |
||
| 145 | public function isAhead(): bool |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns whether HEAD is behind its remote tracking branch. |
||
| 159 | * |
||
| 160 | * If this returns true it means that a pull is needed to bring the branch |
||
| 161 | * up-to-date with the remote. |
||
| 162 | */ |
||
| 163 | public function isBehind(): bool |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns whether HEAD needs to be merged with its remote tracking branch. |
||
| 177 | * |
||
| 178 | * If this returns true it means that HEAD has diverged from its remote |
||
| 179 | * tracking branch; new commits are present locally as well as on the |
||
| 180 | * remote. |
||
| 181 | */ |
||
| 182 | public function needsMerge(): bool |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns a GitBranches object containing information on the repository's |
||
| 196 | * branches. |
||
| 197 | */ |
||
| 198 | public function getBranches(): GitBranches |
||
| 202 | |||
| 203 | /** |
||
| 204 | * This is synonymous with `git push origin tag v1.2.3`. |
||
| 205 | * |
||
| 206 | * @param string $repository The destination of the push operation, which is either a URL or name of |
||
| 207 | * the remote. Defaults to "origin". |
||
| 208 | * @param mixed[] $options |
||
| 209 | */ |
||
| 210 | public function pushTag(string $tag, string $repository = 'origin', array $options = []): string |
||
| 214 | |||
| 215 | /** |
||
| 216 | * This is synonymous with `git push --tags origin`. |
||
| 217 | * |
||
| 218 | * @param string $repository The destination of the push operation, which is either a URL or name of the remote. |
||
| 219 | * @param mixed[] $options |
||
| 220 | */ |
||
| 221 | public function pushTags(string $repository = 'origin', array $options = []): string |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Fetches all remotes. |
||
| 229 | * |
||
| 230 | * This is synonymous with `git fetch --all`. |
||
| 231 | * |
||
| 232 | * @param mixed[] $options |
||
| 233 | */ |
||
| 234 | public function fetchAll(array $options = []): string |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Create a new branch and check it out. |
||
| 242 | * |
||
| 243 | * This is synonymous with `git checkout -b`. |
||
| 244 | * |
||
| 245 | * @param mixed[] $options |
||
| 246 | */ |
||
| 247 | public function checkoutNewBranch(string $branch, array $options = []): string |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Adds a remote to the repository. |
||
| 255 | * |
||
| 256 | * @param mixed[] $options An associative array of options, with the following keys: |
||
| 257 | * - -f: Boolean, set to true to run git fetch immediately after the |
||
| 258 | * remote is set up. Defaults to false. |
||
| 259 | * - --tags: Boolean. By default only the tags from the fetched branches |
||
| 260 | * are imported when git fetch is run. Set this to true to import every |
||
| 261 | * tag from the remote repository. Defaults to false. |
||
| 262 | * - --no-tags: Boolean, when set to true, git fetch does not import tags |
||
| 263 | * from the remote repository. Defaults to false. |
||
| 264 | * - -t: Optional array of branch names to track. If left empty, all |
||
| 265 | * branches will be tracked. |
||
| 266 | * - -m: Optional name of the master branch to track. This will set up a |
||
| 267 | * symbolic ref 'refs/remotes/<name>/HEAD which points at the specified |
||
| 268 | * master branch on the remote. When omitted, no symbolic ref will be |
||
| 269 | * created. |
||
| 270 | */ |
||
| 271 | public function addRemote(string $name, string $url, array $options = []): string |
||
| 301 | |||
| 302 | public function removeRemote(string $name): string |
||
| 306 | |||
| 307 | public function hasRemote(string $name): bool |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return string[] An associative array with the following keys: |
||
| 314 | * - fetch: the fetch URL. |
||
| 315 | * - push: the push URL. |
||
| 316 | */ |
||
| 317 | public function getRemote(string $name): array |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return mixed[] An associative array, keyed by remote name, containing an associative array with keys: |
||
| 328 | * - fetch: the fetch URL. |
||
| 329 | * - push: the push URL. |
||
| 330 | */ |
||
| 331 | public function getRemotes(): array |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the fetch or push URL of a given remote. |
||
| 349 | * |
||
| 350 | * @param string $operation The operation for which to return the remote. Can be either 'fetch' or 'push'. |
||
| 351 | */ |
||
| 352 | public function getRemoteUrl(string $remote, string $operation = 'fetch'): string |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @code $git->add('some/file.txt'); |
||
| 365 | * |
||
| 366 | * @param mixed[] $options |
||
| 367 | */ |
||
| 368 | public function add(string $filepattern, array $options = []): string |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @code $git->apply('the/file/to/read/the/patch/from'); |
||
| 375 | * |
||
| 376 | * @param mixed ...$argsAndOptions |
||
| 377 | */ |
||
| 378 | public function apply(...$argsAndOptions): string |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @code $git->applyRaw('raw-diff-content'); |
||
| 385 | * |
||
| 386 | * @param mixed ...$argsAndOptions |
||
| 387 | */ |
||
| 388 | public function applyRaw(string $patch, ...$argsAndOptions): string |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Find by binary search the change that introduced a bug. |
||
| 395 | * |
||
| 396 | * @code $git->bisect('good', '2.6.13-rc2'); |
||
| 397 | * $git->bisect('view', ['stat' => true]); |
||
| 398 | * |
||
| 399 | * @param mixed ...$argsAndOptions |
||
| 400 | */ |
||
| 401 | public function bisect(...$argsAndOptions): string |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @code $git->branch('my2.6.14', 'v2.6.14'); |
||
| 408 | * $git->branch('origin/html', 'origin/man', ['d' => true, 'r' => 'origin/todo']); |
||
| 409 | * |
||
| 410 | * @param mixed ...$argsAndOptions |
||
| 411 | */ |
||
| 412 | public function branch(...$argsAndOptions): string |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @code $git->checkout('new-branch', ['b' => true]); |
||
| 419 | * |
||
| 420 | * @param mixed ...$argsAndOptions |
||
| 421 | */ |
||
| 422 | public function checkout(...$argsAndOptions): string |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Executes a `git clone` command. |
||
| 429 | * |
||
| 430 | * @code $git->cloneRepository('git://github.com/cpliakas/git-wrapper.git'); |
||
| 431 | * |
||
| 432 | * @param mixed[] $options |
||
| 433 | */ |
||
| 434 | public function cloneRepository(string $repository, array $options = []): string |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Record changes to the repository. If only one argument is passed, it is assumed to be the commit message. |
||
| 442 | * Therefore `$git->commit('Message');` yields a `git commit -am "Message"` command. |
||
| 443 | * |
||
| 444 | * @code $git->commit('My commit message'); |
||
| 445 | * $git->commit('Makefile', ['m' => 'My commit message']); |
||
| 446 | * |
||
| 447 | * @param mixed ...$argsAndOptions |
||
| 448 | */ |
||
| 449 | public function commit(...$argsAndOptions): string |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @code $git->config('user.email', '[email protected]'); |
||
| 463 | * $git->config('user.name', 'Chris Pliakas'); |
||
| 464 | * |
||
| 465 | * @param mixed ...$argsAndOptions |
||
| 466 | */ |
||
| 467 | public function config(...$argsAndOptions): string |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @code $git->diff(); |
||
| 474 | * $git->diff('topic', 'master'); |
||
| 475 | * |
||
| 476 | * @param mixed ...$argsAndOptions |
||
| 477 | */ |
||
| 478 | public function diff(...$argsAndOptions): string |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @code $git->fetch('origin'); |
||
| 485 | * $git->fetch(['all' => true]); |
||
| 486 | * |
||
| 487 | * @param mixed ...$argsAndOptions |
||
| 488 | */ |
||
| 489 | public function fetch(...$argsAndOptions): string |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Print lines matching a pattern. |
||
| 496 | * |
||
| 497 | * @code $git->grep('time_t', '--', '*.[ch]'); |
||
| 498 | * |
||
| 499 | * @param mixed ...$argsAndOptions |
||
| 500 | */ |
||
| 501 | public function grep(...$argsAndOptions): string |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create an empty git repository or reinitialize an existing one. |
||
| 508 | * |
||
| 509 | * @code $git->init(['bare' => true]); |
||
| 510 | * |
||
| 511 | * @param mixed[] $options |
||
| 512 | */ |
||
| 513 | public function init(array $options = []): string |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @code $git->log(['no-merges' => true]); |
||
| 521 | * $git->log('v2.6.12..', 'include/scsi', 'drivers/scsi'); |
||
| 522 | * |
||
| 523 | * @param mixed ...$argsAndOptions |
||
| 524 | */ |
||
| 525 | public function log(...$argsAndOptions): string |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @code $git->merge('fixes', 'enhancements'); |
||
| 532 | * |
||
| 533 | * @param mixed ...$argsAndOptions |
||
| 534 | */ |
||
| 535 | public function merge(...$argsAndOptions): string |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @code $git->mv('orig.txt', 'dest.txt'); |
||
| 542 | * |
||
| 543 | * @param mixed[] $options |
||
| 544 | */ |
||
| 545 | public function mv(string $source, string $destination, array $options = []): string |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @code $git->pull('upstream', 'master'); |
||
| 553 | * |
||
| 554 | * @param mixed ...$argsAndOptions |
||
| 555 | */ |
||
| 556 | public function pull(...$argsAndOptions): string |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @code $git->push('upstream', 'master'); |
||
| 563 | * |
||
| 564 | * @param mixed ...$argsAndOptions |
||
| 565 | */ |
||
| 566 | public function push(...$argsAndOptions): string |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @code $git->rebase('subsystem@{1}', ['onto' => 'subsystem']); |
||
| 573 | * |
||
| 574 | * @param mixed ...$argsAndOptions |
||
| 575 | */ |
||
| 576 | public function rebase(...$argsAndOptions): string |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @code $git->remote('add', 'upstream', 'git://github.com/cpliakas/git-wrapper.git'); |
||
| 583 | * |
||
| 584 | * @param mixed ...$argsAndOptions |
||
| 585 | */ |
||
| 586 | public function remote(...$argsAndOptions): string |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @code $git->reset(['hard' => true]); |
||
| 593 | * |
||
| 594 | * @param mixed ...$argsAndOptions |
||
| 595 | */ |
||
| 596 | public function reset(...$argsAndOptions): string |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @code $git->rm('oldfile.txt'); |
||
| 603 | * |
||
| 604 | * @param mixed[] $options |
||
| 605 | */ |
||
| 606 | public function rm(string $filepattern, array $options = []): string |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @code $git->show('v1.0.0'); |
||
| 614 | * |
||
| 615 | * @param mixed[] $options |
||
| 616 | */ |
||
| 617 | public function show(string $object, array $options = []): string |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @code $git->status(['s' => true]); |
||
| 625 | * |
||
| 626 | * @param mixed ...$argsAndOptions |
||
| 627 | */ |
||
| 628 | public function status(...$argsAndOptions): string |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @code $git->tag('v1.0.0'); |
||
| 635 | * |
||
| 636 | * @param mixed ...$argsAndOptions |
||
| 637 | */ |
||
| 638 | public function tag(...$argsAndOptions): string |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @code $git->clean('-d', '-f'); |
||
| 645 | * |
||
| 646 | * @param mixed ...$argsAndOptions |
||
| 647 | */ |
||
| 648 | public function clean(...$argsAndOptions): string |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @code $git->archive('HEAD', ['o' => '/path/to/archive']); |
||
| 655 | * |
||
| 656 | * @param mixed ...$argsAndOptions |
||
| 657 | */ |
||
| 658 | public function archive(...$argsAndOptions): string |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Returns a GitTags object containing information on the repository's tags. |
||
| 665 | */ |
||
| 666 | public function tags(): GitTags |
||
| 670 | |||
| 671 | private function ensureAddRemoveArgsAreValid(string $name, string $url): void |
||
| 681 | } |
||
| 682 |