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 FixedVariableCG extends AbstractCG |
||
16 | { |
||
17 | /** string Character group matching regexp pattern matching group name */ |
||
18 | const PATTERN_GROUP = 'fixedVariable'; |
||
19 | |||
20 | /** string Character group matching regexp pattern */ |
||
21 | const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.FixedCG::PATTERN_REGEXP.VariableCG::PATTERN_REGEXP.')'; |
||
22 | |||
23 | /** @var VariableCG */ |
||
24 | protected $variableCG; |
||
25 | |||
26 | /** @var FixedCG */ |
||
27 | protected $fixedCG; |
||
28 | |||
29 | /** |
||
30 | * @inheritdoc |
||
31 | */ |
||
32 | 20 | View Code Duplication | public function __construct(string $string, int $length = 0) |
40 | |||
41 | /** |
||
42 | * Get variable character group common prefix, if exists then |
||
43 | * append fixed character group prefix. |
||
44 | * |
||
45 | * @inheritdoc |
||
46 | */ |
||
47 | 2 | public function getCommonPrefix(AbstractCG $group): string |
|
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | 14 | public function compare(AbstractCG $group): int |
|
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | * @param AbstractCG|FixedVariableCG|FixedVariableCG|VariableFixedCG $group |
||
94 | */ |
||
95 | 10 | View Code Duplication | protected function compareLength(AbstractCG $group): int |
105 | |||
106 | /** |
||
107 | * Longer fixed character group has higher priority. |
||
108 | * |
||
109 | * @param FixedVariableCG $group Compared character group |
||
110 | * |
||
111 | * @return int Comparison result |
||
112 | */ |
||
113 | 10 | private function compareFixed(FixedVariableCG $group): int |
|
118 | } |
||
119 |
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.