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 TSimpleDateFormatter 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 TSimpleDateFormatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class TSimpleDateFormatter |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Formatting pattern. |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $pattern; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Charset, default is 'UTF-8' |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $charset = 'UTF-8'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor, create a new date time formatter. |
||
| 66 | * @param string $pattern formatting pattern. |
||
| 67 | * @param string $charset pattern and value charset |
||
| 68 | */ |
||
| 69 | public function __construct($pattern, $charset = 'UTF-8') |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string formatting pattern. |
||
| 77 | */ |
||
| 78 | public function getPattern() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $pattern formatting pattern. |
||
| 85 | */ |
||
| 86 | public function setPattern($pattern) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string formatting charset. |
||
| 93 | */ |
||
| 94 | public function getCharset() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $charset formatting charset. |
||
| 101 | */ |
||
| 102 | public function setCharset($charset) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Format the date according to the pattern. |
||
| 109 | * @param int|string $value the date to format, either integer or a string readable by strtotime. |
||
| 110 | * @return string formatted date. |
||
| 111 | */ |
||
| 112 | public function format($value) |
||
| 127 | |||
| 128 | public function getMonthPattern() |
||
| 144 | |||
| 145 | View Code Duplication | public function getDayPattern() |
|
| 155 | |||
| 156 | View Code Duplication | public function getYearPattern() |
|
| 166 | |||
| 167 | public function getDayMonthYearOrdering() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Gets the time stamp from string or integer. |
||
| 185 | * @param int|string $value date to parse |
||
| 186 | * @return array date info array |
||
| 187 | */ |
||
| 188 | private function getDate($value) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param mixed $value |
||
| 201 | * @return bool true if the given value matches with the date pattern. |
||
| 202 | */ |
||
| 203 | public function isValidDate($value) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Parse the string according to the pattern. |
||
| 214 | * @param int|string $value date string or integer to parse |
||
| 215 | * @param bool $defaultToCurrentTime |
||
| 216 | * @throws TInvalidDataValueException if date string is malformed. |
||
| 217 | * @return int date time stamp |
||
| 218 | */ |
||
| 219 | public function parse($value, $defaultToCurrentTime = true) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Calculate the length of a string, may be consider iconv_strlen? |
||
| 354 | * @param mixed $string |
||
| 355 | */ |
||
| 356 | private function length($string) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Get the char at a position. |
||
| 364 | * @param mixed $string |
||
| 365 | * @param mixed $pos |
||
| 366 | */ |
||
| 367 | private function charAt($string, $pos) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Gets a portion of a string, uses iconv_substr. |
||
| 374 | * @param mixed $string |
||
| 375 | * @param mixed $start |
||
| 376 | * @param mixed $length |
||
| 377 | */ |
||
| 378 | private function substring($string, $start, $length) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Returns true if char at position equals a particular char. |
||
| 385 | * @param mixed $string |
||
| 386 | * @param mixed $pos |
||
| 387 | * @param mixed $char |
||
| 388 | */ |
||
| 389 | private function charEqual($string, $pos, $char) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Gets integer from part of a string, allows integers of any length. |
||
| 396 | * @param string $str string to retrieve the integer from. |
||
| 397 | * @param int $i starting position |
||
| 398 | * @param int $minlength minimum integer length |
||
| 399 | * @param int $maxlength maximum integer length |
||
| 400 | * @return string integer portion of the string, null otherwise |
||
| 401 | */ |
||
| 402 | private function getInteger($str, $i, $minlength, $maxlength) |
||
| 416 | } |
||
| 417 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.