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 $converterProcessor The processor instance |
||
|
|
|||
| 82 | * @param \TechDivision\Import\Serializers\ConfigurationAwareSerializerFactoryInterface $valueCsvSerializerFactory The CSV value serializer factory |
||
| 83 | */ |
||
| 84 | 8 | public function __construct( |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the configuration instance. |
||
| 101 | * |
||
| 102 | * @return \TechDivision\Import\ConfigurationInterface The configuration instance |
||
| 103 | */ |
||
| 104 | 8 | 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 | 8 | 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 | 8 | 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 | 8 | protected function getValueCsvSerializer() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Returns the import processor instance. |
||
| 143 | * |
||
| 144 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 145 | */ |
||
| 146 | 6 | 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 | 6 | protected function getEntityTypeId() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the multiple value delimiter from the configuration. |
||
| 163 | * |
||
| 164 | * @return string The multiple value delimiter |
||
| 165 | */ |
||
| 166 | 2 | protected function getMultipleValueDelimiter() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the multiple field delimiter from the configuration. |
||
| 173 | * |
||
| 174 | * @return string The multiple field delimiter |
||
| 175 | */ |
||
| 176 | 8 | 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 | 6 | 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 | 3 | View Code Duplication | protected function unpack($attributeCode, $value) |
| 251 | |||
| 252 | /** |
||
| 253 | * Passes the configuration and initializes the serializer. |
||
| 254 | * |
||
| 255 | * @param \TechDivision\Import\Configuration\CsvConfigurationInterface $configuration The CSV configuration |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | 8 | public function init(CsvConfigurationInterface $configuration) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Unserializes the elements of the passed string. |
||
| 271 | * |
||
| 272 | * @param string|null $serialized The value to unserialize |
||
| 273 | * @param string|null $delimiter The delimiter used to unserialize the elements |
||
| 274 | * |
||
| 275 | * @return array The unserialized values |
||
| 276 | * @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
||
| 277 | */ |
||
| 278 | 4 | public function unserialize($serialized = null, $delimiter = null) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Serializes the elements of the passed array. |
||
| 302 | * |
||
| 303 | * @param array|null $unserialized The serialized data |
||
| 304 | * @param string|null $delimiter The delimiter used to serialize the values |
||
| 305 | * |
||
| 306 | * @return string The serialized array |
||
| 307 | * @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
||
| 308 | */ |
||
| 309 | 4 | public function serialize(array $unserialized = null, $delimiter = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Extracts the elements of the passed value by exploding them |
||
| 328 | * with the also passed delimiter. |
||
| 329 | * |
||
| 330 | * @param string|null $value The value to extract |
||
| 331 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
| 332 | * |
||
| 333 | * @return array|null The exploded values |
||
| 334 | * @see \TechDivision\Import\Serializers\SerializerInterface::unserialize() |
||
| 335 | */ |
||
| 336 | 3 | public function explode($value = null, $delimiter = null) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Compacts the elements of the passed value by imploding them |
||
| 343 | * with the also passed delimiter. |
||
| 344 | * |
||
| 345 | * @param array|null $value The values to compact |
||
| 346 | * @param string|null $delimiter The delimiter use to implode the values |
||
| 347 | * |
||
| 348 | * @return string|null The compatected value |
||
| 349 | * @see \TechDivision\Import\Serializers\SerializerInterface::serialize() |
||
| 350 | */ |
||
| 351 | public function implode(array $value = null, $delimiter = null) |
||
| 355 | } |
||
| 356 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.