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 |
||
22 | class ViolationMessageDeserializer implements Deserializer { |
||
23 | |||
24 | /** |
||
25 | * @var EntityIdParser |
||
26 | */ |
||
27 | private $entityIdParser; |
||
28 | |||
29 | /** |
||
30 | * @var DataValueFactory |
||
31 | */ |
||
32 | private $dataValueFactory; |
||
33 | |||
34 | public function __construct( |
||
41 | |||
42 | public function unabbreviateViolationMessageKey( $messageKeySuffix ) { |
||
45 | |||
46 | /** |
||
47 | * @param array $serialization |
||
48 | * @return ViolationMessage |
||
49 | */ |
||
50 | public function deserialize( $serialization ) { |
||
63 | |||
64 | /** |
||
65 | * @param ViolationMessage $message |
||
66 | * @param array $serializedArgument [ 't' => ViolationMessage::TYPE_*, 'v' => serialized value, 'r' => $role ] |
||
67 | * @return ViolationMessage $message with the deserialized argument appended |
||
68 | */ |
||
69 | private function deserializeArgument( ViolationMessage $message, array $serializedArgument ) { |
||
101 | |||
102 | /** |
||
103 | * @param string $string any value that shall simply be deserialized into itself |
||
104 | * @return string that same value, unchanged |
||
105 | */ |
||
106 | private function deserializeStringByIdentity( $string ) { |
||
109 | |||
110 | /** |
||
111 | * @param string $entityIdSerialization entity ID serialization |
||
112 | * @return EntityId |
||
113 | */ |
||
114 | private function deserializeEntityId( $entityIdSerialization ) { |
||
117 | |||
118 | /** |
||
119 | * @param string[] $entityIdSerializations entity ID serializations |
||
120 | * @return EntityId[] |
||
121 | */ |
||
122 | private function deserializeEntityIdList( array $entityIdSerializations ) { |
||
125 | |||
126 | /** |
||
127 | * @param string $valueSerialization entity ID serialization, '::somevalue' or '::novalue' |
||
128 | * @return ItemIdSnakValue |
||
129 | */ |
||
130 | private function deserializeItemIdSnakValue( $valueSerialization ) { |
||
140 | |||
141 | /** |
||
142 | * @param string[] $valueSerializations entity ID serializations, '::somevalue's or '::novalue's |
||
143 | * @return ItemIdSnakValue[] |
||
144 | */ |
||
145 | private function deserializeItemIdSnakValueList( $valueSerializations ) { |
||
148 | |||
149 | /** |
||
150 | * @param array $dataValueSerialization the data value in array form |
||
151 | * @return DataValue |
||
152 | */ |
||
153 | private function deserializeDataValue( array $dataValueSerialization ) { |
||
156 | |||
157 | /** |
||
158 | * @param string $scopeAbbreviation |
||
159 | * @return string one of the Context::TYPE_* constants |
||
160 | */ |
||
161 | View Code Duplication | private function deserializeConstraintScope( $scopeAbbreviation ) { |
|
177 | |||
178 | /** |
||
179 | * @param string[] $scopeAbbreviations |
||
180 | * @return string[] Context::TYPE_* constants |
||
181 | */ |
||
182 | private function deserializeConstraintScopeList( array $scopeAbbreviations ) { |
||
185 | |||
186 | /** |
||
187 | * @param string $scopeAbbreviation |
||
188 | * @return string one of the Context::TYPE_* constants |
||
189 | */ |
||
190 | View Code Duplication | private function deserializePropertyScope( $scopeAbbreviation ) { |
|
206 | |||
207 | /** |
||
208 | * @param string[] $scopeAbbreviations |
||
209 | * @return string[] Context::TYPE_* constants |
||
210 | */ |
||
211 | private function deserializePropertyScopeList( array $scopeAbbreviations ) { |
||
214 | |||
215 | /** |
||
216 | * @param mixed $textSerialization {@see MultilingualTextValue::getArrayValue} |
||
217 | * @return MultilingualTextValue |
||
218 | */ |
||
219 | private function deserializeMultilingualText( $textSerialization ) { |
||
222 | |||
223 | } |
||
224 |
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.