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 |
||
32 | class ApplicationHelp extends AbstractHelp |
||
33 | { |
||
34 | /** |
||
35 | * @var Application |
||
36 | */ |
||
37 | private $application; |
||
38 | |||
39 | /** |
||
40 | * Creates the help. |
||
41 | * |
||
42 | * @param Application $application The application to render. |
||
43 | */ |
||
44 | 19 | public function __construct(Application $application) |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 19 | protected function renderHelp(BlockLayout $layout) |
|
81 | |||
82 | /** |
||
83 | * Renders the application name. |
||
84 | * |
||
85 | * @param BlockLayout $layout The layout. |
||
86 | * @param Application $application The application. |
||
87 | */ |
||
88 | 19 | protected function renderName(BlockLayout $layout, Application $application) |
|
93 | |||
94 | /** |
||
95 | * Renders the "Usage" section. |
||
96 | * |
||
97 | * @param BlockLayout $layout The layout. |
||
98 | * @param Application $application The application to describe. |
||
99 | * @param ArgsFormat $argsFormat The format of the console arguments. |
||
100 | */ |
||
101 | 19 | protected function renderUsage(BlockLayout $layout, Application $application, ArgsFormat $argsFormat) |
|
113 | |||
114 | /** |
||
115 | * Renders the "Commands" section. |
||
116 | * |
||
117 | * @param BlockLayout $layout The layout. |
||
118 | * @param CommandCollection $commands The commands to describe. |
||
119 | */ |
||
120 | 13 | View Code Duplication | protected function renderCommands(BlockLayout $layout, CommandCollection $commands) |
135 | |||
136 | /** |
||
137 | * Renders a command in the "Commands" section. |
||
138 | * |
||
139 | * @param BlockLayout $layout The layout. |
||
140 | * @param Command $command The command to describe. |
||
141 | */ |
||
142 | 13 | protected function renderCommand(BlockLayout $layout, Command $command) |
|
149 | |||
150 | /** |
||
151 | * Renders the "Description" section. |
||
152 | * |
||
153 | * @param BlockLayout $layout The layout. |
||
154 | * @param string $help The help text. |
||
155 | */ |
||
156 | 1 | protected function renderDescription(BlockLayout $layout, $help) |
|
166 | } |
||
167 |
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.