| Conditions | 39 |
| Paths | 1149 |
| Total Lines | 114 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 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 |
||
| 199 | public function parse($value,$defaultToCurrentTime=true) |
||
| 200 | { |
||
| 201 | if(is_int($value) || is_float($value)) |
||
| 202 | return $value; |
||
| 203 | else if(!is_string($value)) |
||
| 204 | throw new TInvalidDataValueException('date_to_parse_must_be_string', $value); |
||
| 205 | |||
| 206 | if(empty($this->pattern)) return time(); |
||
| 207 | |||
| 208 | $date = time(); |
||
| 209 | |||
| 210 | if($this->length(trim($value)) < 1) |
||
| 211 | return $defaultToCurrentTime ? $date : null; |
||
| 212 | |||
| 213 | $pattern = $this->pattern; |
||
| 214 | |||
| 215 | $i_val = 0; |
||
| 216 | $i_format = 0; |
||
| 217 | $pattern_length = $this->length($pattern); |
||
| 218 | $c = ''; |
||
| 219 | $token=''; |
||
| 220 | $x=null; $y=null; |
||
| 221 | |||
| 222 | |||
| 223 | if($defaultToCurrentTime) |
||
| 224 | { |
||
| 225 | $year = "{$date['year']}"; |
||
| 226 | $month = $date['mon']; |
||
| 227 | $day = $date['mday']; |
||
| 228 | } |
||
| 229 | else |
||
| 230 | { |
||
| 231 | $year = null; |
||
| 232 | $month = null; |
||
| 233 | $day = null; |
||
| 234 | } |
||
| 235 | |||
| 236 | while ($i_format < $pattern_length) |
||
| 237 | { |
||
| 238 | $c = $this->charAt($pattern,$i_format); |
||
| 239 | $token=''; |
||
| 240 | while ($this->charEqual($pattern, $i_format, $c) |
||
| 241 | && ($i_format < $pattern_length)) |
||
| 242 | { |
||
| 243 | $token .= $this->charAt($pattern, $i_format++); |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($token=='yyyy' || $token=='yy' || $token=='y') |
||
| 247 | { |
||
| 248 | if ($token=='yyyy') { $x=4;$y=4; } |
||
| 249 | if ($token=='yy') { $x=2;$y=2; } |
||
| 250 | if ($token=='y') { $x=2;$y=4; } |
||
| 251 | $year = $this->getInteger($value,$i_val,$x,$y); |
||
| 252 | if($year === null) |
||
| 253 | return null; |
||
| 254 | //throw new TInvalidDataValueException('Invalid year', $value); |
||
| 255 | $i_val += strlen($year); |
||
| 256 | if(strlen($year) == 2) |
||
| 257 | { |
||
| 258 | $iYear = (int)$year; |
||
| 259 | if($iYear > 70) |
||
| 260 | $year = $iYear + 1900; |
||
| 261 | else |
||
| 262 | $year = $iYear + 2000; |
||
| 263 | } |
||
| 264 | $year = (int)$year; |
||
| 265 | } |
||
| 266 | elseif($token=='MM' || $token=='M') |
||
| 267 | { |
||
| 268 | $month=$this->getInteger($value,$i_val, |
||
| 269 | $this->length($token),2); |
||
| 270 | $iMonth = (int)$month; |
||
| 271 | if($month === null || $iMonth < 1 || $iMonth > 12 ) |
||
| 272 | return null; |
||
| 273 | //throw new TInvalidDataValueException('Invalid month', $value); |
||
| 274 | $i_val += strlen($month); |
||
| 275 | $month = $iMonth; |
||
| 276 | } |
||
| 277 | elseif ($token=='dd' || $token=='d') |
||
| 278 | { |
||
| 279 | $day = $this->getInteger($value,$i_val, |
||
| 280 | $this->length($token), 2); |
||
| 281 | $iDay = (int)$day; |
||
| 282 | if($day === null || $iDay < 1 || $iDay >31) |
||
| 283 | return null; |
||
| 284 | //throw new TInvalidDataValueException('Invalid day', $value); |
||
| 285 | $i_val += strlen($day); |
||
| 286 | $day = $iDay; |
||
| 287 | } |
||
| 288 | else |
||
| 289 | { |
||
| 290 | if($this->substring($value, $i_val, $this->length($token)) != $token) |
||
| 291 | return null; |
||
| 292 | //throw new TInvalidDataValueException("Subpattern '{$this->pattern}' mismatch", $value); |
||
| 293 | else |
||
| 294 | $i_val += $this->length($token); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | if ($i_val != $this->length($value)) |
||
| 298 | return null; |
||
| 299 | //throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value); |
||
| 300 | if(!$defaultToCurrentTime && ($month === null || $day === null || $year === null)) |
||
| 301 | return null; |
||
| 302 | else |
||
| 303 | { |
||
| 304 | if(empty($year)) { |
||
| 305 | $year = date('Y'); |
||
| 306 | } |
||
| 307 | $day = (int)$day <= 0 ? 1 : (int)$day; |
||
| 308 | $month = (int)$month <= 0 ? 1 : (int)$month; |
||
| 309 | $s = Prado::createComponent('System.Util.TDateTimeStamp'); |
||
| 310 | return $s->getTimeStamp(0, 0, 0, $month, $day, $year); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 370 |
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.