| Conditions | 12 |
| Paths | 6 |
| Total Lines | 52 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 45 | public static function seconds( $str ) { |
||
| 46 | |||
| 47 | $hours = 0; |
||
| 48 | $minutes = 0; |
||
| 49 | $seconds = 0; |
||
| 50 | |||
| 51 | if( preg_match('/^\d+:\d+$/', $str) ) { |
||
| 52 | list(, $minutes, $seconds) = explode(':', $str); |
||
| 53 | } |
||
| 54 | elseif( preg_match('/^\d+:\d+:\d+$/', $str) ) { |
||
| 55 | list($hours, $minutes, $seconds) = explode(':', $str); |
||
| 56 | } |
||
| 57 | else { |
||
| 58 | |||
| 59 | // convert invalid characters to spaces |
||
| 60 | $str = preg_replace('/[^a-z0-9. ]+/iu', ' ', $str); |
||
| 61 | |||
| 62 | // strip multiple spaces |
||
| 63 | $str = preg_replace('/ {2,}/u', ' ', $str); |
||
| 64 | |||
| 65 | // compress scales and units together so '2 hours' => '2hours' |
||
| 66 | $str = preg_replace('/([0-9.]+) ([cdehimnorstu]+)/u', '$1$2', $str); |
||
| 67 | |||
| 68 | foreach( explode(' ', $str) as $item ) { |
||
| 69 | |||
| 70 | if( !preg_match('/^([0-9.]+)([cdehimnorstu]+)$/u', $item, $m) ) |
||
| 71 | return false; |
||
| 72 | |||
| 73 | list(, $scale, $unit) = $m; |
||
| 74 | |||
| 75 | $scale = ((float) $scale != (int) $scale) ? (float) $scale : (int) $scale; |
||
| 76 | |||
| 77 | if( preg_match('/^h(r|our|ours)?$/u', $unit) && !$hours ) { |
||
| 78 | $hours = $scale; |
||
| 79 | } |
||
| 80 | elseif( preg_match('/^m(in|ins|inute|inutes)?$/u', $unit) && !$minutes ) { |
||
| 81 | $minutes = $scale; |
||
| 82 | } |
||
| 83 | elseif( preg_match('/^s(ec|ecs|econd|econds)?$/u', $unit) && !$seconds ) { |
||
| 84 | $seconds = $scale; |
||
| 85 | } |
||
| 86 | else { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | } |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | return ($hours * 3600) + ($minutes * 60) + $seconds; |
||
| 95 | |||
| 96 | } |
||
| 97 | |||
| 100 | // EOF |