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 Command 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 Command, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Command |
||
33 | { |
||
34 | private $application; |
||
35 | private $name; |
||
36 | private $processTitle; |
||
37 | private $aliases = array(); |
||
38 | private $definition; |
||
39 | private $help; |
||
40 | private $description; |
||
41 | private $ignoreValidationErrors = false; |
||
42 | private $applicationDefinitionMerged = false; |
||
43 | private $applicationDefinitionMergedWithArgs = false; |
||
44 | private $code; |
||
45 | private $synopsis; |
||
46 | private $helperSet; |
||
47 | |||
48 | /** |
||
49 | * Constructor. |
||
50 | * |
||
51 | * @param string|null $name The name of the command; passing null means it must be set in configure() |
||
52 | * |
||
53 | * @throws \LogicException When the command name is empty |
||
54 | * |
||
55 | * @api |
||
56 | */ |
||
57 | public function __construct($name = null) |
||
71 | |||
72 | /** |
||
73 | * Ignores validation errors. |
||
74 | * |
||
75 | * This is mainly useful for the help command. |
||
76 | */ |
||
77 | public function ignoreValidationErrors() |
||
78 | { |
||
79 | $this->ignoreValidationErrors = true; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Sets the application instance for this command. |
||
84 | * |
||
85 | * @param Application $application An Application instance |
||
86 | * |
||
87 | * @api |
||
88 | */ |
||
89 | public function setApplication(Application $application = null) |
||
98 | |||
99 | /** |
||
100 | * Sets the helper set. |
||
101 | * |
||
102 | * @param HelperSet $helperSet A HelperSet instance |
||
103 | */ |
||
104 | public function setHelperSet(HelperSet $helperSet) |
||
105 | { |
||
106 | $this->helperSet = $helperSet; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Gets the helper set. |
||
111 | * |
||
112 | * @return HelperSet A HelperSet instance |
||
113 | */ |
||
114 | public function getHelperSet() |
||
115 | { |
||
116 | return $this->helperSet; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Gets the application instance for this command. |
||
121 | * |
||
122 | * @return Application An Application instance |
||
123 | * |
||
124 | * @api |
||
125 | */ |
||
126 | public function getApplication() |
||
130 | |||
131 | /** |
||
132 | * Checks whether the command is enabled or not in the current environment. |
||
133 | * |
||
134 | * Override this to check for x or y and return false if the command can not |
||
135 | * run properly under the current conditions. |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function isEnabled() |
||
140 | { |
||
141 | return true; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Configures the current command. |
||
146 | */ |
||
147 | protected function configure() |
||
150 | |||
151 | /** |
||
152 | * Executes the current command. |
||
153 | * |
||
154 | * This method is not abstract because you can use this class |
||
155 | * as a concrete class. In this case, instead of defining the |
||
156 | * execute() method, you set the code to execute by passing |
||
157 | * a Closure to the setCode() method. |
||
158 | * |
||
159 | * @param InputInterface $input An InputInterface instance |
||
160 | * @param OutputInterface $output An OutputInterface instance |
||
161 | * |
||
162 | * @return null|int null or 0 if everything went fine, or an error code |
||
163 | * |
||
164 | * @throws \LogicException When this abstract method is not implemented |
||
165 | * |
||
166 | * @see setCode() |
||
167 | */ |
||
168 | protected function execute(InputInterface $input, OutputInterface $output) |
||
172 | |||
173 | /** |
||
174 | * Interacts with the user. |
||
175 | * |
||
176 | * This method is executed before the InputDefinition is validated. |
||
177 | * This means that this is the only place where the command can |
||
178 | * interactively ask for values of missing required arguments. |
||
179 | * |
||
180 | * @param InputInterface $input An InputInterface instance |
||
181 | * @param OutputInterface $output An OutputInterface instance |
||
182 | */ |
||
183 | protected function interact(InputInterface $input, OutputInterface $output) |
||
186 | |||
187 | /** |
||
188 | * Initializes the command just after the input has been validated. |
||
189 | * |
||
190 | * This is mainly useful when a lot of commands extends one main command |
||
191 | * where some things need to be initialized based on the input arguments and options. |
||
192 | * |
||
193 | * @param InputInterface $input An InputInterface instance |
||
194 | * @param OutputInterface $output An OutputInterface instance |
||
195 | */ |
||
196 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
199 | |||
200 | /** |
||
201 | * Runs the command. |
||
202 | * |
||
203 | * The code to execute is either defined directly with the |
||
204 | * setCode() method or by overriding the execute() method |
||
205 | * in a sub-class. |
||
206 | * |
||
207 | * @param InputInterface $input An InputInterface instance |
||
208 | * @param OutputInterface $output An OutputInterface instance |
||
209 | * |
||
210 | * @return int The command exit code |
||
211 | * |
||
212 | * @throws \Exception |
||
213 | * |
||
214 | * @see setCode() |
||
215 | * @see execute() |
||
216 | * |
||
217 | * @api |
||
218 | */ |
||
219 | public function run(InputInterface $input, OutputInterface $output) |
||
262 | |||
263 | /** |
||
264 | * Sets the code to execute when running this command. |
||
265 | * |
||
266 | * If this method is used, it overrides the code defined |
||
267 | * in the execute() method. |
||
268 | * |
||
269 | * @param callable $code A callable(InputInterface $input, OutputInterface $output) |
||
270 | * |
||
271 | * @return Command The current instance |
||
272 | * |
||
273 | * @throws \InvalidArgumentException |
||
274 | * |
||
275 | * @see execute() |
||
276 | * |
||
277 | * @api |
||
278 | */ |
||
279 | public function setCode($code) |
||
289 | |||
290 | /** |
||
291 | * Merges the application definition with the command definition. |
||
292 | * |
||
293 | * This method is not part of public API and should not be used directly. |
||
294 | * |
||
295 | * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments |
||
296 | */ |
||
297 | public function mergeApplicationDefinition($mergeArgs = true) |
||
316 | |||
317 | /** |
||
318 | * Sets an array of argument and option instances. |
||
319 | * |
||
320 | * @param array|InputDefinition $definition An array of argument and option instances or a definition instance |
||
321 | * |
||
322 | * @return Command The current instance |
||
323 | * |
||
324 | * @api |
||
325 | */ |
||
326 | public function setDefinition($definition) |
||
338 | |||
339 | /** |
||
340 | * Gets the InputDefinition attached to this Command. |
||
341 | * |
||
342 | * @return InputDefinition An InputDefinition instance |
||
343 | * |
||
344 | * @api |
||
345 | */ |
||
346 | public function getDefinition() |
||
350 | |||
351 | /** |
||
352 | * Gets the InputDefinition to be used to create XML and Text representations of this Command. |
||
353 | * |
||
354 | * Can be overridden to provide the original command representation when it would otherwise |
||
355 | * be changed by merging with the application InputDefinition. |
||
356 | * |
||
357 | * This method is not part of public API and should not be used directly. |
||
358 | * |
||
359 | * @return InputDefinition An InputDefinition instance |
||
360 | */ |
||
361 | public function getNativeDefinition() |
||
362 | { |
||
363 | return $this->getDefinition(); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Adds an argument. |
||
368 | * |
||
369 | * @param string $name The argument name |
||
370 | * @param int $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL |
||
371 | * @param string $description A description text |
||
372 | * @param mixed $default The default value (for InputArgument::OPTIONAL mode only) |
||
373 | * |
||
374 | * @return Command The current instance |
||
375 | * |
||
376 | * @api |
||
377 | */ |
||
378 | public function addArgument($name, $mode = null, $description = '', $default = null) |
||
384 | |||
385 | /** |
||
386 | * Adds an option. |
||
387 | * |
||
388 | * @param string $name The option name |
||
389 | * @param string $shortcut The shortcut (can be null) |
||
390 | * @param int $mode The option mode: One of the InputOption::VALUE_* constants |
||
391 | * @param string $description A description text |
||
392 | * @param mixed $default The default value (must be null for InputOption::VALUE_REQUIRED or InputOption::VALUE_NONE) |
||
393 | * |
||
394 | * @return Command The current instance |
||
395 | * |
||
396 | * @api |
||
397 | */ |
||
398 | public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) |
||
404 | |||
405 | /** |
||
406 | * Sets the name of the command. |
||
407 | * |
||
408 | * This method can set both the namespace and the name if |
||
409 | * you separate them by a colon (:) |
||
410 | * |
||
411 | * $command->setName('foo:bar'); |
||
412 | * |
||
413 | * @param string $name The command name |
||
414 | * |
||
415 | * @return Command The current instance |
||
416 | * |
||
417 | * @throws \InvalidArgumentException When the name is invalid |
||
418 | * |
||
419 | * @api |
||
420 | */ |
||
421 | public function setName($name) |
||
429 | |||
430 | /** |
||
431 | * Sets the process title of the command. |
||
432 | * |
||
433 | * This feature should be used only when creating a long process command, |
||
434 | * like a daemon. |
||
435 | * |
||
436 | * PHP 5.5+ or the proctitle PECL library is required |
||
437 | * |
||
438 | * @param string $title The process title |
||
439 | * |
||
440 | * @return Command The current instance |
||
441 | */ |
||
442 | public function setProcessTitle($title) |
||
443 | { |
||
444 | $this->processTitle = $title; |
||
445 | |||
446 | return $this; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Returns the command name. |
||
451 | * |
||
452 | * @return string The command name |
||
453 | * |
||
454 | * @api |
||
455 | */ |
||
456 | public function getName() |
||
460 | |||
461 | /** |
||
462 | * Sets the description for the command. |
||
463 | * |
||
464 | * @param string $description The description for the command |
||
465 | * |
||
466 | * @return Command The current instance |
||
467 | * |
||
468 | * @api |
||
469 | */ |
||
470 | public function setDescription($description) |
||
476 | |||
477 | /** |
||
478 | * Returns the description for the command. |
||
479 | * |
||
480 | * @return string The description for the command |
||
481 | * |
||
482 | * @api |
||
483 | */ |
||
484 | public function getDescription() |
||
488 | |||
489 | /** |
||
490 | * Sets the help for the command. |
||
491 | * |
||
492 | * @param string $help The help for the command |
||
493 | * |
||
494 | * @return Command The current instance |
||
495 | * |
||
496 | * @api |
||
497 | */ |
||
498 | public function setHelp($help) |
||
504 | |||
505 | /** |
||
506 | * Returns the help for the command. |
||
507 | * |
||
508 | * @return string The help for the command |
||
509 | * |
||
510 | * @api |
||
511 | */ |
||
512 | public function getHelp() |
||
516 | |||
517 | /** |
||
518 | * Returns the processed help for the command replacing the %command.name% and |
||
519 | * %command.full_name% patterns with the real values dynamically. |
||
520 | * |
||
521 | * @return string The processed help for the command |
||
522 | */ |
||
523 | public function getProcessedHelp() |
||
538 | |||
539 | /** |
||
540 | * Sets the aliases for the command. |
||
541 | * |
||
542 | * @param string[] $aliases An array of aliases for the command |
||
543 | * |
||
544 | * @return Command The current instance |
||
545 | * |
||
546 | * @throws \InvalidArgumentException When an alias is invalid |
||
547 | * |
||
548 | * @api |
||
549 | */ |
||
550 | public function setAliases($aliases) |
||
564 | |||
565 | /** |
||
566 | * Returns the aliases for the command. |
||
567 | * |
||
568 | * @return array An array of aliases for the command |
||
569 | * |
||
570 | * @api |
||
571 | */ |
||
572 | public function getAliases() |
||
576 | |||
577 | /** |
||
578 | * Returns the synopsis for the command. |
||
579 | * |
||
580 | * @return string The synopsis |
||
581 | */ |
||
582 | public function getSynopsis() |
||
590 | |||
591 | /** |
||
592 | * Gets a helper instance by name. |
||
593 | * |
||
594 | * @param string $name The helper name |
||
595 | * |
||
596 | * @return mixed The helper value |
||
597 | * |
||
598 | * @throws \InvalidArgumentException if the helper is not defined |
||
599 | * |
||
600 | * @api |
||
601 | */ |
||
602 | public function getHelper($name) |
||
606 | |||
607 | /** |
||
608 | * Returns a text representation of the command. |
||
609 | * |
||
610 | * @return string A string representing the command |
||
611 | * |
||
612 | * @deprecated Deprecated since version 2.3, to be removed in 3.0. |
||
613 | */ |
||
614 | View Code Duplication | public function asText() |
|
622 | |||
623 | /** |
||
624 | * Returns an XML representation of the command. |
||
625 | * |
||
626 | * @param bool $asDom Whether to return a DOM or an XML string |
||
627 | * |
||
628 | * @return string|\DOMDocument An XML string representing the command |
||
629 | * |
||
630 | * @deprecated Deprecated since version 2.3, to be removed in 3.0. |
||
631 | */ |
||
632 | View Code Duplication | public function asXml($asDom = false) |
|
645 | |||
646 | /** |
||
647 | * Validates a command name. |
||
648 | * |
||
649 | * It must be non-empty and parts can optionally be separated by ":". |
||
650 | * |
||
651 | * @param string $name |
||
652 | * |
||
653 | * @throws \InvalidArgumentException When the name is invalid |
||
654 | */ |
||
655 | private function validateName($name) |
||
661 | } |
||
662 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.