Complex classes like WorkingCopy 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 WorkingCopy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class WorkingCopy implements LoggerAwareInterface |
||
| 11 | { |
||
| 12 | use ExecWithRedactionTrait; |
||
| 13 | use LoggerAwareTrait; |
||
| 14 | |||
| 15 | protected $remote; |
||
| 16 | protected $fork; |
||
| 17 | protected $dir; |
||
| 18 | protected $api; |
||
| 19 | |||
| 20 | const FORCE_MERGE_COMMIT = 0x01; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * WorkingCopy constructor |
||
| 24 | * |
||
| 25 | * @param $url Remote origin for the GitHub repository |
||
| 26 | * @param $dir Checkout location for the project |
||
| 27 | */ |
||
| 28 | protected function __construct($url, $dir, $branch = false, $api = null) |
||
| 37 | |||
| 38 | public static function fromDir($dir, $remoteName = 'origin', $api = null) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * addFork will set a secondary remote on this repository. |
||
| 47 | * The purpose of having a fork remote is if the primary repository |
||
| 48 | * is read-only. If a fork is set, then any branches pushed |
||
| 49 | * will go to the fork; any pull request created will still be |
||
| 50 | * set on the primary repository, but will refer to the branch on |
||
| 51 | * the fork. |
||
| 52 | */ |
||
| 53 | public function addFork($fork_url) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * createFork creates a new secondary repository copied from |
||
| 67 | * the current repository, and sets it up as a fork per 'addFork'. |
||
| 68 | */ |
||
| 69 | public function createFork($forked_project_name, $forked_org = '', $branch = '') |
||
| 83 | |||
| 84 | public function deleteFork() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * forkUrl returns the URL of the forked repository that should |
||
| 95 | * be used for creating any pull requests. |
||
| 96 | */ |
||
| 97 | public function forkUrl() |
||
| 104 | |||
| 105 | public function forkProjectWithOrg() |
||
| 112 | |||
| 113 | public function remoteFork() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Clone the specified repository to the given URL at the indicated |
||
| 123 | * directory. If the desired repository already exists there, then |
||
| 124 | * we will re-use it rather than re-clone the repository. |
||
| 125 | * |
||
| 126 | * @param string $url |
||
| 127 | * @param string $dir |
||
| 128 | * @param HubphAPI|null $api |
||
| 129 | * @return WorkingCopy |
||
| 130 | */ |
||
| 131 | public static function clone($url, $dir, $api = null) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Clone the specified repository to the given URL at the indicated |
||
| 138 | * directory. Only clone a single commit. Since we're only interested |
||
| 139 | * in one commit, we'll just remove the cache if it is present. |
||
| 140 | * |
||
| 141 | * @param string $url |
||
| 142 | * @param string $dir |
||
| 143 | * @param string $branch |
||
| 144 | * @param HubphAPI|null $api |
||
| 145 | * @return WorkingCopy |
||
| 146 | */ |
||
| 147 | public static function shallowClone($url, $dir, $branch, $depth = 1, $api = null) |
||
| 153 | |||
| 154 | public static function unclonedReference($url, $dir, $branch, $api = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Clone the specified branch of the specified repository to the given URL. |
||
| 161 | * |
||
| 162 | * @param string $url |
||
| 163 | * @param string $dir |
||
| 164 | * @param string $branch |
||
| 165 | * @param HubphAPI|null $api |
||
| 166 | * @return WorkingCopy |
||
| 167 | */ |
||
| 168 | public static function cloneBranch($url, $dir, $branch, $api, $depth = false) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * take tranforms this local working copy such that it RETAINS all of its |
||
| 177 | * local files (no change to any unstaged modifications or files) and |
||
| 178 | * TAKES OVER the repository from the provided working copy. |
||
| 179 | * |
||
| 180 | * The local repository that was formerly in place here is disposed. |
||
| 181 | * Any branches or commits not already pushed to the remote repository |
||
| 182 | * are lost. Only the working files remain. The remotes for this working |
||
| 183 | * copy become the remotes from the provided repository. |
||
| 184 | * |
||
| 185 | * The other working copy is disposed: its files are all removed |
||
| 186 | * from the filesystem. |
||
| 187 | */ |
||
| 188 | public function take(WorkingCopy $rhs) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * remove will delete all of the local working files managed by this |
||
| 204 | * object, including the '.git' directory. This method should be called |
||
| 205 | * if the local working copy is corrupted or otherwise becomes unusable. |
||
| 206 | */ |
||
| 207 | public function remove() |
||
| 212 | |||
| 213 | public function remote($remote_name = '') |
||
| 220 | |||
| 221 | public function url($remote_name = '') |
||
| 225 | |||
| 226 | public function dir() |
||
| 230 | |||
| 231 | public function valid() |
||
| 235 | |||
| 236 | public function org($remote_name = '') |
||
| 240 | |||
| 241 | public function project($remote_name = '') |
||
| 245 | |||
| 246 | public function projectWithOrg($remote_name = '') |
||
| 250 | |||
| 251 | /** |
||
| 252 | * List modified files. |
||
| 253 | */ |
||
| 254 | public function status() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Fetch from the specified remote. |
||
| 261 | */ |
||
| 262 | public function fetch($remote, $branch) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Fetch from the specified remote. |
||
| 270 | */ |
||
| 271 | public function fetchTags($remote = 'origin') |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Pull from the specified remote. |
||
| 279 | */ |
||
| 280 | public function pull($remote, $branch) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Push the specified branch to the desired remote. |
||
| 288 | */ |
||
| 289 | public function push($remote = '', $branch = '', $force = false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Force-push the branch |
||
| 304 | */ |
||
| 305 | public function forcePush($remote = '', $branch = '') |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Merge the specified branch into the current branch. |
||
| 312 | */ |
||
| 313 | public function merge($branch, $modes = 0) |
||
| 323 | |||
| 324 | public function cherryPick($sha) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Reset to the specified reference. |
||
| 332 | */ |
||
| 333 | public function reset($ref = '', $hard = false) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * switchBranch is a synonym for 'checkout' |
||
| 341 | */ |
||
| 342 | public function switchBranch($branch) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Switch to the specified branch. Use 'createBranch' to create a new branch. |
||
| 350 | */ |
||
| 351 | public function checkout($branch) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Create a new branch |
||
| 359 | */ |
||
| 360 | public function createBranch($branch, $base = '', $force = false) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Stage the items at the specified path. |
||
| 369 | */ |
||
| 370 | public function add($itemsToAdd) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Stage everything |
||
| 378 | */ |
||
| 379 | public function addAll() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Commit the staged changes. |
||
| 387 | * |
||
| 388 | * @param string $message |
||
| 389 | * @param bool $amend |
||
| 390 | */ |
||
| 391 | public function commit($message, $amend = false) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Commit the staged changes by a specified user at specified date. |
||
| 400 | * |
||
| 401 | * @param string $message |
||
| 402 | * @param string $author |
||
| 403 | * @param string $commit_date |
||
| 404 | * @param bool $amend |
||
| 405 | */ |
||
| 406 | public function commitBy($message, $author, $commit_date, $amend = false) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Ammend the top commit without altering the message. |
||
| 415 | */ |
||
| 416 | public function amend() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Add a tag |
||
| 423 | */ |
||
| 424 | public function tag($tag, $ref = '') |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return the commit message for the sprecified ref |
||
| 432 | */ |
||
| 433 | public function message($ref = 'HEAD') |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return the commit date for the sprecified ref |
||
| 440 | */ |
||
| 441 | public function commitDate($ref = 'HEAD') |
||
| 445 | |||
| 446 | public function branch($ref = 'HEAD') |
||
| 450 | |||
| 451 | public function revParse($ref) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Show a diff of the current modified and uncommitted files. |
||
| 458 | */ |
||
| 459 | public function diff() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Create a pull request. |
||
| 466 | * |
||
| 467 | * @param string $message |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | public function pr($message, $body = '', $base = 'master', $head = '', $forked_org = '') |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Show a diff of the specified reference from the commit before it. |
||
| 489 | */ |
||
| 490 | public function show($ref = "HEAD") |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Add a remote (or change the URL to an existing remote) |
||
| 497 | */ |
||
| 498 | public function addRemote($url, $remote) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * If the directory exists, check its remote. Fail if there is |
||
| 505 | * some project there that is not the requested project. |
||
| 506 | */ |
||
| 507 | protected function confirmCachedRepoHasCorrectRemote($emptyOk = false) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Set the remote origin to the provided url |
||
| 533 | * @param string $url |
||
| 534 | * @param string $dir |
||
| 535 | * @param string $remote |
||
| 536 | */ |
||
| 537 | protected static function setRemoteUrl($url, $dir, $remote = 'origin') |
||
| 548 | |||
| 549 | /** |
||
| 550 | * If the directory does not exist, then clone it. |
||
| 551 | */ |
||
| 552 | public function cloneIfNecessary($branch = false, $depth = false) |
||
| 565 | |||
| 566 | protected function freshClone($branch = false, $depth = false) |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Run a git function on the local working copy. Fail on error. |
||
| 588 | * |
||
| 589 | * @return string stdout |
||
| 590 | */ |
||
| 591 | public function git($cmd, $replacements = [], $redacted = []) |
||
| 595 | } |
||
| 596 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.