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:
Complex classes like PackageCommandHandler 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 PackageCommandHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class PackageCommandHandler |
||
36 | { |
||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $stateStrings = array( |
||
41 | PackageState::ENABLED => 'enabled', |
||
42 | PackageState::NOT_FOUND => 'not-found', |
||
43 | PackageState::NOT_LOADABLE => 'not-loadable', |
||
44 | ); |
||
45 | |||
46 | /** |
||
47 | * @var PackageManager |
||
48 | */ |
||
49 | private $packageManager; |
||
50 | |||
51 | /** |
||
52 | * Creates the handler. |
||
53 | * |
||
54 | * @param PackageManager $packageManager The package manager. |
||
55 | */ |
||
56 | 20 | public function __construct(PackageManager $packageManager) |
|
60 | |||
61 | /** |
||
62 | * Handles the "package --list" command. |
||
63 | * |
||
64 | * @param Args $args The console arguments. |
||
65 | * @param IO $io The I/O. |
||
66 | * |
||
67 | * @return int The status code. |
||
68 | */ |
||
69 | 11 | public function handleList(Args $args, IO $io) |
|
70 | { |
||
71 | 11 | $packages = $this->getSelectedPackages($args); |
|
72 | |||
73 | 11 | if ($args->isOptionSet('format')) { |
|
74 | 1 | $this->printPackagesWithFormat($io, $packages, $args->getOption('format')); |
|
75 | } else { |
||
76 | 10 | $this->printPackagesByState($io, $packages, $this->getSelectedStates($args)); |
|
77 | } |
||
78 | |||
79 | 11 | return 0; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Handles the "package --install" command. |
||
84 | * |
||
85 | * @param Args $args The console arguments. |
||
86 | * |
||
87 | * @return int The status code. |
||
88 | */ |
||
89 | 5 | public function handleInstall(Args $args) |
|
90 | { |
||
91 | 5 | $packageName = $args->getArgument('name'); |
|
92 | 5 | $installPath = Path::makeAbsolute($args->getArgument('path'), getcwd()); |
|
93 | 5 | $installer = $args->getOption('installer'); |
|
94 | 5 | $env = $args->isOptionSet('dev') ? Environment::DEV : Environment::PROD; |
|
95 | |||
96 | 5 | $this->packageManager->installPackage($installPath, $packageName, $installer, $env); |
|
97 | |||
98 | 5 | return 0; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Handles the "package --rename" command. |
||
103 | * |
||
104 | * @param Args $args The console arguments. |
||
105 | * |
||
106 | * @return int The status code. |
||
107 | */ |
||
108 | 1 | public function handleRename(Args $args) |
|
109 | { |
||
110 | 1 | $packageName = $args->getArgument('name'); |
|
111 | 1 | $newName = $args->getArgument('new-name'); |
|
112 | |||
113 | 1 | $this->packageManager->renamePackage($packageName, $newName); |
|
114 | |||
115 | 1 | return 0; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Handles the "package --delete" command. |
||
120 | * |
||
121 | * @param Args $args The console arguments. |
||
122 | * |
||
123 | * @return int The status code. |
||
124 | */ |
||
125 | 2 | public function handleDelete(Args $args) |
|
126 | { |
||
127 | 2 | $packageName = $args->getArgument('name'); |
|
128 | |||
129 | 2 | if (!$this->packageManager->hasPackage($packageName)) { |
|
130 | 1 | throw new RuntimeException(sprintf( |
|
131 | 1 | 'The package "%s" is not installed.', |
|
132 | $packageName |
||
133 | )); |
||
134 | } |
||
135 | |||
136 | 1 | $this->packageManager->removePackage($packageName); |
|
137 | |||
138 | 1 | return 0; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Handles the "package --clean" command. |
||
143 | * |
||
144 | * @param Args $args The console arguments. |
||
145 | * @param IO $io The I/O. |
||
146 | * |
||
147 | * @return int The status code. |
||
148 | */ |
||
149 | 1 | public function handleClean(Args $args, IO $io) |
|
|
|||
150 | { |
||
151 | 1 | $expr = Expr::method('getState', Expr::same(PackageState::NOT_FOUND)); |
|
152 | |||
153 | 1 | foreach ($this->packageManager->findPackages($expr) as $package) { |
|
154 | 1 | $io->writeLine('Removing '.$package->getName()); |
|
155 | 1 | $this->packageManager->removePackage($package->getName()); |
|
156 | } |
||
157 | |||
158 | 1 | return 0; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns the package states that should be displayed for the given |
||
163 | * console arguments. |
||
164 | * |
||
165 | * @param Args $args The console arguments. |
||
166 | * |
||
167 | * @return int[] A list of {@link PackageState} constants. |
||
168 | */ |
||
169 | 11 | private function getSelectedStates(Args $args) |
|
170 | { |
||
171 | 11 | $states = array(); |
|
172 | |||
173 | 11 | if ($args->isOptionSet('enabled')) { |
|
174 | 3 | $states[] = PackageState::ENABLED; |
|
175 | } |
||
176 | |||
177 | 11 | if ($args->isOptionSet('not-found')) { |
|
178 | 2 | $states[] = PackageState::NOT_FOUND; |
|
179 | } |
||
180 | |||
181 | 11 | if ($args->isOptionSet('not-loadable')) { |
|
182 | 1 | $states[] = PackageState::NOT_LOADABLE; |
|
183 | } |
||
184 | |||
185 | 11 | return $states ?: PackageState::all(); |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * Returns the packages that should be displayed for the given console |
||
190 | * arguments. |
||
191 | * |
||
192 | * @param Args $args The console arguments. |
||
193 | * |
||
194 | * @return PackageCollection The packages. |
||
195 | */ |
||
196 | 11 | private function getSelectedPackages(Args $args) |
|
197 | { |
||
198 | 11 | $states = $this->getSelectedStates($args); |
|
199 | 11 | $expr = Expr::true(); |
|
200 | 11 | $envs = array(); |
|
201 | |||
202 | 11 | if ($states !== PackageState::all()) { |
|
203 | 5 | $expr = $expr->andMethod('getState', Expr::in($states)); |
|
204 | } |
||
205 | |||
206 | 11 | if ($args->isOptionSet('installer')) { |
|
207 | 2 | $expr = $expr->andMethod('getInstallInfo', Expr::method('getInstallerName', Expr::same($args->getOption('installer')))); |
|
208 | } |
||
209 | |||
210 | 11 | if ($args->isOptionSet('prod')) { |
|
211 | 2 | $envs[] = Environment::PROD; |
|
212 | } |
||
213 | |||
214 | 11 | if ($args->isOptionSet('dev')) { |
|
215 | 2 | $envs[] = Environment::DEV; |
|
216 | } |
||
217 | |||
218 | 11 | if (count($envs) > 0) { |
|
219 | 3 | $expr = $expr->andMethod('getInstallInfo', Expr::method('getEnvironment', Expr::in($envs))); |
|
220 | } |
||
221 | |||
222 | 11 | return $this->packageManager->findPackages($expr); |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Prints packages with intermediate headers for the package states. |
||
227 | * |
||
228 | * @param IO $io The I/O. |
||
229 | * @param PackageCollection $packages The packages to print. |
||
230 | * @param int[] $states The states to print. |
||
231 | */ |
||
232 | 10 | private function printPackagesByState(IO $io, PackageCollection $packages, array $states) |
|
233 | { |
||
234 | 10 | $printStates = count($states) > 1; |
|
235 | |||
236 | 10 | foreach ($states as $state) { |
|
237 | 10 | $filteredPackages = array_filter($packages->toArray(), function (Package $package) use ($state) { |
|
238 | 10 | return $state === $package->getState(); |
|
239 | 10 | }); |
|
240 | |||
241 | 10 | if (0 === count($filteredPackages)) { |
|
242 | 2 | continue; |
|
243 | } |
||
244 | |||
245 | 10 | if ($printStates) { |
|
246 | 6 | $this->printPackageState($io, $state); |
|
247 | } |
||
248 | |||
249 | 10 | if (PackageState::NOT_LOADABLE === $state) { |
|
250 | 5 | $this->printNotLoadablePackages($io, $filteredPackages, $printStates); |
|
251 | } else { |
||
252 | 9 | $styleTag = PackageState::ENABLED === $state ? null : 'bad'; |
|
253 | 9 | $this->printPackageTable($io, $filteredPackages, $styleTag, $printStates); |
|
254 | } |
||
255 | |||
256 | 10 | if ($printStates) { |
|
257 | 10 | $io->writeLine(''); |
|
258 | } |
||
259 | } |
||
260 | 10 | } |
|
261 | |||
262 | /** |
||
263 | * Prints packages using the given format. |
||
264 | * |
||
265 | * @param IO $io The I/O. |
||
266 | * @param PackageCollection $packages The packages to print. |
||
267 | * @param string $format The format string. |
||
268 | */ |
||
269 | 1 | private function printPackagesWithFormat(IO $io, PackageCollection $packages, $format) |
|
283 | |||
284 | /** |
||
285 | * Prints the heading for a given package state. |
||
286 | * |
||
287 | * @param IO $io The I/O. |
||
288 | * @param int $packageState The {@link PackageState} constant. |
||
289 | */ |
||
290 | 6 | View Code Duplication | private function printPackageState(IO $io, $packageState) |
291 | { |
||
292 | switch ($packageState) { |
||
293 | 6 | case PackageState::ENABLED: |
|
294 | 6 | $io->writeLine('The following packages are currently enabled:'); |
|
295 | 6 | $io->writeLine(''); |
|
296 | |||
297 | 6 | return; |
|
298 | 5 | case PackageState::NOT_FOUND: |
|
299 | 4 | $io->writeLine('The following packages could not be found:'); |
|
300 | 4 | $io->writeLine(' (use "puli package --clean" to remove)'); |
|
301 | 4 | $io->writeLine(''); |
|
302 | |||
303 | 4 | return; |
|
304 | 4 | case PackageState::NOT_LOADABLE: |
|
305 | 4 | $io->writeLine('The following packages could not be loaded:'); |
|
306 | 4 | $io->writeLine(''); |
|
307 | |||
308 | 4 | return; |
|
309 | } |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Prints a list of packages in a table. |
||
314 | * |
||
315 | * @param IO $io The I/O. |
||
316 | * @param Package[] $packages The packages. |
||
317 | * @param string|null $styleTag The tag used to style the output. If `null`, |
||
318 | * the default colors are used. |
||
319 | * @param bool $indent Whether to indent the output. |
||
320 | */ |
||
321 | 9 | private function printPackageTable(IO $io, array $packages, $styleTag = null, $indent = false) |
|
349 | |||
350 | /** |
||
351 | * Prints not-loadable packages in a table. |
||
352 | * |
||
353 | * @param IO $io The I/O. |
||
354 | * @param Package[] $packages The not-loadable packages. |
||
355 | * @param bool $indent Whether to indent the output. |
||
356 | */ |
||
357 | 5 | private function printNotLoadablePackages(IO $io, array $packages, $indent = false) |
|
391 | } |
||
392 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.