| Conditions | 15 |
| Paths | 816 |
| Total Lines | 126 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 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 |
||
| 121 | protected function createSpreadSheet() |
||
| 122 | { |
||
| 123 | $shifts = $this->shifts; |
||
| 124 | $dept = $this->department; |
||
| 125 | $ssheat = new Spreadsheet(); |
||
| 126 | $sheat = $ssheat->getActiveSheet(); |
||
| 127 | $sheat->setCellValue('A1', $dept['departmentName']); |
||
| 128 | $count = count($shifts); |
||
| 129 | $days = array(); |
||
| 130 | $roles = array(); |
||
| 131 | $roles2 = array(); |
||
| 132 | for($i = 0; $i < $count; $i++) |
||
| 133 | { |
||
| 134 | $start = new \DateTime($shifts[$i]['startTime']); |
||
| 135 | $end = new \DateTime($shifts[$i]['endTime']); |
||
| 136 | $shifts[$i]['startTime'] = $start; |
||
| 137 | $shifts[$i]['endTime'] = $end; |
||
| 138 | $startDateStr = $start->format('l (n/j/Y)'); |
||
| 139 | $endDateStr = $end->format('l (n/j/Y)'); |
||
| 140 | $days[$startDateStr] = 1; |
||
| 141 | $days[$endDateStr] = 1; |
||
| 142 | $diff = $start->diff($end); |
||
| 143 | $shifts[$i]['length'] = $diff->h; |
||
| 144 | if(!isset($roles[$shifts[$i]['roleID']])) |
||
| 145 | { |
||
| 146 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 147 | $roles2[$shifts[$i]['roleID']] = array(); |
||
| 148 | } |
||
| 149 | else |
||
| 150 | { |
||
| 151 | if($roles[$shifts[$i]['roleID']] < $shifts[$i]['length']) |
||
| 152 | { |
||
| 153 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | array_push($roles2[$shifts[$i]['roleID']], array('start'=>$start, 'end'=>$end)); |
||
| 157 | } |
||
| 158 | arsort($roles); |
||
| 159 | usort($shifts, array($this, 'shiftTimeSort')); |
||
| 160 | $originalStartTime = $shifts[0]['startTime']; |
||
| 161 | $str = $shifts[0]['startTime']->format('c'); |
||
| 162 | $start = date_parse($str); |
||
| 163 | $lastShift = $shifts[$count - 1]; |
||
| 164 | $interval = $lastShift['endTime']->diff($shifts[0]['startTime']); |
||
| 165 | $hourCount = ($interval->d * 24) + $interval->h; |
||
| 166 | $tmp = $this->getHoursArrays($start['hour'], $hourCount); |
||
| 167 | $simpleHours = $tmp[0]; |
||
| 168 | $militaryHours = $tmp[1]; |
||
| 169 | $sheat->fromArray($simpleHours, null, 'B2'); |
||
| 170 | $sheat->fromArray($militaryHours, null, 'B3'); |
||
| 171 | $mergeCount = 24 - $start['hour']; |
||
| 172 | if($mergeCount > $hourCount) |
||
| 173 | { |
||
| 174 | $mergeCount = $hourCount; |
||
| 175 | } |
||
| 176 | $days = array_keys($days); |
||
| 177 | $cellIndex = 2; |
||
| 178 | while($mergeCount) |
||
| 179 | { |
||
| 180 | $sheat->mergeCellsByColumnAndRow($cellIndex, 1, $cellIndex + $mergeCount - 1, 1); |
||
| 181 | $sheat->setCellValueByColumnAndRow($cellIndex, 1, array_shift($days)); |
||
| 182 | $cell = $sheat->getCellByColumnAndRow($cellIndex, 1); |
||
| 183 | $cell->getStyle()->getAlignment()->setHorizontal('center'); |
||
| 184 | $cellIndex += $mergeCount; |
||
| 185 | $hourCount -= $mergeCount; |
||
| 186 | $mergeCount = $hourCount; |
||
| 187 | if($mergeCount > 24) |
||
| 188 | { |
||
| 189 | $mergeCount = 24; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | $i = 0; |
||
| 193 | $rows = array(); |
||
| 194 | foreach($roles as $role=>$hour) |
||
| 195 | { |
||
| 196 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 197 | array_push($rows, $role); |
||
| 198 | $overlaps = array(); |
||
| 199 | for($j = 0; $j < count($roles2[$role]) - 1; $j++) |
||
| 200 | { |
||
| 201 | $currRole = $roles2[$role][$j]; |
||
| 202 | $nextRole = $roles2[$role][$j + 1]; |
||
| 203 | if($currRole['end'] > $nextRole['start']) |
||
| 204 | { |
||
| 205 | $str = $currRole['start']->format('c'); |
||
| 206 | if(!isset($overlaps[$str])) |
||
| 207 | { |
||
| 208 | $overlaps[$str] = 0; |
||
| 209 | } |
||
| 210 | $overlaps[$str]++; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | if(!empty($overlaps)) |
||
| 214 | { |
||
| 215 | $overlapCount = max(array_values($overlaps)); |
||
| 216 | for($j = 0; $j < $overlapCount + 1; $j++) |
||
| 217 | { |
||
| 218 | $i++; |
||
| 219 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 220 | if($j > 0) |
||
| 221 | { |
||
| 222 | array_push($rows, $role); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | else |
||
| 227 | { |
||
| 228 | $i++; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | $shift = array_shift($shifts); |
||
| 232 | while($shift) |
||
| 233 | { |
||
| 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 | } |
||
| 318 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.