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 |
||
19 | View Code Duplication | class LabelSetter { |
|
|
|||
20 | |||
21 | /** |
||
22 | * @var WikibaseApi |
||
23 | */ |
||
24 | private $api; |
||
25 | |||
26 | /** |
||
27 | * @param WikibaseApi $api |
||
28 | */ |
||
29 | public function __construct( WikibaseApi $api ) { |
||
32 | |||
33 | /** |
||
34 | * @since 0.2 |
||
35 | * |
||
36 | * @param Term $label |
||
37 | * @param EntityId|Item|Property|SiteLink $target |
||
38 | * @param EditInfo|null $editInfo |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | public function set( Term $label, $target, EditInfo $editInfo = null ) { |
||
55 | |||
56 | /** |
||
57 | * @param mixed $target |
||
58 | * |
||
59 | * @throws UnexpectedValueException |
||
60 | * |
||
61 | * @todo Fix duplicated code |
||
62 | */ |
||
63 | private function throwExceptionsOnBadTarget( $target ) { |
||
71 | |||
72 | /** |
||
73 | * @param EntityId|Item|Property $target |
||
74 | * |
||
75 | * @throws UnexpectedValueException |
||
76 | * @return EntityId|SiteLink |
||
77 | * |
||
78 | * @todo Fix duplicated code |
||
79 | */ |
||
80 | private function getEntityIdentifierFromTarget( $target ) { |
||
87 | |||
88 | /** |
||
89 | * @param EntityId|SiteLink $target |
||
90 | * |
||
91 | * @throws UnexpectedValueException |
||
92 | * @return array |
||
93 | * |
||
94 | * @todo Fix duplicated code |
||
95 | */ |
||
96 | private function getTargetParamsFromTarget( $target ) { |
||
108 | |||
109 | } |
||
110 |
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.