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 |
||
47 | class TNumberFormat extends TI18NControl implements \Prado\IDataRenderer |
||
48 | { |
||
49 | /** |
||
50 | * Cached NumberFormatters set to the application culture. |
||
51 | * @var NumberFormat |
||
52 | */ |
||
53 | protected static $formatters; |
||
54 | |||
55 | /** |
||
56 | * Get the number formatting pattern. |
||
57 | * @return string format pattern. |
||
58 | */ |
||
59 | public function getPattern() |
||
63 | |||
64 | /** |
||
65 | * Set the number format pattern. |
||
66 | * @param string $pattern format pattern. |
||
67 | */ |
||
68 | public function setPattern($pattern) |
||
72 | |||
73 | /** |
||
74 | * Get the numberic value for this control. |
||
75 | * @return string number |
||
76 | */ |
||
77 | public function getValue() |
||
81 | |||
82 | /** |
||
83 | * Set the numberic value for this control. |
||
84 | * @param string $value the number value |
||
85 | */ |
||
86 | public function setValue($value) |
||
90 | |||
91 | /** |
||
92 | * Get the default text value for this control. |
||
93 | * @return string default text value |
||
94 | */ |
||
95 | public function getDefaultText() |
||
99 | |||
100 | /** |
||
101 | * Set the default text value for this control. |
||
102 | * @param string $value default text value |
||
103 | */ |
||
104 | public function setDefaultText($value) |
||
108 | |||
109 | /** |
||
110 | * Get the numberic value for this control. |
||
111 | * This method is required by {@link \Prado\IDataRenderer}. |
||
112 | * It is the same as {@link getValue()}. |
||
113 | * @return string number |
||
114 | * @see getValue |
||
115 | * @since 3.1.2 |
||
116 | */ |
||
117 | public function getData() |
||
121 | |||
122 | /** |
||
123 | * Set the numberic value for this control. |
||
124 | * This method is required by {@link \Prado\IDataRenderer}. |
||
125 | * It is the same as {@link setValue()}. |
||
126 | * @param string $value the number value |
||
127 | * @see setValue |
||
128 | * @since 3.1.2 |
||
129 | */ |
||
130 | public function setData($value) |
||
134 | |||
135 | /** |
||
136 | * Get the formatting type for this control. |
||
137 | * @return string formatting type. |
||
138 | */ |
||
139 | public function getType() |
||
143 | |||
144 | /** |
||
145 | * Set the formatting type for this control. |
||
146 | * @param string $type formatting type, either "decimal", "currency", "percentage", "scientific", "spellout", "ordinal" or "duration" |
||
147 | * @throws TPropertyTypeInvalidException |
||
148 | */ |
||
149 | public function setType($type) |
||
172 | |||
173 | /** |
||
174 | * @return string 3 letter currency code. Defaults to 'USD'. |
||
175 | */ |
||
176 | public function getCurrency() |
||
180 | |||
181 | /** |
||
182 | * Set the 3-letter ISO 4217 code. For example, the code |
||
183 | * "USD" represents the US Dollar and "EUR" represents the Euro currency. |
||
184 | * @param string $currency currency code. |
||
185 | */ |
||
186 | public function setCurrency($currency) |
||
190 | |||
191 | /** |
||
192 | * Formats the localized number, be it currency or decimal, or percentage. |
||
193 | * If the culture is not specified, the default application |
||
194 | * culture will be used. |
||
195 | * @param string $culture |
||
196 | * @param string $type |
||
197 | * @return NumberFormatter |
||
198 | */ |
||
199 | protected function getFormatter($culture, $type) |
||
208 | |||
209 | /** |
||
210 | * Formats the localized number, be it currency or decimal, or percentage. |
||
211 | * If the culture is not specified, the default application |
||
212 | * culture will be used. |
||
213 | * @return string formatted number |
||
214 | */ |
||
215 | protected function getFormattedValue() |
||
244 | |||
245 | public function render($writer) |
||
249 | |||
250 | /** |
||
251 | * Convert UTF-8 strings to a different encoding. NB. The result |
||
252 | * may not have been encoded if iconv fails. |
||
253 | * @param string $string the UTF-8 string for conversion |
||
254 | * @param string $to detination encoding |
||
255 | * @return string encoded string. |
||
256 | */ |
||
257 | View Code Duplication | protected function I18N_toEncoding($string, $to) |
|
265 | } |
||
266 |
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.