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 |
||
32 | abstract class AbstractEnumType extends Type |
||
33 | { |
||
34 | /** @var string */ |
||
35 | protected $name = ''; |
||
36 | |||
37 | /** |
||
38 | * @var array|mixed[] Array of ENUM Values, where ENUM values are keys and their readable versions are values |
||
39 | * |
||
40 | * @static |
||
41 | */ |
||
42 | protected static $choices = []; |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | * |
||
47 | * @throws InvalidArgumentException |
||
48 | */ |
||
49 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
||
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function convertToPHPValue($value, AbstractPlatform $platform) |
||
80 | |||
81 | /** |
||
82 | * Gets the SQL declaration snippet for a field of this type. |
||
83 | * |
||
84 | * @param mixed[] $fieldDeclaration The field declaration |
||
85 | * @param AbstractPlatform $platform The currently used database platform |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function getName(): string |
||
138 | |||
139 | /** |
||
140 | * Get readable choices for the ENUM field. |
||
141 | * |
||
142 | * @static |
||
143 | * |
||
144 | * @return mixed[] |
||
145 | */ |
||
146 | public static function getChoices(): array |
||
150 | |||
151 | /** |
||
152 | * Get values for the ENUM field. |
||
153 | * |
||
154 | * @static |
||
155 | * |
||
156 | * @return mixed[] Values for the ENUM field |
||
157 | */ |
||
158 | public static function getValues(): array |
||
162 | |||
163 | /** |
||
164 | * Get random value for the ENUM field. |
||
165 | * |
||
166 | * @static |
||
167 | * |
||
168 | * @return int|string |
||
169 | */ |
||
170 | public static function getRandomValue() |
||
177 | |||
178 | /** |
||
179 | * Get array of ENUM Values, where ENUM values are keys and their readable versions are values. |
||
180 | * |
||
181 | * @static |
||
182 | * |
||
183 | * @return mixed[] Array of values in readable format |
||
184 | */ |
||
185 | public static function getReadableValues(): array |
||
189 | |||
190 | /** |
||
191 | * Asserts that given choice exists in the array of ENUM values. |
||
192 | * |
||
193 | * @param mixed $value ENUM value |
||
194 | * |
||
195 | * @throws InvalidArgumentException |
||
196 | */ |
||
197 | public static function assertValidChoice($value): void |
||
198 | { |
||
199 | View Code Duplication | if (!isset(static::$choices[$value])) { |
|
200 | throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM type "%s".', (string) $value, static::class)); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get value in readable format. |
||
206 | * |
||
207 | * @param mixed $value ENUM value |
||
208 | * |
||
209 | * @static |
||
210 | * |
||
211 | * @return mixed Value in readable format |
||
212 | */ |
||
213 | public static function getReadableValue($value) |
||
219 | |||
220 | /** |
||
221 | * Check if some string value exists in the array of ENUM values. |
||
222 | * |
||
223 | * @param mixed $value ENUM value |
||
224 | * |
||
225 | * @static |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | public static function isValueExist($value): bool |
||
233 | |||
234 | /** |
||
235 | * Get default value for DDL statement. |
||
236 | * |
||
237 | * @static |
||
238 | * |
||
239 | * @return mixed|null Default value for DDL statement |
||
240 | */ |
||
241 | public static function getDefaultValue() |
||
245 | |||
246 | /** |
||
247 | * Gets an array of database types that map to this Doctrine type. |
||
248 | * |
||
249 | * @param AbstractPlatform $platform |
||
250 | * |
||
251 | * @return string[] |
||
252 | */ |
||
253 | public function getMappedDatabaseTypes(AbstractPlatform $platform): array |
||
261 | } |
||
262 |
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.