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 |
||
20 | class ViolationMessageSerializer implements Serializer { |
||
21 | |||
22 | private function abbreviateViolationMessageKey( $fullMessageKey ) { |
||
25 | |||
26 | /** |
||
27 | * @param ViolationMessage $object |
||
28 | * @return array |
||
29 | */ |
||
30 | public function serialize( $object ) { |
||
45 | |||
46 | /** |
||
47 | * @param array $argument element of ViolationMessage::getArguments() |
||
48 | * @return array [ 't' => ViolationMessage::TYPE_*, 'v' => serialized value, 'r' => $role ] |
||
49 | */ |
||
50 | private function serializeArgument( array $argument ) { |
||
88 | |||
89 | /** |
||
90 | * @param string $string any value that shall simply be serialized to itself |
||
91 | * @return string that same value, unchanged |
||
92 | */ |
||
93 | private function serializeStringByIdentity( $string ) { |
||
97 | |||
98 | /** |
||
99 | * @param EntityId $entityId |
||
100 | * @return string entity ID serialization |
||
101 | */ |
||
102 | private function serializeEntityId( EntityId $entityId ) { |
||
105 | |||
106 | /** |
||
107 | * @param EntityId[] $entityIdList |
||
108 | * @return string[] entity ID serializations |
||
109 | */ |
||
110 | private function serializeEntityIdList( array $entityIdList ) { |
||
113 | |||
114 | /** |
||
115 | * @param ItemIdSnakValue $value |
||
116 | * @return string entity ID serialization, '::somevalue', or '::novalue' |
||
117 | * (according to EntityId::PATTERN, entity ID serializations can never begin with two colons) |
||
118 | */ |
||
119 | private function serializeItemIdSnakValue( ItemIdSnakValue $value ) { |
||
135 | |||
136 | /** |
||
137 | * @param ItemIdSnakValue[] $valueList |
||
138 | * @return string[] array of entity ID serializations, '::somevalue's or '::novalue's |
||
139 | */ |
||
140 | private function serializeItemIdSnakValueList( array $valueList ) { |
||
143 | |||
144 | /** |
||
145 | * @param DataValue $dataValue |
||
146 | * @return array the data value in array form |
||
147 | */ |
||
148 | private function serializeDataValue( DataValue $dataValue ) { |
||
151 | |||
152 | /** |
||
153 | * @param string $contextType one of the Context::TYPE_* constants |
||
154 | * @return string the abbreviated context type |
||
155 | */ |
||
156 | View Code Duplication | private function serializeContextType( $contextType ) { |
|
172 | |||
173 | /** |
||
174 | * @param string[] $contextTypeList Context::TYPE_* constants |
||
175 | * @return string[] abbreviated context types |
||
176 | */ |
||
177 | private function serializeContextTypeList( array $contextTypeList ) { |
||
180 | |||
181 | /** |
||
182 | * @param MultilingualTextValue $text |
||
183 | * @return mixed {@see MultilingualTextValue::getArrayValue} |
||
184 | */ |
||
185 | private function serializeMultilingualText( MultilingualTextValue $text ) { |
||
188 | |||
189 | } |
||
190 |
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.