| Conditions | 26 |
| Paths | > 20000 |
| Total Lines | 192 |
| 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 |
||
| 23 | protected function createSpreadSheet() |
||
| 24 | { |
||
| 25 | $shifts = $this->shifts; |
||
| 26 | $dept = $this->department; |
||
| 27 | $ssheat = new Spreadsheet(); |
||
| 28 | $sheat = $ssheat->getActiveSheet(); |
||
| 29 | $sheat->setCellValue('A1', $dept['departmentName']); |
||
| 30 | $count = count($shifts); |
||
| 31 | $days = array(); |
||
| 32 | $roles = array(); |
||
| 33 | $roles2 = array(); |
||
| 34 | for($i = 0; $i < $count; $i++) |
||
| 35 | { |
||
| 36 | $start = new \DateTime($shifts[$i]['startTime']); |
||
| 37 | $end = new \DateTime($shifts[$i]['endTime']); |
||
| 38 | $shifts[$i]['startTime'] = $start; |
||
| 39 | $shifts[$i]['endTime'] = $end; |
||
| 40 | $startDateStr = $start->format('l (n/j/Y)'); |
||
| 41 | $endDateStr = $end->format('l (n/j/Y)'); |
||
| 42 | $days[$startDateStr] = 1; |
||
| 43 | $days[$endDateStr] = 1; |
||
| 44 | $diff = $start->diff($end); |
||
| 45 | $shifts[$i]['length'] = $diff->h; |
||
| 46 | if(!isset($roles[$shifts[$i]['roleID']])) |
||
| 47 | { |
||
| 48 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 49 | $roles2[$shifts[$i]['roleID']] = array(); |
||
| 50 | } |
||
| 51 | else |
||
| 52 | { |
||
| 53 | if($roles[$shifts[$i]['roleID']] < $shifts[$i]['length']) |
||
| 54 | { |
||
| 55 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | array_push($roles2[$shifts[$i]['roleID']], array('start'=>$start, 'end'=>$end)); |
||
| 59 | } |
||
| 60 | arsort($roles); |
||
| 61 | usort($shifts, array($this, 'shiftTimeSort')); |
||
| 62 | $originalStartTime = $shifts[0]['startTime']; |
||
| 63 | $str = $shifts[0]['startTime']->format('c'); |
||
| 64 | $start = date_parse($str); |
||
| 65 | $lastShift = $shifts[$count - 1]; |
||
| 66 | $interval = $lastShift['endTime']->diff($shifts[0]['startTime']); |
||
| 67 | $hourCount = ($interval->d*24) + $interval->h; |
||
| 68 | $simpleHours = array(); |
||
| 69 | $militaryHours = array(); |
||
| 70 | $hour = $start['hour']; |
||
| 71 | for($i = 0; $i < $hourCount; $i++) |
||
| 72 | { |
||
| 73 | if($hour < 12) |
||
| 74 | { |
||
| 75 | if($hour === 0) |
||
| 76 | { |
||
| 77 | array_push($simpleHours, '12a'); |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | array_push($simpleHours, $hour.'a'); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | else |
||
| 85 | { |
||
| 86 | if($hour === 12) |
||
| 87 | { |
||
| 88 | array_push($simpleHours, $hour.'p'); |
||
| 89 | } |
||
| 90 | else |
||
| 91 | { |
||
| 92 | array_push($simpleHours, ($hour - 12).'p'); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | array_push($militaryHours, $hour.':00'); |
||
| 96 | $hour++; |
||
| 97 | if($hour === 24) |
||
| 98 | { |
||
| 99 | $hour = 0; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | $sheat->fromArray($simpleHours, null, 'B2'); |
||
| 103 | $sheat->fromArray($militaryHours, null, 'B3'); |
||
| 104 | $mergeCount = 24 - $start['hour']; |
||
| 105 | if($mergeCount > $hourCount) |
||
| 106 | { |
||
| 107 | $mergeCount = $hourCount; |
||
| 108 | } |
||
| 109 | $days = array_keys($days); |
||
| 110 | $cellIndex = 2; |
||
| 111 | while($mergeCount) |
||
| 112 | { |
||
| 113 | $sheat->mergeCellsByColumnAndRow($cellIndex, 1, $cellIndex + $mergeCount - 1, 1); |
||
| 114 | $sheat->setCellValueByColumnAndRow($cellIndex, 1, array_shift($days)); |
||
| 115 | $cell = $sheat->getCellByColumnAndRow($cellIndex, 1); |
||
| 116 | $cell->getStyle()->getAlignment()->setHorizontal('center'); |
||
| 117 | $cellIndex += $mergeCount; |
||
| 118 | $hourCount -= $mergeCount; |
||
| 119 | $mergeCount = $hourCount; |
||
| 120 | if($mergeCount > 24) |
||
| 121 | { |
||
| 122 | $mergeCount = 24; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | $i = 0; |
||
| 126 | $rows = array(); |
||
| 127 | foreach($roles as $role=>$hour) |
||
| 128 | { |
||
| 129 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 130 | array_push($rows, $role); |
||
| 131 | $overlaps = array(); |
||
| 132 | for($j = 0; $j < count($roles2[$role]) - 1; $j++) |
||
| 133 | { |
||
| 134 | $currRole = $roles2[$role][$j]; |
||
| 135 | $nextRole = $roles2[$role][$j + 1]; |
||
| 136 | if($currRole['end'] > $nextRole['start']) |
||
| 137 | { |
||
| 138 | $str = $currRole['start']->format('c'); |
||
| 139 | if(!isset($overlaps[$str])) |
||
| 140 | { |
||
| 141 | $overlaps[$str] = 0; |
||
| 142 | } |
||
| 143 | $overlaps[$str]++; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | if(!empty($overlaps)) |
||
| 147 | { |
||
| 148 | $overlapCount = max(array_values($overlaps)); |
||
| 149 | for($j = 0; $j < $overlapCount + 1; $j++) |
||
| 150 | { |
||
| 151 | $i++; |
||
| 152 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 153 | if($j > 0) |
||
| 154 | { |
||
| 155 | array_push($rows, $role); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | else |
||
| 160 | { |
||
| 161 | $i++; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $shift = array_shift($shifts); |
||
| 165 | while($shift) |
||
| 166 | { |
||
| 167 | $i = 1; |
||
| 168 | $timeDiff = $originalStartTime->diff($shift['startTime']); |
||
| 169 | $hoursFromStart = ($timeDiff->d*24)+$timeDiff->h; |
||
| 170 | $firstRow = array_search($shift['roleID'], $rows); |
||
| 171 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4); |
||
| 172 | if($cell->isInMergeRange()) |
||
| 173 | { |
||
| 174 | while($rows[$firstRow+$i] === $shift['roleID']) |
||
| 175 | { |
||
| 176 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4+$i); |
||
| 177 | if(!$cell->isInMergeRange()) |
||
| 178 | { |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | $i++; |
||
| 182 | } |
||
| 183 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4+$i, $hoursFromStart+1+$shift['length'], $firstRow+4+$i); |
||
| 184 | } |
||
| 185 | else |
||
| 186 | { |
||
| 187 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4, $hoursFromStart+1+$shift['length'], $firstRow+4); |
||
| 188 | } |
||
| 189 | $shift = array_shift($shifts); |
||
| 190 | } |
||
| 191 | $rowCount = count($rows); |
||
| 192 | $style = $sheat->getStyleByColumnAndRow(2, 4, 1+count($simpleHours), 3 + $rowCount); |
||
| 193 | $style->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); |
||
| 194 | $hourCount = count($simpleHours); |
||
| 195 | for($i = 0; $i < $hourCount; $i++) |
||
| 196 | { |
||
| 197 | for($j = 0; $j < $rowCount; $j++) |
||
| 198 | { |
||
| 199 | $cell = $sheat->getCellByColumnAndRow($i+2, $j+4); |
||
| 200 | if($cell->isInMergeRange()) |
||
| 201 | { |
||
| 202 | continue; |
||
| 203 | } |
||
| 204 | else |
||
| 205 | { |
||
| 206 | $style = $cell->getStyle(); |
||
| 207 | $style->getBorders()->getAllBorders()->setBorderStyle(false); |
||
| 208 | $style->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTGRAY); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | $sheat->getColumnDimension('A')->setAutoSize(true); |
||
| 213 | return $ssheat; |
||
| 214 | } |
||
| 215 | |||
| 284 |