| Conditions | 15 |
| Paths | 816 |
| Total Lines | 128 |
| 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 |
||
| 120 | protected function createSpreadSheet() |
||
| 121 | { |
||
| 122 | $shifts = $this->shifts; |
||
| 123 | $dept = $this->department; |
||
| 124 | $ssheat = new Spreadsheet(); |
||
| 125 | $sheat = $ssheat->getActiveSheet(); |
||
| 126 | $sheat->setCellValue('A1', $dept['departmentName']); |
||
| 127 | $count = count($shifts); |
||
| 128 | $days = array(); |
||
| 129 | $roles = array(); |
||
| 130 | $roles2 = array(); |
||
| 131 | for($i = 0; $i < $count; $i++) |
||
| 132 | { |
||
| 133 | $start = new \DateTime($shifts[$i]['startTime']); |
||
| 134 | $end = new \DateTime($shifts[$i]['endTime']); |
||
| 135 | $shifts[$i]['startTime'] = $start; |
||
| 136 | $shifts[$i]['endTime'] = $end; |
||
| 137 | $startDateStr = $start->format('l (n/j/Y)'); |
||
| 138 | $endDateStr = $end->format('l (n/j/Y)'); |
||
| 139 | $days[$startDateStr] = 1; |
||
| 140 | $days[$endDateStr] = 1; |
||
| 141 | $diff = $start->diff($end); |
||
| 142 | $shifts[$i]['length'] = $diff->h; |
||
| 143 | if(!isset($roles[$shifts[$i]['roleID']])) |
||
| 144 | { |
||
| 145 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 146 | $roles2[$shifts[$i]['roleID']] = array(); |
||
| 147 | } |
||
| 148 | else |
||
| 149 | { |
||
| 150 | if($roles[$shifts[$i]['roleID']] < $shifts[$i]['length']) |
||
| 151 | { |
||
| 152 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | array_push($roles2[$shifts[$i]['roleID']], array('start'=>$start, 'end'=>$end)); |
||
| 156 | } |
||
| 157 | arsort($roles); |
||
| 158 | usort($shifts, array($this, 'shiftTimeSort')); |
||
| 159 | $originalStartTime = $shifts[0]['startTime']; |
||
| 160 | $str = $shifts[0]['startTime']->format('c'); |
||
| 161 | $start = date_parse($str); |
||
| 162 | $lastShift = $shifts[$count - 1]; |
||
| 163 | $interval = $lastShift['endTime']->diff($shifts[0]['startTime']); |
||
| 164 | $hourCount = ($interval->d*24) + $interval->h; |
||
| 165 | $tmp = $this->getHoursArrays($start['hour'], $hourCount); |
||
| 166 | $simpleHours = $tmp[0]; |
||
| 167 | $militaryHours = $tmp[1]; |
||
| 168 | $sheat->fromArray($simpleHours, null, 'B2'); |
||
| 169 | $sheat->fromArray($militaryHours, null, 'B3'); |
||
| 170 | $mergeCount = 24 - $start['hour']; |
||
| 171 | if($mergeCount > $hourCount) |
||
| 172 | { |
||
| 173 | $mergeCount = $hourCount; |
||
| 174 | } |
||
| 175 | $days = array_keys($days); |
||
| 176 | $cellIndex = 2; |
||
| 177 | while($mergeCount) |
||
| 178 | { |
||
| 179 | $sheat->mergeCellsByColumnAndRow($cellIndex, 1, $cellIndex + $mergeCount - 1, 1); |
||
| 180 | $sheat->setCellValueByColumnAndRow($cellIndex, 1, array_shift($days)); |
||
| 181 | $cell = $sheat->getCellByColumnAndRow($cellIndex, 1); |
||
| 182 | $cell->getStyle()->getAlignment()->setHorizontal('center'); |
||
| 183 | $cellIndex += $mergeCount; |
||
| 184 | $hourCount -= $mergeCount; |
||
| 185 | $mergeCount = $hourCount; |
||
| 186 | if($mergeCount > 24) |
||
| 187 | { |
||
| 188 | $mergeCount = 24; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $i = 0; |
||
| 192 | $rows = array(); |
||
| 193 | foreach($roles as $role=>$hour) |
||
| 194 | { |
||
| 195 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 196 | array_push($rows, $role); |
||
| 197 | $overlaps = array(); |
||
| 198 | for($j = 0; $j < count($roles2[$role]) - 1; $j++) |
||
| 199 | { |
||
| 200 | $currRole = $roles2[$role][$j]; |
||
| 201 | $nextRole = $roles2[$role][$j + 1]; |
||
| 202 | if($currRole['end'] > $nextRole['start']) |
||
| 203 | { |
||
| 204 | $str = $currRole['start']->format('c'); |
||
| 205 | if(!isset($overlaps[$str])) |
||
| 206 | { |
||
| 207 | $overlaps[$str] = 0; |
||
| 208 | } |
||
| 209 | $overlaps[$str]++; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | if(!empty($overlaps)) |
||
| 213 | { |
||
| 214 | $overlapCount = max(array_values($overlaps)); |
||
| 215 | for($j = 0; $j < $overlapCount + 1; $j++) |
||
| 216 | { |
||
| 217 | $i++; |
||
| 218 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 219 | if($j > 0) |
||
| 220 | { |
||
| 221 | array_push($rows, $role); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | else |
||
| 226 | { |
||
| 227 | $i++; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | $shift = array_shift($shifts); |
||
| 231 | while($shift) |
||
| 232 | { |
||
| 233 | $i = 1; |
||
| 234 | $timeDiff = $originalStartTime->diff($shift['startTime']); |
||
| 235 | $hoursFromStart = ($timeDiff->d*24)+$timeDiff->h; |
||
| 236 | $rowForShift = $this->getRowForShift($shift['roleID'], $rows, $hoursFromStart+2, $sheat); |
||
| 237 | $this->createShiftCell($sheat, $hoursFromStart+2, $rowForShift, $shift); |
||
| 238 | $shift = array_shift($shifts); |
||
| 239 | } |
||
| 240 | $rowCount = count($rows); |
||
| 241 | $style = $sheat->getStyleByColumnAndRow(2, 4, 1+count($simpleHours), 3 + $rowCount); |
||
| 242 | $style->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); |
||
| 243 | $hourCount = count($simpleHours); |
||
| 244 | $this->grayOutUnused($hourCount, $rowCount, $sheat); |
||
| 245 | $sheat->getColumnDimension('A')->setAutoSize(true); |
||
| 246 | return $ssheat; |
||
| 247 | } |
||
| 248 | |||
| 317 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: