| Conditions | 14 |
| Paths | 6 |
| Total Lines | 76 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 59 | public function work(Monorepo $repo, InputInterface $input, SymfonyStyle $output): int |
||
| 60 | { |
||
| 61 | $mainRepo = $repo->getRepository(); |
||
| 62 | $output->writeln('<info>Checking git filter-repo command existence...</info>'); |
||
| 63 | |||
| 64 | if ($readTree = $input->getOption('read-tree')) { |
||
| 65 | $output->writeln('<warning>Using read-tree merge is dangerous and may break your repository.</warning>'); |
||
| 66 | |||
| 67 | if (!$input->getOption('quiet') && !$output->confirm('Do you want to continue?', false)) { |
||
| 68 | return $mainRepo->getExitCode(); |
||
| 69 | } |
||
| 70 | } elseif (!$mainRepo->check('filter-repo', ['-h'])) { |
||
| 71 | $output->writeln('Visit <comment>https://github.com/newren/git-filter-repo</comment> and install git-filter-repo.'); |
||
| 72 | |||
| 73 | return WorkflowCommand::FAILURE; |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!empty($mainRepo->run('status', ['--porcelain']))) { |
||
| 77 | $output->writeln('<error>Git status shows pending changes in repo</error>'); |
||
| 78 | |||
| 79 | return WorkflowCommand::FAILURE; |
||
| 80 | } |
||
| 81 | |||
| 82 | return $repo->resolveRepository($output, static function (array $required) use ($output, $repo, $mainRepo, $readTree): int { |
||
|
|
|||
| 83 | [$url, $remote, $path, $clonePath] = $required; |
||
| 84 | $output->writeln(\sprintf('<info>Rewriting %s commit history to point to %s</info>', $url, $path)); |
||
| 85 | |||
| 86 | if (!$readTree) { |
||
| 87 | $mainRepo->run('filter-repo', ['--force', '--to-subdirectory-filter', $path], null, $clonePath); |
||
| 88 | } |
||
| 89 | |||
| 90 | $getRemoteBranches = $mainRepo->runConcurrent([ |
||
| 91 | ['config', '--add', "remote.{$remote}.fetch", "+refs/tags/*:refs/tags/{$remote}/*"], |
||
| 92 | ['config', "remote.{$remote}.tagOpt", '--no-tags'], |
||
| 93 | ['fetch', '--all'], |
||
| 94 | ['for-each-ref', '--format="%(refname:lstrip=3)"', "refs/remotes/{$remote}"], |
||
| 95 | ])[3] ?? ''; |
||
| 96 | |||
| 97 | foreach (\explode("\n", $getRemoteBranches) as $branch) { |
||
| 98 | if (empty($branch = \trim($branch, '"'))) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | $hasBranch = $mainRepo->check('show-ref', ['--verify', "refs/heads/{$branch}"]); |
||
| 102 | $mergeMsg = "Merge branch '{$remote}/{$branch}' into {$branch}"; |
||
| 103 | |||
| 104 | if ($readTree) { |
||
| 105 | $commands = [ |
||
| 106 | ['checkout', '-b', $hasBranch ? $branch.($i = \uniqid('-')) : $branch, "{$remote}/{$branch}"], |
||
| 107 | ['rm', '-rf', '*'], |
||
| 108 | ['read-tree', '--prefix', $path.'/', "{$remote}/{$branch}"], |
||
| 109 | ['commit', '-m', "Added remote-tracking {$remote}/{$branch}", '--allow-empty'], |
||
| 110 | ['reset', '--quiet', '--hard'], |
||
| 111 | ]; |
||
| 112 | |||
| 113 | if ($hasBranch) { |
||
| 114 | $commands[] = ['checkout', $branch]; |
||
| 115 | $commands[] = ['merge', '--allow-unrelated-histories', $branch.$i, '-m', $mergeMsg]; |
||
| 116 | $commands[] = ['branch', '-D', $branch.$i]; |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $commands = [ |
||
| 120 | ['checkout', '--quiet', "{$remote}/{$branch}"], |
||
| 121 | ['switch', '--quiet', ...($hasBranch ? [$branch] : ['-c', $branch])], |
||
| 122 | ['merge', "{$remote}/{$branch}", '--allow-unrelated-histories', '--no-edit', '--no-verify', '--quiet', '-m', $mergeMsg], |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | |||
| 126 | $mainRepo->runConcurrent($commands); |
||
| 127 | } |
||
| 128 | |||
| 129 | if (0 === $merged = $mainRepo->getExitCode()) { |
||
| 130 | $output->writeln(\sprintf('Merged "%s" into <info>%s/%s</info>', $url, $mainRepo->getPath(), \ltrim($path, '/'))); |
||
| 131 | } |
||
| 132 | |||
| 133 | return $merged; |
||
| 134 | }, static fn (array $v): bool => 'true' === $v[3]); |
||
| 135 | } |
||
| 137 |
This check looks for imports that have been defined, but are not used in the scope.