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 |
||
30 | class CommandHelp extends AbstractHelp |
||
31 | { |
||
32 | /** |
||
33 | * @var Command |
||
34 | */ |
||
35 | private $command; |
||
36 | |||
37 | /** |
||
38 | * Creates the help. |
||
39 | * |
||
40 | * @param Command $command The command to render. |
||
41 | */ |
||
42 | 27 | public function __construct(Command $command) |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 27 | protected function renderHelp(BlockLayout $layout) |
|
51 | { |
||
52 | 27 | $help = $this->command->getConfig()->getHelp(); |
|
53 | 27 | $argsFormat = $this->command->getArgsFormat(); |
|
54 | 27 | $subCommands = $this->command->getNamedSubCommands(); |
|
55 | |||
56 | 27 | $this->renderUsage($layout, $this->command); |
|
57 | |||
58 | 27 | if ($argsFormat->hasArguments()) { |
|
59 | 4 | $this->renderArguments($layout, $argsFormat->getArguments()); |
|
60 | } |
||
61 | |||
62 | 27 | if (!$subCommands->isEmpty()) { |
|
63 | 8 | $this->renderSubCommands($layout, $subCommands); |
|
64 | } |
||
65 | |||
66 | 27 | if ($argsFormat->hasOptions(false)) { |
|
67 | 9 | $this->renderOptions($layout, $argsFormat->getOptions(false)); |
|
68 | } |
||
69 | |||
70 | 27 | if ($argsFormat->getBaseFormat() && $argsFormat->getBaseFormat()->hasOptions()) { |
|
71 | 14 | $this->renderGlobalOptions($layout, $argsFormat->getBaseFormat()->getOptions()); |
|
72 | } |
||
73 | |||
74 | 27 | if ($help) { |
|
75 | 1 | $this->renderDescription($layout, $help); |
|
76 | } |
||
77 | 27 | } |
|
78 | |||
79 | /** |
||
80 | * Renders the "Usage" section. |
||
81 | * |
||
82 | * @param BlockLayout $layout The layout. |
||
83 | * @param Command $command The command to render. |
||
84 | */ |
||
85 | 27 | protected function renderUsage(BlockLayout $layout, Command $command) |
|
86 | { |
||
87 | 27 | $formatsToPrint = array(); |
|
88 | |||
89 | // Start with the default commands |
||
90 | 27 | if ($command->hasDefaultSubCommands()) { |
|
91 | // If the command has default commands, print them |
||
92 | 4 | foreach ($command->getDefaultSubCommands() as $subCommand) { |
|
93 | // The name of the sub command is only optional (i.e. printed |
||
94 | // wrapped in brackets: "[sub]") if the command is not |
||
95 | // anonymous |
||
96 | 4 | $nameOptional = !$subCommand->getConfig()->isAnonymous(); |
|
97 | |||
98 | 4 | $formatsToPrint[] = array($subCommand->getArgsFormat(), $nameOptional); |
|
99 | } |
||
100 | } else { |
||
101 | // Otherwise print the command's usage itself |
||
102 | 23 | $formatsToPrint[] = array($command->getArgsFormat(), false); |
|
103 | } |
||
104 | |||
105 | // Add remaining sub-commands |
||
106 | 27 | foreach ($command->getSubCommands() as $subCommand) { |
|
107 | // Don't duplicate default commands |
||
108 | 8 | if (!$subCommand->getConfig()->isDefault()) { |
|
109 | 8 | $formatsToPrint[$subCommand->getName()] = array($subCommand->getArgsFormat(), false); |
|
110 | } |
||
111 | } |
||
112 | |||
113 | 27 | $appName = $command->getApplication()->getConfig()->getName(); |
|
114 | 27 | $prefix = count($formatsToPrint) > 1 ? ' ' : ''; |
|
115 | |||
116 | 27 | $layout->add(new Paragraph('<b>USAGE</b>')); |
|
117 | 27 | $layout->beginBlock(); |
|
118 | |||
119 | 27 | foreach ($formatsToPrint as $vars) { |
|
120 | 27 | $this->renderSynopsis($layout, $vars[0], $appName, $prefix, $vars[1]); |
|
121 | 27 | $prefix = 'or: '; |
|
122 | } |
||
123 | |||
124 | 27 | if ($command->hasAliases()) { |
|
125 | 1 | $layout->add(new EmptyLine()); |
|
126 | 1 | $this->renderAliases($layout, $command->getAliases()); |
|
127 | } |
||
128 | |||
129 | 27 | $layout->endBlock(); |
|
130 | 27 | $layout->add(new EmptyLine()); |
|
131 | 27 | } |
|
132 | |||
133 | /** |
||
134 | * Renders the aliases of a command. |
||
135 | * |
||
136 | * @param BlockLayout $layout The layout. |
||
137 | * @param string[] $aliases The aliases to render. |
||
138 | */ |
||
139 | 1 | protected function renderAliases(BlockLayout $layout, $aliases) |
|
143 | |||
144 | /** |
||
145 | * Renders the "Commands" section. |
||
146 | * |
||
147 | * @param BlockLayout $layout The layout. |
||
148 | * @param CommandCollection $subCommands The sub-commands to render. |
||
149 | */ |
||
150 | 8 | View Code Duplication | protected function renderSubCommands(BlockLayout $layout, CommandCollection $subCommands) |
164 | |||
165 | /** |
||
166 | * Renders a sub-command in the "Commands" section. |
||
167 | * |
||
168 | * @param BlockLayout $layout The layout. |
||
169 | * @param Command $command The command to render. |
||
170 | */ |
||
171 | 8 | protected function renderSubCommand(BlockLayout $layout, Command $command) |
|
222 | |||
223 | /** |
||
224 | * Renders the description of a sub-command. |
||
225 | * |
||
226 | * @param BlockLayout $layout The layout. |
||
227 | * @param string $description The description. |
||
228 | */ |
||
229 | 7 | protected function renderSubCommandDescription(BlockLayout $layout, $description) |
|
234 | |||
235 | /** |
||
236 | * Renders the help text of a sub-command. |
||
237 | * |
||
238 | * @param BlockLayout $layout The layout. |
||
239 | * @param string $help The help text. |
||
240 | */ |
||
241 | protected function renderSubCommandHelp(BlockLayout $layout, $help) |
||
246 | |||
247 | /** |
||
248 | * Renders the argument descriptions of a sub-command. |
||
249 | * |
||
250 | * @param BlockLayout $layout The layout. |
||
251 | * @param Argument[] $arguments The arguments. |
||
252 | */ |
||
253 | 4 | protected function renderSubCommandArguments(BlockLayout $layout, array $arguments) |
|
261 | |||
262 | /** |
||
263 | * Renders the option descriptions of a sub-command. |
||
264 | * |
||
265 | * @param BlockLayout $layout The layout. |
||
266 | * @param Option[] $options The options. |
||
267 | */ |
||
268 | 2 | protected function renderSubCommandOptions(BlockLayout $layout, array $options) |
|
276 | |||
277 | /** |
||
278 | * Renders the "Description" section. |
||
279 | * |
||
280 | * @param BlockLayout $layout The layout. |
||
281 | * @param string $help The help text. |
||
282 | */ |
||
283 | 1 | protected function renderDescription(BlockLayout $layout, $help) |
|
293 | } |
||
294 |
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.