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 |
||
19 | class Locale extends Model |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | protected $table = 'locales'; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | protected $fillable = [ |
||
30 | 'label', |
||
31 | 'code', |
||
32 | 'locale', |
||
33 | 'country', |
||
34 | 'fallback_code', |
||
35 | 'is_default', |
||
36 | 'is_editable', |
||
37 | 'is_publishable', |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | protected $casts = [ |
||
44 | 'is_default' => 'boolean', |
||
45 | 'is_editable' => 'boolean', |
||
46 | 'is_publishable' => 'boolean', |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Return default locale code. |
||
51 | * |
||
52 | * @return string |
||
53 | */ |
||
54 | View Code Duplication | public static function default(): string |
|
72 | |||
73 | |||
74 | public static function getAppOrDefaultLocale(): string |
||
78 | |||
79 | public static function getAppOrDefaultCountry($key = 'app.country'): string |
||
83 | |||
84 | /** |
||
85 | * Return default country code. |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | View Code Duplication | public static function defaultCountry(): string |
|
106 | |||
107 | /** |
||
108 | * Return fallback code for given locale code. |
||
109 | * |
||
110 | * @param string $code |
||
111 | * @return string |
||
112 | */ |
||
113 | public static function fallback(string $code): string |
||
131 | |||
132 | public static function canBeSave(string $country, string $locale): bool |
||
136 | |||
137 | protected static function _getLocalesDisabled(): array |
||
142 | |||
143 | public function isEnabled(): bool |
||
147 | |||
148 | View Code Duplication | public static function getLocale(string $locale): string |
|
162 | |||
163 | View Code Duplication | public static function getCountry(string $locale): string |
|
177 | |||
178 | |||
179 | } |
||
180 |
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.