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 |
||
39 | class AdditionalAttributeCsvSerializer extends AbstractCsvSerializer |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The factory instance for the CSV value serializer. |
||
44 | * |
||
45 | * @var \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface |
||
46 | */ |
||
47 | private $valueCsvSerializerFactory; |
||
48 | |||
49 | /** |
||
50 | * The CSV value serializer instance. |
||
51 | * |
||
52 | * @var \TechDivision\Import\Serializers\SerializerInterface |
||
53 | */ |
||
54 | private $valueCsvSerializer; |
||
55 | |||
56 | /** |
||
57 | * The entity type from the configuration. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $entityType; |
||
62 | |||
63 | /** |
||
64 | * The configuration instance. |
||
65 | * |
||
66 | * @var \TechDivision\Import\ConfigurationInterface |
||
67 | */ |
||
68 | private $configuration; |
||
69 | |||
70 | /** |
||
71 | * The convert processor instance. |
||
72 | * |
||
73 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
74 | */ |
||
75 | private $importProcessor; |
||
76 | |||
77 | /** |
||
78 | * Initialize the serializer with the passed CSV value serializer factory. |
||
79 | * |
||
80 | * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
||
81 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The processor instance |
||
82 | * @param \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory |
||
83 | */ |
||
84 | 10 | public function __construct( |
|
98 | |||
99 | /** |
||
100 | * Returns the configuration instance. |
||
101 | * |
||
102 | * @return \TechDivision\Import\ConfigurationInterface The configuration instance |
||
103 | */ |
||
104 | 10 | protected function getConfiguration() |
|
108 | |||
109 | /** |
||
110 | * Returns the factory instance for the CSV value serializer. |
||
111 | * |
||
112 | * @return \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface The CSV value serializer factory instance |
||
113 | */ |
||
114 | 10 | protected function getValueCsvSerializerFactory() |
|
118 | |||
119 | /** |
||
120 | * Returns the CSV value serializer instance. |
||
121 | * |
||
122 | * @param \TechDivision\Import\Serializers\SerializerInterface $valueCsvSerializer The CSV value serializer instance |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | 10 | protected function setValueCsvSerializer(SerializerInterface $valueCsvSerializer) |
|
130 | |||
131 | /** |
||
132 | * Returns the CSV value serializer instance. |
||
133 | * |
||
134 | * @return \TechDivision\Import\Serializers\SerializerInterface The CSV value serializer instance |
||
135 | */ |
||
136 | 10 | protected function getValueCsvSerializer() |
|
140 | |||
141 | /** |
||
142 | * Returns the import processor instance. |
||
143 | * |
||
144 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
145 | */ |
||
146 | 8 | protected function getImportProcessor() |
|
150 | |||
151 | /** |
||
152 | * Returns entity type ID mapped from the configuration. |
||
153 | * |
||
154 | * @return integer The mapped entity type ID |
||
155 | */ |
||
156 | 8 | protected function getEntityTypeId() |
|
160 | |||
161 | /** |
||
162 | * Returns the multiple value delimiter from the configuration. |
||
163 | * |
||
164 | * @return string The multiple value delimiter |
||
165 | */ |
||
166 | 4 | protected function getMultipleValueDelimiter() |
|
170 | |||
171 | /** |
||
172 | * Returns the multiple field delimiter from the configuration. |
||
173 | * |
||
174 | * @return string The multiple field delimiter |
||
175 | */ |
||
176 | 10 | protected function getMultipleFieldDelimiter() |
|
180 | |||
181 | /** |
||
182 | * Loads and returns the attribute with the passed code from the database. |
||
183 | * |
||
184 | * @param string $attributeCode The code of the attribute to return |
||
185 | * |
||
186 | * @return array The attribute |
||
187 | */ |
||
188 | 8 | protected function loadAttributeByAttributeCode($attributeCode) |
|
192 | |||
193 | /** |
||
194 | * Packs the passed value according to the frontend input type of the attribute with the passed code. |
||
195 | * |
||
196 | * @param string $attributeCode The code of the attribute to pack the passed value for |
||
197 | * @param mixed $value The value to pack |
||
198 | * |
||
199 | * @return string The packed value |
||
200 | */ |
||
201 | 3 | View Code Duplication | protected function pack($attributeCode, $value) |
221 | |||
222 | /** |
||
223 | * Unpacks the passed value according to the frontend input type of the attribute with the passed code. |
||
224 | * |
||
225 | * @param string $attributeCode The code of the attribute to pack the passed value for |
||
226 | * @param string $value The value to unpack |
||
227 | * |
||
228 | * @return mixed The unpacked value |
||
229 | */ |
||
230 | 5 | View Code Duplication | protected function unpack($attributeCode, $value) |
231 | { |
||
232 | |||
233 | // load the attibute with the passed code |
||
234 | 5 | $attribute = $this->loadAttributeByAttributeCode($attributeCode); |
|
235 | |||
236 | // unpack the value according to the attribute's frontend input type |
||
237 | 5 | switch ($attribute[MemberNames::FRONTEND_INPUT]) { |
|
238 | 5 | case FrontendInputTypes::MULTISELECT: |
|
239 | 3 | return explode($this->getMultipleValueDelimiter(), $value); |
|
240 | break; |
||
241 | |||
242 | 5 | case FrontendInputTypes::BOOLEAN: |
|
243 | 3 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
244 | break; |
||
245 | |||
246 | default: |
||
247 | 5 | return $value; |
|
248 | } |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Passes the configuration and initializes the serializer. |
||
253 | * |
||
254 | * @param \TechDivision\Import\Configuration\CsvConfigurationInterface $configuration The CSV configuration |
||
255 | * |
||
256 | * @return void |
||
257 | */ |
||
258 | 10 | public function init(CsvConfigurationInterface $configuration) |
|
267 | |||
268 | /** |
||
269 | * Unserializes the elements of the passed string. |
||
270 | * |
||
271 | * @param string|null $serialized The value to unserialize |
||
272 | * @param string|null $delimiter The delimiter used to unserialize the elements |
||
273 | * |
||
274 | * @return array The unserialized values |
||
275 | * @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
||
276 | */ |
||
277 | 6 | public function unserialize($serialized = null, $delimiter = null) |
|
298 | |||
299 | /** |
||
300 | * Serializes the elements of the passed array. |
||
301 | * |
||
302 | * @param array|null $unserialized The serialized data |
||
303 | * @param string|null $delimiter The delimiter used to serialize the values |
||
304 | * |
||
305 | * @return string The serialized array |
||
306 | * @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
||
307 | */ |
||
308 | 4 | public function serialize(array $unserialized = null, $delimiter = null) |
|
324 | |||
325 | /** |
||
326 | * Extracts the elements of the passed value by exploding them |
||
327 | * with the also passed delimiter. |
||
328 | * |
||
329 | * @param string|null $value The value to extract |
||
330 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
331 | * |
||
332 | * @return array|null The exploded values |
||
333 | * @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
||
334 | */ |
||
335 | 5 | public function explode($value = null, $delimiter = null) |
|
339 | |||
340 | /** |
||
341 | * Compacts the elements of the passed value by imploding them |
||
342 | * with the also passed delimiter. |
||
343 | * |
||
344 | * @param array|null $value The values to compact |
||
345 | * @param string|null $delimiter The delimiter use to implode the values |
||
346 | * |
||
347 | * @return string|null The compatected value |
||
348 | * @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
||
349 | */ |
||
350 | public function implode(array $value = null, $delimiter = null) |
||
354 | } |
||
355 |
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.