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 |
||
26 | class SharedPackageInstallerSolver implements InstallerInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var SymlinkFilesystem |
||
30 | */ |
||
31 | protected $filesystem; |
||
32 | |||
33 | /** |
||
34 | * @var SharedPackageSolver |
||
35 | */ |
||
36 | protected $solver; |
||
37 | |||
38 | /** |
||
39 | * @var SharedPackageInstaller |
||
40 | */ |
||
41 | protected $symlinkInstaller; |
||
42 | |||
43 | /** |
||
44 | * @var LibraryInstaller |
||
45 | */ |
||
46 | protected $defaultInstaller; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @param SharedPackageSolver $solver |
||
51 | * @param SharedPackageInstaller $symlinkInstaller |
||
52 | * @param LibraryInstaller $defaultInstaller |
||
53 | */ |
||
54 | public function __construct( |
||
55 | SharedPackageSolver $solver, |
||
56 | SharedPackageInstaller $symlinkInstaller, |
||
57 | LibraryInstaller $defaultInstaller |
||
58 | ) |
||
59 | { |
||
60 | $this->solver = $solver; |
||
61 | $this->symlinkInstaller = $symlinkInstaller; |
||
62 | $this->defaultInstaller = $defaultInstaller; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Returns the installation path of a package |
||
67 | * |
||
68 | * @param PackageInterface $package |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getInstallPath(PackageInterface $package) |
||
80 | |||
81 | /** |
||
82 | * @param InstalledRepositoryInterface $repo |
||
83 | * @param PackageInterface $package |
||
84 | */ |
||
85 | View Code Duplication | public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
|
93 | |||
94 | /** |
||
95 | * @param InstalledRepositoryInterface $repo |
||
96 | * @param PackageInterface $package |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | View Code Duplication | public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) |
|
101 | { |
||
102 | if ($this->solver->isSharedPackage($package)) { |
||
103 | return $this->symlinkInstaller->isInstalled($repo, $package); |
||
104 | } |
||
105 | |||
106 | return $this->defaultInstaller->isInstalled($repo, $package); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) |
||
113 | { |
||
114 | // If both packages are not shared |
||
115 | if (!$this->solver->isSharedPackage($initial) && !$this->solver->isSharedPackage($target)) { |
||
116 | $this->defaultInstaller->update($repo, $initial, $target); |
||
117 | } else { |
||
118 | if (!$repo->hasPackage($initial)) { |
||
119 | throw new \InvalidArgumentException('Package is not installed : ' . $initial->getPrettyName()); |
||
120 | } |
||
121 | |||
122 | $this->symlinkInstaller->update($repo, $initial, $target); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param InstalledRepositoryInterface $repo |
||
128 | * @param PackageInterface $package |
||
129 | * |
||
130 | * @throws FilesystemException |
||
131 | */ |
||
132 | public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
144 | |||
145 | /** |
||
146 | * @param string $packageType |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function supports($packageType) |
||
156 | } |
||
157 |