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 PluginCommandHandler |
||
27 | { |
||
28 | /** |
||
29 | * @var RootModuleFileManager |
||
30 | */ |
||
31 | private $manager; |
||
32 | |||
33 | /** |
||
34 | * Creates the handler. |
||
35 | * |
||
36 | * @param RootModuleFileManager $manager The root module file manager |
||
37 | */ |
||
38 | 6 | public function __construct(RootModuleFileManager $manager) |
|
39 | { |
||
40 | 6 | $this->manager = $manager; |
|
41 | 6 | } |
|
42 | |||
43 | /** |
||
44 | * Handles the "puli plugin --list" command. |
||
45 | * |
||
46 | * @param Args $args The console arguments |
||
47 | * @param IO $io The I/O |
||
48 | * |
||
49 | * @return int The status code |
||
50 | */ |
||
51 | 2 | public function handleList(Args $args, IO $io) |
|
|
|||
52 | { |
||
53 | 2 | $pluginClasses = $this->manager->getPluginClasses(); |
|
54 | |||
55 | 2 | if (!$pluginClasses) { |
|
56 | 1 | $io->writeLine('No plugin classes. Use "puli plugin --install <class>" to install a plugin class.'); |
|
57 | |||
58 | 1 | return 0; |
|
59 | } |
||
60 | |||
61 | 1 | foreach ($pluginClasses as $pluginClass) { |
|
62 | 1 | $io->writeLine(sprintf('<c1>%s</c1>', $pluginClass)); |
|
63 | } |
||
64 | |||
65 | 1 | return 0; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Handles the "puli plugin --install" command. |
||
70 | * |
||
71 | * @param Args $args The console arguments |
||
72 | * |
||
73 | * @return int The status code |
||
74 | */ |
||
75 | 2 | View Code Duplication | public function handleInstall(Args $args) |
76 | { |
||
77 | 2 | $pluginClass = $args->getArgument('class'); |
|
78 | |||
79 | 2 | if ($this->manager->hasPluginClass($pluginClass)) { |
|
80 | 1 | throw new RuntimeException(sprintf( |
|
81 | 1 | 'The plugin class "%s" is already installed.', |
|
82 | $pluginClass |
||
83 | )); |
||
84 | } |
||
85 | |||
86 | 1 | $this->manager->addPluginClass($pluginClass); |
|
87 | |||
88 | 1 | return 0; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Handles the "puli plugin --remove" command. |
||
93 | * |
||
94 | * @param Args $args The console arguments |
||
95 | * |
||
96 | * @return int The status code |
||
97 | */ |
||
98 | 2 | View Code Duplication | public function handleDelete(Args $args) |
113 | } |
||
114 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.