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); |
||
13 | abstract class AbstractCG |
||
14 | { |
||
15 | /** string Character group matching regexp pattern matching group name */ |
||
16 | const PATTERN_GROUP = ''; |
||
17 | |||
18 | /** string Regular expression matching character group */ |
||
19 | const PATTERN_REGEXP = ''; |
||
20 | |||
21 | /** string Character group matching regexp pattern */ |
||
22 | const PATTERN = ''; |
||
23 | |||
24 | /** @var int Character group length */ |
||
25 | protected $length; |
||
26 | |||
27 | /** @var string Character group string */ |
||
28 | protected $string; |
||
29 | |||
30 | /** |
||
31 | * AbstractCharacterGroup constructor. |
||
32 | * |
||
33 | * @param string $string Character group string |
||
34 | * @param int $length Character group length |
||
35 | */ |
||
36 | 23 | public function __construct(string $string, int $length = 0) |
|
41 | |||
42 | /** |
||
43 | * Create character group from input string. |
||
44 | * |
||
45 | * @param string $input Input string |
||
46 | * |
||
47 | * @return NullCG|AbstractCG|FixedCG|VariableCG Character group instance |
||
48 | */ |
||
49 | 19 | public static function fromString(string &$input) |
|
63 | |||
64 | /** |
||
65 | * @return bool True if character group is variable length otherwise false |
||
66 | */ |
||
67 | 1 | public function isVariable(): bool |
|
71 | |||
72 | /** |
||
73 | * Compare character groups. |
||
74 | * |
||
75 | * @param AbstractCG $group Compared character group |
||
76 | * |
||
77 | * @return int -1, 0, 1 Lower, equal, higher |
||
78 | */ |
||
79 | 14 | View Code Duplication | public function compare(AbstractCG $group): int |
94 | |||
95 | /** |
||
96 | * Check if compared character group has same type. |
||
97 | * |
||
98 | * @param AbstractCG $group Compared character group |
||
99 | * |
||
100 | * @return bool True if character group has same type otherwise false |
||
101 | */ |
||
102 | 20 | public function isSameType(AbstractCG $group): bool |
|
106 | |||
107 | /** |
||
108 | * Compare this character group length to compared character group length. |
||
109 | * |
||
110 | * @param AbstractCG $group Compared character group |
||
111 | * |
||
112 | * @return int -1, 0, 1 Character groups comparison result |
||
113 | */ |
||
114 | abstract protected function compareLength(AbstractCG $group): int; |
||
115 | |||
116 | /** |
||
117 | * @return bool True if character group is fixed length otherwise false |
||
118 | */ |
||
119 | 7 | public function isFixed(): bool |
|
123 | |||
124 | /** |
||
125 | * Get two character groups longest common prefix. |
||
126 | * |
||
127 | * @param AbstractCG $group Compared character group |
||
128 | * |
||
129 | * @return string Longest common prefix or empty string |
||
130 | */ |
||
131 | abstract public function getCommonPrefix(AbstractCG $group): string; |
||
132 | |||
133 | /** |
||
134 | * @return string Character group string |
||
135 | */ |
||
136 | 2 | public function getString(): string |
|
140 | } |
||
141 |
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.