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:
Complex classes like localization often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use localization, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class localization |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * The version of this class |
||
30 | * @var string |
||
31 | */ |
||
32 | private $classVersion = '0.4.1'; |
||
33 | |||
34 | /** |
||
35 | * Saves the current timezone settings |
||
36 | * @var \DateTimeZone |
||
37 | */ |
||
38 | public $timezone = null; |
||
39 | |||
40 | /** |
||
41 | * The timezoneId we are in |
||
42 | * @var string |
||
43 | */ |
||
44 | public $timezoneId = 'UTC'; |
||
45 | |||
46 | /** |
||
47 | * Indicated whether the timezone is in DST or not |
||
48 | * @var boolean |
||
49 | */ |
||
50 | public $timezoneInDST = false; |
||
51 | |||
52 | /** |
||
53 | * Contains the most probable language associated with the selected locale (2 letter code) |
||
54 | * @var string |
||
55 | */ |
||
56 | public $language = ''; |
||
57 | |||
58 | /** |
||
59 | * Which are the most probable timezone candidates for the current setted locale |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $_timezoneCandidates = array(); |
||
63 | |||
64 | /** |
||
65 | * Contains the value of the current setted locale |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $_currentLocale = ''; |
||
69 | |||
70 | /** |
||
71 | * Contains a list of valid timezones |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $_validTimeZones = array(); |
||
75 | |||
76 | /** |
||
77 | * Constructor, will call setLocale internally |
||
78 | * |
||
79 | * @param string $setLocale |
||
80 | */ |
||
81 | public function __construct($setLocale = '') |
||
85 | |||
86 | /** |
||
87 | * Returns a string with basic information of this class |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function __toString() |
||
95 | |||
96 | /** |
||
97 | * Sets the locale to the given locale |
||
98 | * |
||
99 | * This function will also set the "old" setlocale, mainly timezone support for PHP5.3 - PHP5.5 |
||
100 | * |
||
101 | * @param string $locale Locale we want to set in RFC 4646 format |
||
102 | * @return string Returns the setted locale |
||
103 | */ |
||
104 | public function setDefault($locale) |
||
117 | |||
118 | /** |
||
119 | * Gets the default setted locale |
||
120 | */ |
||
121 | public function getDefault() |
||
126 | |||
127 | /** |
||
128 | * Sets some options |
||
129 | * |
||
130 | * @param string $locale |
||
|
|||
131 | * @return boolean |
||
132 | */ |
||
133 | private function _setOptions() |
||
139 | |||
140 | /** |
||
141 | * Sends header to browser containing charset and contentType (mandatory) |
||
142 | * |
||
143 | * @param string $charset Charset to send to browser. Defaults to UTF-8 |
||
144 | * @param string $contentType ContentType to send to browser. Defaults to text/html |
||
145 | * @return boolean Returns true if headers could be sent, false otherwise |
||
146 | */ |
||
147 | public function sendHeaders($charset = 'UTF-8', $contentType = 'text/html') |
||
162 | |||
163 | /** |
||
164 | * Detects on basis of the browser settings which locale we should apply |
||
165 | * |
||
166 | * @return string Returns the setted locale |
||
167 | */ |
||
168 | public function autodetectLocale() |
||
176 | |||
177 | /** |
||
178 | * Applies simple rules to print a number |
||
179 | * |
||
180 | * @link http://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatstyle |
||
181 | * |
||
182 | * @param float $value |
||
183 | * @param constant $type One of the accepted constants for numfmt_create (See link) |
||
184 | * @param int $minimumDigits The minimum significant digits. Defaults to -1, which equals locale default |
||
185 | * @param int $maximumDigits The maximum significant digits. Defaults to -1, which equals locale default |
||
186 | * @return string Returns the given value formatted according to current locale |
||
187 | */ |
||
188 | public function formatNumber($value = 0, $type = 0, $minimumDigits = -1, $maximumDigits = -1) |
||
205 | |||
206 | /** |
||
207 | * Returns the 3-letter (ISO 4217) currency code of the current locale |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getCurrencyISOCode() |
||
216 | |||
217 | /** |
||
218 | * Returns a intlDateFormatter object |
||
219 | * |
||
220 | * @param int $dateConstant |
||
221 | * @param int $timeConstant |
||
222 | * @param string $timezone |
||
223 | * @return \IntlDateFormatter Returns a \IntlDateFormatter object with given options |
||
224 | */ |
||
225 | private function _getDateObject( |
||
239 | |||
240 | /** |
||
241 | * Returns a formatted medium-typed date |
||
242 | * |
||
243 | * @TODO Implement printing locale in another locale |
||
244 | * |
||
245 | * @param int $value The UNIX timestamp we want to print. Defaults to current time |
||
246 | * @param string $timezone The timezone for which we want the information |
||
247 | * @param int $type The type of date we want to print |
||
248 | * @param string $locale The locale in which we want the date |
||
249 | * @return string The formatted date according to current locale settings |
||
250 | */ |
||
251 | View Code Duplication | public function formatSimpleDate($value = 0, $timezone = '', $type = \IntlDateFormatter::MEDIUM, $locale = '') |
|
260 | |||
261 | /** |
||
262 | * Returns a formatted medium-typed time |
||
263 | * |
||
264 | * @TODO Implement printing locale in another locale |
||
265 | * |
||
266 | * @param string $timezone The timezone in which we want to print the time |
||
267 | * @param int $value The UNIX timestamp we want to print. Defaults to current time |
||
268 | * @param int $type The type of time we want to print |
||
269 | * @param string $locale The locale in which we want the time |
||
270 | * @return string The formatted time according to current locale settings |
||
271 | */ |
||
272 | View Code Duplication | public function formatSimpleTime($value = 0, $timezone = '', $type = \intlDateFormatter::MEDIUM) |
|
281 | |||
282 | /** |
||
283 | * Gets the possible timezone candidates and if only found one, instantiates a DateTimeZone object |
||
284 | * |
||
285 | * @param string $region |
||
286 | */ |
||
287 | private function _setTimezoneCandidates($region = '') |
||
297 | |||
298 | /** |
||
299 | * Gets timezone candidates for the current locale |
||
300 | * |
||
301 | * @TODO Improve this |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | public function getTimeZoneCandidates() |
||
309 | |||
310 | /** |
||
311 | * Verifies that the given timezone exists and sets the timezone to the selected timezone |
||
312 | * |
||
313 | * @TODO Verify that the given timezone exists |
||
314 | * |
||
315 | * @param string $timezoneName |
||
316 | * @return string |
||
317 | */ |
||
318 | public function setTimezone($timeZoneName = 'UTC') |
||
331 | |||
332 | /** |
||
333 | * Checks whether a timezonename is valid or not |
||
334 | * |
||
335 | * @param string $timeZoneName |
||
336 | * @return boolean Returns true if timezone name is valid, false otherwise |
||
337 | */ |
||
338 | public function isValidTimeZone($timeZoneName = '') |
||
352 | |||
353 | /** |
||
354 | * Gets the offset for the current timezone |
||
355 | * |
||
356 | * @param string $unit |
||
357 | * @param string $when |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getTimezoneOffset($unit = 'seconds', $when = 'now') |
||
390 | } |
||
391 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.