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 |
||
31 | abstract class AbstractHelp implements Component |
||
32 | { |
||
33 | /** |
||
34 | * Renders the usage. |
||
35 | * |
||
36 | * @param IO $io The I/O. |
||
37 | * @param int $indentation The number of spaces to indent. |
||
38 | */ |
||
39 | 46 | public function render(IO $io, $indentation = 0) |
|
40 | { |
||
41 | 46 | $layout = new BlockLayout(); |
|
42 | |||
43 | 46 | $this->renderHelp($layout); |
|
44 | |||
45 | 46 | $layout->render($io, $indentation); |
|
46 | 46 | } |
|
47 | |||
48 | /** |
||
49 | * Renders the usage. |
||
50 | * |
||
51 | * Overwrite this class in your sub-classes to implement the actual |
||
52 | * rendering. |
||
53 | * |
||
54 | * @param BlockLayout $layout The layout. |
||
55 | */ |
||
56 | abstract protected function renderHelp(BlockLayout $layout); |
||
57 | |||
58 | /** |
||
59 | * Renders a list of arguments. |
||
60 | * |
||
61 | * @param BlockLayout $layout The layout. |
||
62 | * @param Argument[] $arguments The arguments to render. |
||
63 | */ |
||
64 | 23 | View Code Duplication | protected function renderArguments(BlockLayout $layout, array $arguments) |
76 | |||
77 | /** |
||
78 | * Renders an argument. |
||
79 | * |
||
80 | * @param BlockLayout $layout The layout. |
||
81 | * @param Argument $argument The argument to render. |
||
82 | */ |
||
83 | 25 | protected function renderArgument(BlockLayout $layout, Argument $argument) |
|
95 | |||
96 | /** |
||
97 | * Renders a list of options. |
||
98 | * |
||
99 | * @param BlockLayout $layout The layout. |
||
100 | * @param Option[] $options The options to render. |
||
101 | */ |
||
102 | 9 | View Code Duplication | protected function renderOptions(BlockLayout $layout, array $options) |
114 | |||
115 | /** |
||
116 | * Renders a list of global options. |
||
117 | * |
||
118 | * @param BlockLayout $layout The layout. |
||
119 | * @param Option[] $options The global options to render. |
||
120 | */ |
||
121 | 28 | View Code Duplication | protected function renderGlobalOptions(BlockLayout $layout, array $options) |
133 | |||
134 | /** |
||
135 | * Renders an option. |
||
136 | * |
||
137 | * @param BlockLayout $layout The layout. |
||
138 | * @param Option $option The option to render. |
||
139 | */ |
||
140 | 34 | protected function renderOption(BlockLayout $layout, Option $option) |
|
169 | |||
170 | /** |
||
171 | * Renders the synopsis of a console command. |
||
172 | * |
||
173 | * @param BlockLayout $layout The layout. |
||
174 | * @param ArgsFormat $argsFormat The console arguments to render. |
||
175 | * @param string $appName The name of the application binary. |
||
176 | * @param string $prefix The prefix to insert. |
||
177 | * @param bool $lastOptional Set to `true` if the last command of the |
||
178 | * console arguments is optional. This |
||
179 | * command will be enclosed in brackets in |
||
180 | * the output. |
||
181 | */ |
||
182 | 46 | protected function renderSynopsis(BlockLayout $layout, ArgsFormat $argsFormat, $appName, $prefix = '', $lastOptional = false) |
|
239 | |||
240 | /** |
||
241 | * Formats the default value of an argument or an option. |
||
242 | * |
||
243 | * @param mixed $value The default value to format. |
||
244 | * |
||
245 | * @return string The formatted value. |
||
246 | */ |
||
247 | 1 | protected function formatValue($value) |
|
255 | } |
||
256 |
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@return
doc comment to communicate to implementors of these methods what they are expected to return.