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 |
||
| 7 | trait TranslatedRouteCommandContext |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Returns whether a given locale is supported. |
||
| 11 | * |
||
| 12 | * @param string $locale |
||
| 13 | * |
||
| 14 | * @return bool |
||
| 15 | */ |
||
| 16 | protected function isSupportedLocale($locale) |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @return string[] |
||
| 23 | */ |
||
| 24 | protected function getSupportedLocales() |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return \RichanFongdasen\I18n\I18nService |
||
| 31 | */ |
||
| 32 | protected function getI18nService() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | protected function getBootstrapPath() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string|null $locale |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | View Code Duplication | protected function makeLocaleRoutesPath($locale = null) |
|
| 55 | { |
||
| 56 | $path = $this->laravel->getCachedRoutesPath(); |
||
| 57 | if ($locale === null) { |
||
| 58 | return $path; |
||
| 59 | } |
||
| 60 | $path = substr($path, 0, -4).'_'.$locale.'.php'; |
||
| 61 | |||
| 62 | return $path; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The env key that the forced locale for routing is stored in. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | protected function getLocaleEnvKey() |
||
| 74 | } |
||
| 75 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: