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 |
||
31 | abstract class AbstractEnumType extends Type |
||
32 | { |
||
33 | /** @var string */ |
||
34 | protected $name = ''; |
||
35 | |||
36 | /** |
||
37 | * @var array Array of ENUM Values, where ENUM values are keys and their readable versions are values |
||
38 | * |
||
39 | * @static |
||
40 | */ |
||
41 | protected static $choices = []; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | * |
||
46 | * @throws \InvalidArgumentException |
||
47 | */ |
||
48 | public function convertToDatabaseValue($value, AbstractPlatform $platform) |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function convertToPHPValue($value, AbstractPlatform $platform) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function getName(): string |
||
121 | |||
122 | /** |
||
123 | * Get readable choices for the ENUM field. |
||
124 | * |
||
125 | * @static |
||
126 | * |
||
127 | * @return array Values for the ENUM field |
||
128 | */ |
||
129 | public static function getChoices(): array |
||
133 | |||
134 | /** |
||
135 | * Get values for the ENUM field. |
||
136 | * |
||
137 | * @static |
||
138 | * |
||
139 | * @return array Values for the ENUM field |
||
140 | */ |
||
141 | public static function getValues(): array |
||
145 | |||
146 | /** |
||
147 | * Get random value for the ENUM field. |
||
148 | * |
||
149 | * @static |
||
150 | * |
||
151 | * @return int|string |
||
152 | */ |
||
153 | public static function getRandomValue() |
||
160 | |||
161 | /** |
||
162 | * Get array of ENUM Values, where ENUM values are keys and their readable versions are values. |
||
163 | * |
||
164 | * @static |
||
165 | * |
||
166 | * @return array Array of values with readable format |
||
167 | */ |
||
168 | public static function getReadableValues(): array |
||
172 | |||
173 | /** |
||
174 | * Asserts that given choice exists in the array of ENUM values. |
||
175 | * |
||
176 | * @param string $value ENUM value |
||
177 | * |
||
178 | * @throws \InvalidArgumentException |
||
179 | */ |
||
180 | public static function assertValidChoice(string $value): void |
||
186 | |||
187 | /** |
||
188 | * Get value in readable format. |
||
189 | * |
||
190 | * @param string $value ENUM value |
||
191 | * |
||
192 | * @static |
||
193 | * |
||
194 | * @return string $value Value in readable format |
||
195 | */ |
||
196 | public static function getReadableValue(string $value): string |
||
202 | |||
203 | /** |
||
204 | * Check if some string value exists in the array of ENUM values. |
||
205 | * |
||
206 | * @param string $value ENUM value |
||
207 | * |
||
208 | * @static |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public static function isValueExist(string $value): bool |
||
216 | |||
217 | /** |
||
218 | * Gets an array of database types that map to this Doctrine type. |
||
219 | * |
||
220 | * @param AbstractPlatform $platform |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getMappedDatabaseTypes(AbstractPlatform $platform): array |
||
232 | } |
||
233 |
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.