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 |
||
16 | abstract class FlaggedEnum extends ReadableEnum |
||
17 | { |
||
18 | const NONE = 0; |
||
19 | |||
20 | /** @var array */ |
||
21 | private static $masks = []; |
||
22 | |||
23 | /** @var array */ |
||
24 | private static $readables = []; |
||
25 | |||
26 | /** @var int[] */ |
||
27 | protected $flags; |
||
28 | |||
29 | 30 | /** |
|
30 | * {@inheritdoc} |
||
31 | 30 | */ |
|
32 | 1 | public static function accepts($value): bool |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | 8 | */ |
|
48 | View Code Duplication | public static function readables(): array |
|
62 | 2 | ||
63 | /** |
||
64 | 2 | * {@inheritdoc} |
|
65 | 2 | * |
|
66 | 2 | * @param string $separator A delimiter used between each bit flag's readable string |
|
67 | */ |
||
68 | public static function readableFor($value, string $separator = '; '): string |
||
93 | |||
94 | 26 | /** |
|
95 | 1 | * Gets the human representation for the none value. |
|
96 | 1 | */ |
|
97 | 1 | protected static function readableForNone(): string |
|
101 | 1 | ||
102 | /** |
||
103 | * Gets an integer value of the possible flags for enumeration. |
||
104 | 1 | * |
|
105 | * @throws LogicException If the possibles values are not valid bit flags |
||
106 | */ |
||
107 | private static function getBitmask(): int |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | 13 | * |
|
128 | * @param string $separator A delimiter used between each bit flag's readable string |
||
129 | 13 | */ |
|
130 | 7 | public function getReadable(string $separator = '; '): string |
|
134 | |||
135 | /** |
||
136 | * Gets an array of bit flags of the value. |
||
137 | */ |
||
138 | 13 | public function getFlags(): array |
|
151 | 9 | ||
152 | /** |
||
153 | * Determines whether the specified flag is set in a numeric value. |
||
154 | * |
||
155 | * @param int $bitFlag The bit flag or bit flags |
||
156 | * |
||
157 | * @return bool True if the bit flag or bit flags are also set in the current instance; otherwise, false |
||
158 | */ |
||
159 | public function hasFlag(int $bitFlag): bool |
||
167 | |||
168 | 2 | /** |
|
169 | 1 | * Computes a new value with given flags, and returns the corresponding instance. |
|
170 | * |
||
171 | * @param int $flags The bit flag or bit flags |
||
172 | 1 | * |
|
173 | * @throws InvalidValueException When $flags is not acceptable for this enumeration type |
||
174 | * |
||
175 | * @return static The enum instance for computed value |
||
176 | */ |
||
177 | public function withFlags(int $flags): self |
||
185 | |||
186 | 4 | /** |
|
187 | 1 | * Computes a new value without given flags, and returns the corresponding instance. |
|
188 | * |
||
189 | * @param int $flags The bit flag or bit flags |
||
190 | 3 | * |
|
191 | * @throws InvalidValueException When $flags is not acceptable for this enumeration type |
||
192 | * |
||
193 | * @return static The enum instance for computed value |
||
194 | */ |
||
195 | public function withoutFlags(int $flags): self |
||
203 | } |
||
204 |
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.