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 |
||
| 10 | class Number extends Intl |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Array of localized number formatters. |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $formatters; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The current locale. |
||
| 21 | * |
||
| 22 | * @var string $locale |
||
| 23 | */ |
||
| 24 | protected $locale; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The current locale. |
||
| 28 | * |
||
| 29 | * @var string $locale |
||
| 30 | */ |
||
| 31 | protected $fallbackLocale; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Format a number. |
||
| 35 | * |
||
| 36 | * @param string|int|float $number |
||
| 37 | * @param array $options |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | 15 | public function format($number, $options = []) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Format as percentage. |
||
| 49 | * |
||
| 50 | * @param string|int|float $number |
||
| 51 | * @param array $options |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 3 | public function percent($number, $options = []) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Parse a localized number into native PHP format. |
||
| 63 | * |
||
| 64 | * @param string|int|float $number |
||
| 65 | * @param array $options |
||
| 66 | * @return string|false |
||
| 67 | */ |
||
| 68 | 3 | public function parse($number, $options = []) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get the current locale. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 24 | public function getLocale() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set the current locale. |
||
| 87 | * |
||
| 88 | * @param $locale |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | 24 | public function setLocale($locale) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get the fallback locale. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 24 | public function getFallbackLocale() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Set the fallback locale. |
||
| 112 | * |
||
| 113 | * @param $locale |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | 24 | public function setFallbackLocale($locale) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Load the format repository for the given locale. |
||
| 127 | * |
||
| 128 | * @param string $locale |
||
| 129 | * @return void |
||
| 130 | */ |
||
| 131 | 24 | protected function load($locale, $fallbackLocale) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Get the formatter's key. |
||
| 142 | * |
||
| 143 | * @param string|null $locale |
||
| 144 | * @param string|null $fallbackLocale |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | 24 | View Code Duplication | protected function getLocalesKey($locale = null, $fallbackLocale = null) |
| 154 | |||
| 155 | /** |
||
| 156 | * The current number formatter. |
||
| 157 | * |
||
| 158 | * @return \CommerceGuys\Intl\Formatter\NumberFormatter |
||
| 159 | */ |
||
| 160 | 21 | protected function formatter() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Merges the options array. |
||
| 167 | * |
||
| 168 | * @param array $options |
||
| 169 | * @param array $defaults |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 21 | protected function mergeOptions(array $options, array $defaults = []) |
|
| 178 | } |
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: