Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class GitHooksInstaller extends LibraryInstaller |
||
14 | { |
||
15 | /** |
||
16 | * @inheritdoc |
||
17 | */ |
||
18 | 2 | public function supports($packageType) |
|
22 | |||
23 | /** |
||
24 | * {@inheritDoc} |
||
25 | */ |
||
26 | View Code Duplication | public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
|
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | View Code Duplication | public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) |
|
1 ignored issue
–
show
|
|||
54 | { |
||
55 | parent::update($repo, $initial, $target); |
||
56 | |||
57 | $gitRootPath = realpath($this->getGitRootPath()); |
||
58 | if (!is_dir($gitRootPath)) { |
||
59 | $this->io->writeError(sprintf(' <warning>Skipped update of %s: Not a git repository found on "git-root" path %s</warning>', $target->getName(), $gitRootPath)); |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | $originPath = $this->getInstallPath($initial); |
||
64 | $targetPath = $this->getGitHooksInstallPath(); |
||
65 | |||
66 | if ($this->io->isVerbose()) { |
||
67 | $this->io->write(sprintf(' Updating git hooks from %s into %s', $originPath, $targetPath)); |
||
68 | } |
||
69 | |||
70 | // throws exception if one of both paths doesn't exists or couldn't be created |
||
71 | $this->filesystem->ensureDirectoryExists($originPath); |
||
72 | $this->filesystem->ensureDirectoryExists($targetPath); |
||
73 | |||
74 | $this->copyGitHooks($originPath, $targetPath, true); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
81 | { |
||
82 | parent::uninstall($repo, $package); |
||
83 | |||
84 | $gitRootPath = realpath($this->getGitRootPath()); |
||
85 | if (!is_dir($gitRootPath)) { |
||
86 | $this->io->writeError(sprintf(' Skipped update of %s: Not a git repository found on "git-root" path %s', $package->getName(), $gitRootPath)); |
||
87 | return; |
||
88 | } |
||
89 | |||
90 | $originPath = $this->getInstallPath($package); |
||
91 | $targetPath = $this->getGitHooksInstallPath(); |
||
92 | |||
93 | if ($this->io->isVerbose()) { |
||
94 | $this->io->write(sprintf(' Uninstalling git hooks from %s into %s', $originPath, $targetPath)); |
||
95 | } |
||
96 | |||
97 | $this->removeGitHooks($originPath, $targetPath); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $sourcePath |
||
102 | * @param string $targetPath |
||
103 | * @param bool $isUpdate |
||
104 | */ |
||
105 | private function copyGitHooks($sourcePath, $targetPath, $isUpdate = false) |
||
138 | |||
139 | /** |
||
140 | * @param string $sourcePath |
||
141 | * @param string $targetPath |
||
142 | */ |
||
143 | private function removeGitHooks($sourcePath, $targetPath) |
||
160 | |||
161 | /** |
||
162 | * Returns the installation path of the Git hooks |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | private function getGitHooksInstallPath() |
||
170 | |||
171 | /** |
||
172 | * Returns the git root path |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | private function getGitRootPath() |
||
183 | } |
||
184 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.