| Conditions | 15 |
| Paths | 200 |
| Total Lines | 71 |
| 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 |
||
| 101 | protected function generateVTimeZoneComponent(VCalendar $vcalendar, \DateTimeZone $timeZone, int $from = 0, int $to = 0) |
||
| 102 | { |
||
| 103 | if (! $from) { |
||
| 104 | $from = time(); |
||
| 105 | } |
||
| 106 | if (! $to) { |
||
| 107 | $to = $from; |
||
| 108 | } |
||
| 109 | |||
| 110 | // get all transitions for one year back/ahead |
||
| 111 | $year = 86400 * 360; |
||
| 112 | $transitions = $timeZone->getTransitions($from - $year, $to + $year); |
||
| 113 | |||
| 114 | $vTimeZone = $vcalendar->createComponent('VTIMEZONE'); |
||
| 115 | $vTimeZone->TZID = $timeZone->getName(); |
||
| 116 | |||
| 117 | $std = null; |
||
| 118 | $dst = null; |
||
| 119 | foreach ($transitions as $i => $trans) { |
||
| 120 | $component = null; |
||
|
|
|||
| 121 | |||
| 122 | if ($i === 0) { |
||
| 123 | // remember the offset for the next TZOFFSETFROM value |
||
| 124 | $tzfrom = $trans['offset'] / 3600; |
||
| 125 | } |
||
| 126 | |||
| 127 | // daylight saving time definition |
||
| 128 | if ($trans['isdst']) { |
||
| 129 | $t_dst = $trans['ts']; |
||
| 130 | $dst = $vcalendar->createComponent('DAYLIGHT'); |
||
| 131 | $component = $dst; |
||
| 132 | } // standard time definition |
||
| 133 | else { |
||
| 134 | $t_std = $trans['ts']; |
||
| 135 | $std = $vcalendar->createComponent('STANDARD'); |
||
| 136 | $component = $std; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ($component) { |
||
| 140 | $dt = new \DateTime($trans['time']); |
||
| 141 | $offset = $trans['offset'] / 3600; |
||
| 142 | |||
| 143 | $component->DTSTART = $dt->format('Ymd\THis'); |
||
| 144 | $component->TZOFFSETFROM = sprintf('%s%02d%02d', $tzfrom >= 0 ? '+' : '', floor($tzfrom), |
||
| 145 | ($tzfrom - floor($tzfrom)) * 60); |
||
| 146 | $component->TZOFFSETTO = sprintf('%s%02d%02d', $offset >= 0 ? '+' : '', floor($offset), |
||
| 147 | ($offset - floor($offset)) * 60); |
||
| 148 | |||
| 149 | // add abbreviated timezone name if available |
||
| 150 | if (! empty($trans['abbr'])) { |
||
| 151 | $component->TZNAME = $trans['abbr']; |
||
| 152 | } |
||
| 153 | |||
| 154 | $tzfrom = $offset; |
||
| 155 | $vTimeZone->add($component); |
||
| 156 | } |
||
| 157 | |||
| 158 | // we covered the entire date range |
||
| 159 | if ($std && $dst && min($t_std, $t_dst) < $from && max($t_std, $t_dst) > $to) { |
||
| 160 | break; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | // add X-MICROSOFT-CDO-TZID if available |
||
| 165 | $microsoftExchangeMap = array_flip(TimeZoneUtil::$microsoftExchangeMap); |
||
| 166 | if (array_key_exists($timeZone->getName(), $microsoftExchangeMap)) { |
||
| 167 | $vTimeZone->add('X-MICROSOFT-CDO-TZID', $microsoftExchangeMap[$timeZone->getName()]); |
||
| 168 | } |
||
| 169 | |||
| 170 | return $vTimeZone; |
||
| 171 | } |
||
| 172 | } |
||
| 173 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.