| Total Complexity | 72 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 formatting pattern. |
||
|
|
|||
| 67 | * @param string pattern and value charset |
||
| 68 | */ |
||
| 69 | public function __construct($pattern, $charset = 'UTF-8') |
||
| 70 | { |
||
| 71 | $this->setPattern($pattern); |
||
| 72 | $this->setCharset($charset); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return string formatting pattern. |
||
| 77 | */ |
||
| 78 | public function getPattern() |
||
| 79 | { |
||
| 80 | return $this->pattern; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string formatting pattern. |
||
| 85 | */ |
||
| 86 | public function setPattern($pattern) |
||
| 87 | { |
||
| 88 | $this->pattern = $pattern; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string formatting charset. |
||
| 93 | */ |
||
| 94 | public function getCharset() |
||
| 95 | { |
||
| 96 | return $this->charset; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string formatting charset. |
||
| 101 | */ |
||
| 102 | public function setCharset($charset) |
||
| 103 | { |
||
| 104 | $this->charset = $charset; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Format the date according to the pattern. |
||
| 109 | * @param string|int the date to format, either integer or a string readable by strtotime. |
||
| 110 | * @return string formatted date. |
||
| 111 | */ |
||
| 112 | public function format($value) |
||
| 113 | { |
||
| 114 | $dt = $this->getDate($value); |
||
| 115 | $bits['yyyy'] = $dt->format('Y'); |
||
| 116 | $bits['yy'] = $dt->format('y'); |
||
| 117 | |||
| 118 | $bits['MM'] = $dt->format('m'); |
||
| 119 | $bits['M'] = $dt->format('n'); |
||
| 120 | |||
| 121 | $bits['dd'] = $dt->format('d'); |
||
| 122 | $bits['d'] = $dt->format('j'); |
||
| 123 | |||
| 124 | $pattern = preg_replace('/M{3,4}/', 'MM', $this->pattern); |
||
| 125 | return str_replace(array_keys($bits), $bits, $pattern); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getMonthPattern() |
||
| 129 | { |
||
| 130 | if(is_int(strpos($this->pattern, 'MMMM'))) |
||
| 131 | return 'MMMM'; |
||
| 132 | if(is_int(strpos($this->pattern, 'MMM'))) |
||
| 133 | return 'MMM'; |
||
| 134 | if(is_int(strpos($this->pattern, 'MM'))) |
||
| 135 | return 'MM'; |
||
| 136 | if(is_int(strpos($this->pattern, 'M'))) |
||
| 137 | return 'M'; |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function getDayPattern() |
||
| 142 | { |
||
| 143 | if(is_int(strpos($this->pattern, 'dd'))) |
||
| 144 | return 'dd'; |
||
| 145 | if(is_int(strpos($this->pattern, 'd'))) |
||
| 146 | return 'd'; |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getYearPattern() |
||
| 151 | { |
||
| 152 | if(is_int(strpos($this->pattern, 'yyyy'))) |
||
| 153 | return 'yyyy'; |
||
| 154 | if(is_int(strpos($this->pattern, 'yy'))) |
||
| 155 | return 'yy'; |
||
| 156 | return false; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function getDayMonthYearOrdering() |
||
| 160 | { |
||
| 161 | $ordering = []; |
||
| 162 | if(is_int($day = strpos($this->pattern, 'd'))) |
||
| 163 | $ordering['day'] = $day; |
||
| 164 | if(is_int($month = strpos($this->pattern, 'M'))) |
||
| 165 | $ordering['month'] = $month; |
||
| 166 | if(is_int($year = strpos($this->pattern, 'yy'))) |
||
| 167 | $ordering['year'] = $year; |
||
| 168 | asort($ordering); |
||
| 169 | return array_keys($ordering); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets the time stamp from string or integer. |
||
| 174 | * @param string|int date to parse |
||
| 175 | * @return array date info array |
||
| 176 | */ |
||
| 177 | private function getDate($value) |
||
| 178 | { |
||
| 179 | if(is_numeric($value)) |
||
| 180 | { |
||
| 181 | $date = new \DateTime; |
||
| 182 | $date->setTimeStamp($value); |
||
| 183 | } else { |
||
| 184 | $date = new \DateTime($value); |
||
| 185 | } |
||
| 186 | return $date; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return boolean true if the given value matches with the date pattern. |
||
| 191 | */ |
||
| 192 | public function isValidDate($value) |
||
| 193 | { |
||
| 194 | if($value === null) { |
||
| 195 | return false; |
||
| 196 | } else { |
||
| 197 | return $this->parse($value, false) !== null; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Parse the string according to the pattern. |
||
| 203 | * @param string|int date string or integer to parse |
||
| 204 | * @return int date time stamp |
||
| 205 | * @throws TInvalidDataValueException if date string is malformed. |
||
| 206 | */ |
||
| 207 | public function parse($value, $defaultToCurrentTime = true) |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Calculate the length of a string, may be consider iconv_strlen? |
||
| 327 | */ |
||
| 328 | private function length($string) |
||
| 329 | { |
||
| 330 | //use iconv_strlen or just strlen? |
||
| 331 | return strlen($string); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the char at a position. |
||
| 336 | */ |
||
| 337 | private function charAt($string, $pos) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Gets a portion of a string, uses iconv_substr. |
||
| 344 | */ |
||
| 345 | private function substring($string, $start, $length) |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns true if char at position equals a particular char. |
||
| 352 | */ |
||
| 353 | private function charEqual($string, $pos, $char) |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Gets integer from part of a string, allows integers of any length. |
||
| 360 | * @param string string to retrieve the integer from. |
||
| 361 | * @param int starting position |
||
| 362 | * @param int minimum integer length |
||
| 363 | * @param int maximum integer length |
||
| 364 | * @return string integer portion of the string, null otherwise |
||
| 365 | */ |
||
| 366 | private function getInteger($str, $i, $minlength, $maxlength) |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths