| Conditions | 20 |
| Paths | 4896 |
| Total Lines | 157 |
| 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 |
||
| 77 | protected function createSpreadSheet() |
||
| 78 | { |
||
| 79 | $shifts = $this->shifts; |
||
| 80 | $dept = $this->department; |
||
| 81 | $ssheat = new Spreadsheet(); |
||
| 82 | $sheat = $ssheat->getActiveSheet(); |
||
| 83 | $sheat->setCellValue('A1', $dept['departmentName']); |
||
| 84 | $count = count($shifts); |
||
| 85 | $days = array(); |
||
| 86 | $roles = array(); |
||
| 87 | $roles2 = array(); |
||
| 88 | for($i = 0; $i < $count; $i++) |
||
| 89 | { |
||
| 90 | $start = new \DateTime($shifts[$i]['startTime']); |
||
| 91 | $end = new \DateTime($shifts[$i]['endTime']); |
||
| 92 | $shifts[$i]['startTime'] = $start; |
||
| 93 | $shifts[$i]['endTime'] = $end; |
||
| 94 | $startDateStr = $start->format('l (n/j/Y)'); |
||
| 95 | $endDateStr = $end->format('l (n/j/Y)'); |
||
| 96 | $days[$startDateStr] = 1; |
||
| 97 | $days[$endDateStr] = 1; |
||
| 98 | $diff = $start->diff($end); |
||
| 99 | $shifts[$i]['length'] = $diff->h; |
||
| 100 | if(!isset($roles[$shifts[$i]['roleID']])) |
||
| 101 | { |
||
| 102 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 103 | $roles2[$shifts[$i]['roleID']] = array(); |
||
| 104 | } |
||
| 105 | else |
||
| 106 | { |
||
| 107 | if($roles[$shifts[$i]['roleID']] < $shifts[$i]['length']) |
||
| 108 | { |
||
| 109 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | array_push($roles2[$shifts[$i]['roleID']], array('start'=>$start, 'end'=>$end)); |
||
| 113 | } |
||
| 114 | arsort($roles); |
||
| 115 | usort($shifts, array($this, 'shiftTimeSort')); |
||
| 116 | $originalStartTime = $shifts[0]['startTime']; |
||
| 117 | $str = $shifts[0]['startTime']->format('c'); |
||
| 118 | $start = date_parse($str); |
||
| 119 | $lastShift = $shifts[$count - 1]; |
||
| 120 | $interval = $lastShift['endTime']->diff($shifts[0]['startTime']); |
||
| 121 | $hourCount = ($interval->d*24) + $interval->h; |
||
| 122 | $simpleHours = array(); |
||
| 123 | $militaryHours = array(); |
||
| 124 | $hour = $start['hour']; |
||
| 125 | for($i = 0; $i < $hourCount; $i++) |
||
| 126 | { |
||
| 127 | array_push($simpleHours, $this->getSimpleHour($hour)); |
||
| 128 | array_push($militaryHours, $hour.':00'); |
||
| 129 | $hour++; |
||
| 130 | if($hour === 24) |
||
| 131 | { |
||
| 132 | $hour = 0; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | $sheat->fromArray($simpleHours, null, 'B2'); |
||
| 136 | $sheat->fromArray($militaryHours, null, 'B3'); |
||
| 137 | $mergeCount = 24 - $start['hour']; |
||
| 138 | if($mergeCount > $hourCount) |
||
| 139 | { |
||
| 140 | $mergeCount = $hourCount; |
||
| 141 | } |
||
| 142 | $days = array_keys($days); |
||
| 143 | $cellIndex = 2; |
||
| 144 | while($mergeCount) |
||
| 145 | { |
||
| 146 | $sheat->mergeCellsByColumnAndRow($cellIndex, 1, $cellIndex + $mergeCount - 1, 1); |
||
| 147 | $sheat->setCellValueByColumnAndRow($cellIndex, 1, array_shift($days)); |
||
| 148 | $cell = $sheat->getCellByColumnAndRow($cellIndex, 1); |
||
| 149 | $cell->getStyle()->getAlignment()->setHorizontal('center'); |
||
| 150 | $cellIndex += $mergeCount; |
||
| 151 | $hourCount -= $mergeCount; |
||
| 152 | $mergeCount = $hourCount; |
||
| 153 | if($mergeCount > 24) |
||
| 154 | { |
||
| 155 | $mergeCount = 24; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | $i = 0; |
||
| 159 | $rows = array(); |
||
| 160 | foreach($roles as $role=>$hour) |
||
| 161 | { |
||
| 162 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 163 | array_push($rows, $role); |
||
| 164 | $overlaps = array(); |
||
| 165 | for($j = 0; $j < count($roles2[$role]) - 1; $j++) |
||
| 166 | { |
||
| 167 | $currRole = $roles2[$role][$j]; |
||
| 168 | $nextRole = $roles2[$role][$j + 1]; |
||
| 169 | if($currRole['end'] > $nextRole['start']) |
||
| 170 | { |
||
| 171 | $str = $currRole['start']->format('c'); |
||
| 172 | if(!isset($overlaps[$str])) |
||
| 173 | { |
||
| 174 | $overlaps[$str] = 0; |
||
| 175 | } |
||
| 176 | $overlaps[$str]++; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | if(!empty($overlaps)) |
||
| 180 | { |
||
| 181 | $overlapCount = max(array_values($overlaps)); |
||
| 182 | for($j = 0; $j < $overlapCount + 1; $j++) |
||
| 183 | { |
||
| 184 | $i++; |
||
| 185 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 186 | if($j > 0) |
||
| 187 | { |
||
| 188 | array_push($rows, $role); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | else |
||
| 193 | { |
||
| 194 | $i++; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | $shift = array_shift($shifts); |
||
| 198 | while($shift) |
||
| 199 | { |
||
| 200 | $i = 1; |
||
| 201 | $timeDiff = $originalStartTime->diff($shift['startTime']); |
||
| 202 | $hoursFromStart = ($timeDiff->d*24)+$timeDiff->h; |
||
| 203 | $firstRow = array_search($shift['roleID'], $rows); |
||
| 204 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4); |
||
| 205 | if($cell->isInMergeRange()) |
||
| 206 | { |
||
| 207 | while($rows[$firstRow+$i] === $shift['roleID']) |
||
| 208 | { |
||
| 209 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4+$i); |
||
| 210 | if(!$cell->isInMergeRange()) |
||
| 211 | { |
||
| 212 | break; |
||
| 213 | } |
||
| 214 | $i++; |
||
| 215 | } |
||
| 216 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4+$i, $hoursFromStart+1+$shift['length'], $firstRow+4+$i); |
||
| 217 | $this->setShiftNameInCell($sheat, $hoursFromStart+2, $firstRow+4+$i, $shift); |
||
| 218 | } |
||
| 219 | else |
||
| 220 | { |
||
| 221 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4, $hoursFromStart+1+$shift['length'], $firstRow+4); |
||
| 222 | $this->setShiftNameInCell($sheat, $hoursFromStart+2, $firstRow+4, $shift); |
||
| 223 | } |
||
| 224 | $shift = array_shift($shifts); |
||
| 225 | } |
||
| 226 | $rowCount = count($rows); |
||
| 227 | $style = $sheat->getStyleByColumnAndRow(2, 4, 1+count($simpleHours), 3 + $rowCount); |
||
| 228 | $style->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); |
||
| 229 | $hourCount = count($simpleHours); |
||
| 230 | $this->grayOutUnused($hourCount, $rowCount, $sheat); |
||
| 231 | $sheat->getColumnDimension('A')->setAutoSize(true); |
||
| 232 | return $ssheat; |
||
| 233 | } |
||
| 234 | |||
| 303 |