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 namespace Limoncello\Flute\Validation\Execution; |
||
| 27 | class JsonApiRuleSerializer |
||
| 28 | { |
||
| 29 | /** Serialized indexes key */ |
||
| 30 | const SERIALIZED_RULE_SETS = 0; |
||
| 31 | |||
| 32 | /** Serialized rules key */ |
||
| 33 | const SERIALIZED_BLOCKS = self::SERIALIZED_RULE_SETS + 1; |
||
| 34 | |||
| 35 | /** Index key */ |
||
| 36 | const ID_SERIALIZED = 0; |
||
| 37 | |||
| 38 | /** Index key */ |
||
| 39 | const TYPE_SERIALIZED = self::ID_SERIALIZED + 1; |
||
| 40 | |||
| 41 | /** Index key */ |
||
| 42 | const ATTRIBUTES_SERIALIZED = self::TYPE_SERIALIZED + 1; |
||
| 43 | |||
| 44 | /** Index key */ |
||
| 45 | const TO_ONE_SERIALIZED = self::ATTRIBUTES_SERIALIZED + 1; |
||
| 46 | |||
| 47 | /** Index key */ |
||
| 48 | const TO_MANY_SERIALIZED = self::TO_ONE_SERIALIZED + 1; |
||
| 49 | |||
| 50 | // Single rule serialization keys |
||
| 51 | |||
| 52 | /** Index key */ |
||
| 53 | const SINGLE_RULE_INDEX = 0; |
||
| 54 | |||
| 55 | /** Index key */ |
||
| 56 | const SINGLE_RULE_START_INDEXES = self::SINGLE_RULE_INDEX + 1; |
||
| 57 | |||
| 58 | /** Index key */ |
||
| 59 | const SINGLE_RULE_END_INDEXES = self::SINGLE_RULE_START_INDEXES + 1; |
||
| 60 | |||
| 61 | // Rules array serialization keys |
||
| 62 | |||
| 63 | /** Index key */ |
||
| 64 | const RULES_ARRAY_INDEXES = 0; |
||
| 65 | |||
| 66 | /** Index key */ |
||
| 67 | const RULES_ARRAY_START_INDEXES = self::RULES_ARRAY_INDEXES + 1; |
||
| 68 | |||
| 69 | /** Index key */ |
||
| 70 | const RULES_ARRAY_END_INDEXES = self::RULES_ARRAY_START_INDEXES + 1; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var BlockSerializerInterface |
||
| 74 | */ |
||
| 75 | private $blockSerializer; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | private $ruleSets; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Constructor. |
||
| 84 | */ |
||
| 85 | public function __construct() |
||
| 90 | |||
| 91 | /** @noinspection PhpTooManyParametersInspection |
||
| 92 | * @param string $name |
||
| 93 | * @param RuleInterface $idRule |
||
| 94 | * @param RuleInterface $typeRule |
||
| 95 | * @param RuleInterface[] $attributeRules |
||
| 96 | * @param RuleInterface[] $toOneRules |
||
| 97 | * @param RuleInterface[] $toManyRules |
||
| 98 | * |
||
| 99 | * @return self |
||
| 100 | */ |
||
| 101 | public function addResourceRules( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return array |
||
| 132 | * |
||
| 133 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 134 | */ |
||
| 135 | public function getData(): array |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $name |
||
| 145 | * @param array $data |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public static function extractRuleSet(string $name, array $data): array |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param array $data |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public static function extractBlocks(array $data): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param array $ruleSet |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | View Code Duplication | public static function getIdRule(array $ruleSet): array |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $ruleSet |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | View Code Duplication | public static function getTypeRule(array $ruleSet): array |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param array $ruleSet |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | View Code Duplication | public static function getAttributeRules(array $ruleSet): array |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @param array $ruleSet |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | View Code Duplication | public static function getToOneRules(array $ruleSet): array |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param array $ruleSet |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | View Code Duplication | public static function getToManyRules(array $ruleSet): array |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param array $serializedRule |
||
| 244 | * |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | View Code Duplication | public static function getRuleIndex(array $serializedRule): int |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param array $serializedRule |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | View Code Duplication | public static function getRuleStartIndexes(array $serializedRule): array |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $serializedRule |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | View Code Duplication | public static function getRuleEndIndexes(array $serializedRule): array |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $serializedRules |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public static function getRulesIndexes(array $serializedRules): array |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param array $serializedRules |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public static function getRulesStartIndexes(array $serializedRules): array |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param array $serializedRules |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public static function getRulesEndIndexes(array $serializedRules): array |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param int $index |
||
| 328 | * @param array $blocks |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public static function isRuleExist(int $index, array $blocks): bool |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param array $serializedRules |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | * |
||
| 344 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 345 | */ |
||
| 346 | public static function getBlocks(array $serializedRules): array |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return BlockSerializerInterface |
||
| 355 | */ |
||
| 356 | protected function getSerializer(): BlockSerializerInterface |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @return BlockSerializerInterface |
||
| 363 | */ |
||
| 364 | protected function createBlockSerializer(): BlockSerializerInterface |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param RuleInterface $rule |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | private function serializeRule(RuleInterface $rule): array |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param RuleInterface[] $rules |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | private function serializeRulesArray(array $rules): array |
||
| 411 | } |
||
| 412 |
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.