| Conditions | 39 |
| Paths | 1149 |
| Total Lines | 132 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 219 | public function parse($value, $defaultToCurrentTime = true) |
||
| 220 | { |
||
| 221 | if (is_int($value) || is_float($value)) { |
||
| 222 | return $value; |
||
| 223 | } elseif (!is_string($value)) { |
||
| 224 | throw new TInvalidDataValueException('date_to_parse_must_be_string', $value); |
||
| 225 | } |
||
| 226 | |||
| 227 | if (empty($this->pattern)) { |
||
| 228 | return time(); |
||
| 229 | } |
||
| 230 | |||
| 231 | $date = time(); |
||
| 232 | |||
| 233 | if ($this->length(trim($value)) < 1) { |
||
| 234 | return $defaultToCurrentTime ? $date : null; |
||
| 235 | } |
||
| 236 | |||
| 237 | $pattern = $this->pattern; |
||
| 238 | |||
| 239 | $i_val = 0; |
||
| 240 | $i_format = 0; |
||
| 241 | $pattern_length = $this->length($pattern); |
||
| 242 | $c = ''; |
||
| 243 | $token = ''; |
||
| 244 | $x = null; |
||
| 245 | $y = null; |
||
| 246 | |||
| 247 | |||
| 248 | if ($defaultToCurrentTime) { |
||
| 249 | $year = "{$date['year']}"; |
||
| 250 | $month = $date['mon']; |
||
| 251 | $day = $date['mday']; |
||
| 252 | } else { |
||
| 253 | $year = null; |
||
| 254 | $month = null; |
||
| 255 | $day = null; |
||
| 256 | } |
||
| 257 | |||
| 258 | while ($i_format < $pattern_length) { |
||
| 259 | $c = $this->charAt($pattern, $i_format); |
||
| 260 | $token = ''; |
||
| 261 | while ($this->charEqual($pattern, $i_format, $c) |
||
| 262 | && ($i_format < $pattern_length)) { |
||
| 263 | $token .= $this->charAt($pattern, $i_format++); |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($token == 'yyyy' || $token == 'yy' || $token == 'y') { |
||
| 267 | if ($token == 'yyyy') { |
||
| 268 | $x = 4; |
||
| 269 | $y = 4; |
||
| 270 | } |
||
| 271 | if ($token == 'yy') { |
||
| 272 | $x = 2; |
||
| 273 | $y = 2; |
||
| 274 | } |
||
| 275 | if ($token == 'y') { |
||
| 276 | $x = 2; |
||
| 277 | $y = 4; |
||
| 278 | } |
||
| 279 | $year = $this->getInteger($value, $i_val, $x, $y); |
||
| 280 | if ($year === null) { |
||
| 281 | return null; |
||
| 282 | } |
||
| 283 | //throw new TInvalidDataValueException('Invalid year', $value); |
||
| 284 | $i_val += strlen($year); |
||
| 285 | if (strlen($year) == 2) { |
||
| 286 | $iYear = (int) $year; |
||
| 287 | if ($iYear > 70) { |
||
| 288 | $year = $iYear + 1900; |
||
| 289 | } else { |
||
| 290 | $year = $iYear + 2000; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | $year = (int) $year; |
||
| 294 | } elseif ($token == 'MM' || $token == 'M') { |
||
| 295 | $month = $this->getInteger( |
||
| 296 | $value, |
||
| 297 | $i_val, |
||
| 298 | $this->length($token), |
||
| 299 | 2 |
||
| 300 | ); |
||
| 301 | $iMonth = (int) $month; |
||
| 302 | if ($month === null || $iMonth < 1 || $iMonth > 12) { |
||
| 303 | return null; |
||
| 304 | } |
||
| 305 | //throw new TInvalidDataValueException('Invalid month', $value); |
||
| 306 | $i_val += strlen($month); |
||
| 307 | $month = $iMonth; |
||
| 308 | } elseif ($token == 'dd' || $token == 'd') { |
||
| 309 | $day = $this->getInteger( |
||
| 310 | $value, |
||
| 311 | $i_val, |
||
| 312 | $this->length($token), |
||
| 313 | 2 |
||
| 314 | ); |
||
| 315 | $iDay = (int) $day; |
||
| 316 | if ($day === null || $iDay < 1 || $iDay > 31) { |
||
| 317 | return null; |
||
| 318 | } |
||
| 319 | //throw new TInvalidDataValueException('Invalid day', $value); |
||
| 320 | $i_val += strlen($day); |
||
| 321 | $day = $iDay; |
||
| 322 | } else { |
||
| 323 | if ($this->substring($value, $i_val, $this->length($token)) != $token) { |
||
| 324 | return null; |
||
| 325 | } |
||
| 326 | //throw new TInvalidDataValueException("Subpattern '{$this->pattern}' mismatch", $value); |
||
| 327 | else { |
||
| 328 | $i_val += $this->length($token); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | if ($i_val != $this->length($value)) { |
||
| 333 | return null; |
||
| 334 | } |
||
| 335 | //throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value); |
||
| 336 | if (!$defaultToCurrentTime && ($month === null || $day === null || $year === null)) { |
||
| 337 | return null; |
||
| 338 | } else { |
||
| 339 | if (empty($year)) { |
||
| 340 | $year = date('Y'); |
||
| 341 | } |
||
| 342 | $day = (int) $day <= 0 ? 1 : (int) $day; |
||
| 343 | $month = (int) $month <= 0 ? 1 : (int) $month; |
||
| 344 | |||
| 345 | $s = new \DateTime; |
||
| 346 | $s->setDate($year, $month, $day); |
||
| 347 | $s->setTime(0, 0, 0); |
||
| 348 | return $s->getTimeStamp(); |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 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.