| Conditions | 10 |
| Paths | 9 |
| Total Lines | 44 |
| Code Lines | 25 |
| 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 |
||
| 72 | public function calcTransitions( $from = null, $to = null ) { |
||
| 73 | |||
| 74 | if ( $this->localTimezones === [] ) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | if ( $from === null || $to === null ){ |
||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | foreach ( $this->localTimezones as $timezone ) { |
||
| 83 | try { |
||
| 84 | $dateTimezone = new DateTimeZone( $timezone ); |
||
| 85 | } catch( Exception $e ) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | $transitions = $dateTimezone->getTransitions(); |
||
| 90 | |||
| 91 | if ( $transitions === false ) { |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | $min = 0; |
||
| 96 | $max = 1; |
||
| 97 | |||
| 98 | foreach ( $transitions as $i => $transition ) { |
||
| 99 | if ( $transition['ts'] < $from ) { |
||
| 100 | $min = $i; |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ( $transition['ts'] > $to ) { |
||
| 105 | $max = $i; |
||
| 106 | break; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->offsets[$timezone] = $transitions[max( $min-1, 0 )]['offset']; |
||
| 111 | $this->transitions[$timezone] = array_slice( $transitions, $min, $max - $min ); |
||
| 112 | } |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 182 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: