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 declare(strict_types=1); |
||
15 | class VariableFixedCG extends AbstractCharacterGroup |
||
16 | { |
||
17 | /** string Character group matching regexp pattern matching group name */ |
||
18 | const PATTERN_GROUP = 'variableFixed'; |
||
19 | |||
20 | /** string Character group matching regexp pattern */ |
||
21 | const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.VariableCG::PATTERN_REGEXP.FixedCG::PATTERN_REGEXP.')'; |
||
22 | |||
23 | /** @var VariableCG */ |
||
24 | protected $variableCG; |
||
25 | |||
26 | /** @var FixedCG */ |
||
27 | protected $fixedCG; |
||
28 | |||
29 | /** |
||
30 | * @inheritdoc |
||
31 | */ |
||
32 | 15 | public function __construct(string $string, int $length = 0) |
|
42 | |||
43 | /** |
||
44 | * @return bool True if character group is fixed length otherwise false |
||
45 | */ |
||
46 | public function isFixed(): bool |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 12 | View Code Duplication | public function compare(AbstractCharacterGroup $group): int |
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 11 | protected function compareLength(AbstractCharacterGroup $group): int |
|
89 | |||
90 | /** |
||
91 | * Longer fixed character group has higher priority. |
||
92 | * |
||
93 | * @param VariableFixedCG $group Compared character group |
||
94 | * |
||
95 | * @return int Comparison result |
||
96 | */ |
||
97 | 11 | private function compareFixed(VariableFixedCG $group): int |
|
102 | } |
||
103 |
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.