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 |
||
27 | class ViolationMessageRenderer { |
||
28 | |||
29 | /** |
||
30 | * @var EntityIdFormatter |
||
31 | */ |
||
32 | private $entityIdFormatter; |
||
33 | |||
34 | /** |
||
35 | * @var ValueFormatter |
||
36 | */ |
||
37 | private $dataValueFormatter; |
||
38 | |||
39 | /** |
||
40 | * @var MessageLocalizer |
||
41 | */ |
||
42 | protected $messageLocalizer; |
||
43 | |||
44 | /** |
||
45 | * @var Config |
||
46 | */ |
||
47 | private $config; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $maxListLength; |
||
53 | |||
54 | /** |
||
55 | * @param EntityIdFormatter $entityIdFormatter |
||
56 | * @param ValueFormatter $dataValueFormatter |
||
57 | * @param MessageLocalizer $messageLocalizer |
||
58 | * @param Config $config |
||
59 | * @param int $maxListLength The maximum number of elements to be rendered in a list parameter. |
||
60 | * Longer lists are truncated to this length and then rendered with an ellipsis in the HMTL list. |
||
61 | */ |
||
62 | public function __construct( |
||
75 | |||
76 | /** |
||
77 | * @param ViolationMessage $violationMessage |
||
78 | * (temporarily, pre-rendered strings are allowed and returned without changes) |
||
79 | * @return string |
||
80 | */ |
||
81 | public function render( ViolationMessage $violationMessage ) { |
||
94 | |||
95 | /** |
||
96 | * @param string $value HTML |
||
97 | * @param string|null $role one of the Role::* constants |
||
98 | * @return string HTML |
||
99 | */ |
||
100 | protected function addRole( $value, $role ) { |
||
109 | |||
110 | /** |
||
111 | * @param string $key message key |
||
112 | * @return string HTML |
||
113 | */ |
||
114 | protected function msgEscaped( $key ) { |
||
117 | |||
118 | /** |
||
119 | * @param array $argument |
||
120 | * @return array[] params (for Message::params) |
||
121 | */ |
||
122 | protected function renderArgument( array $argument ) { |
||
153 | |||
154 | /** |
||
155 | * @param array $list |
||
156 | * @param string|null $role one of the Role::* constants |
||
157 | * @param callable $render must accept $list elements and $role as parameters |
||
158 | * and return a single-element array with a raw message param (i. e. [ Message::rawParam( … ) ]) |
||
159 | * @return array[] list of parameters as accepted by Message::params() |
||
160 | */ |
||
161 | private function renderList( array $list, $role, callable $render ) { |
||
207 | |||
208 | /** |
||
209 | * @param EntityId $entityId |
||
210 | * @param string|null $role one of the Role::* constants |
||
211 | * @return array[] list of a single raw message param (i. e. [ Message::rawParam( … ) ]) |
||
212 | */ |
||
213 | private function renderEntityId( EntityId $entityId, $role ) { |
||
219 | |||
220 | /** |
||
221 | * @param EntityId[] $entityIdList |
||
222 | * @param string|null $role one of the Role::* constants |
||
223 | * @return array[] list of parameters as accepted by Message::params() |
||
224 | */ |
||
225 | private function renderEntityIdList( array $entityIdList, $role ) { |
||
228 | |||
229 | /** |
||
230 | * @param ItemIdSnakValue $value |
||
231 | * @param string|null $role one of the Role::* constants |
||
232 | * @return array[] list of a single raw message param (i. e. [ Message::rawParam( … ) ]) |
||
233 | */ |
||
234 | private function renderItemIdSnakValue( ItemIdSnakValue $value, $role ) { |
||
260 | |||
261 | /** |
||
262 | * @param ItemIdSnakValue[] $valueList |
||
263 | * @param string|null $role one of the Role::* constants |
||
264 | * @return array[] list of parameters as accepted by Message::params() |
||
265 | */ |
||
266 | private function renderItemIdSnakValueList( array $valueList, $role ) { |
||
269 | |||
270 | /** |
||
271 | * @param DataValue $dataValue |
||
272 | * @param string|null $role one of the Role::* constants |
||
273 | * @return array[] list of parameters as accepted by Message::params() |
||
274 | */ |
||
275 | private function renderDataValue( DataValue $dataValue, $role ) { |
||
281 | |||
282 | /** |
||
283 | * @param string $dataValueType |
||
284 | * @param string|null $role one of the Role::* constants |
||
285 | * @return array[] list of parameters as accepted by Message::params() |
||
286 | */ |
||
287 | private function renderDataValueType( $dataValueType, $role ) { |
||
309 | |||
310 | /** |
||
311 | * @param string $code (not yet HTML-escaped) |
||
312 | * @param string|null $role one of the Role::* constants |
||
313 | * @return array[] list of parameters as accepted by Message::params() |
||
314 | */ |
||
315 | private function renderInlineCode( $code, $role ) { |
||
321 | |||
322 | /** |
||
323 | * @param string $scope one of the Context::TYPE_* constants |
||
324 | * @param string|null $role one of the Role::* constants |
||
325 | * @return array[] list of a single raw message param (i. e. [ Message::rawParam( … ) ]) |
||
326 | */ |
||
327 | View Code Duplication | private function renderConstraintScope( $scope, $role ) { |
|
353 | |||
354 | /** |
||
355 | * @param string[] $text Context::TYPE_* constants |
||
356 | * @param string|null $role one of the Role::* constants |
||
357 | * @return array[] list of parameters as accepted by Message::params() |
||
358 | */ |
||
359 | private function renderConstraintScopeList( array $scopeList, $role ) { |
||
362 | |||
363 | /** |
||
364 | * @param string $scope one of the Context::TYPE_* constants |
||
365 | * @param string|null $role one of the Role::* constants |
||
366 | * @return array[] list of a single raw message param (i. e. [ Message::rawParam( … ) ]) |
||
367 | */ |
||
368 | View Code Duplication | private function renderPropertyScope( $scope, $role ) { |
|
388 | |||
389 | /** |
||
390 | * @param string[] $text Context::TYPE_* constants |
||
391 | * @param string|null $role one of the Role::* constants |
||
392 | * @return array[] list of parameters as accepted by Message::params() |
||
393 | */ |
||
394 | private function renderPropertyScopeList( array $scopeList, $role ) { |
||
397 | |||
398 | /** |
||
399 | * @param string $languageCode MediaWiki language code |
||
400 | * @param string|null $role one of the Role::* constants |
||
401 | * @return array[] list of parameters as accepted by Message::params() |
||
402 | */ |
||
403 | private function renderLanguage( $languageCode, $role ) { |
||
409 | |||
410 | } |
||
411 |
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.