| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 214 |
| 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 |
||
| 269 | public function generateGridSchedule($dept, $shifts, $request, $response, $type) |
||
| 270 | { |
||
| 271 | $ssheat = new Spreadsheet(); |
||
| 272 | $sheat = $ssheat->getActiveSheet(); |
||
| 273 | $sheat->setCellValue('A1', $dept['departmentName']); |
||
| 274 | $count = count($shifts); |
||
| 275 | $days = array(); |
||
| 276 | $roles = array(); |
||
| 277 | $roles2 = array(); |
||
| 278 | for($i = 0; $i < $count; $i++) |
||
| 279 | { |
||
| 280 | $start = new DateTime($shifts[$i]['startTime']); |
||
| 281 | $end = new DateTime($shifts[$i]['endTime']); |
||
| 282 | $shifts[$i]['startTime'] = $start; |
||
| 283 | $shifts[$i]['endTime'] = $end; |
||
| 284 | $startDateStr = $start->format('l (n/j/Y)'); |
||
| 285 | $endDateStr = $end->format('l (n/j/Y)'); |
||
| 286 | $days[$startDateStr] = 1; |
||
| 287 | $days[$endDateStr] = 1; |
||
| 288 | $diff = $start->diff($end); |
||
| 289 | $shifts[$i]['length'] = $diff->h; |
||
| 290 | if(!isset($roles[$shifts[$i]['roleID']])) |
||
| 291 | { |
||
| 292 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 293 | $roles2[$shifts[$i]['roleID']] = array(); |
||
| 294 | } |
||
| 295 | else |
||
| 296 | { |
||
| 297 | if($roles[$shifts[$i]['roleID']] < $shifts[$i]['length']) |
||
| 298 | { |
||
| 299 | $roles[$shifts[$i]['roleID']] = $shifts[$i]['length']; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | array_push($roles2[$shifts[$i]['roleID']], array('start'=>$start, 'end'=>$end)); |
||
| 303 | } |
||
| 304 | arsort($roles); |
||
| 305 | usort($shifts, array($this, 'shiftTimeSort')); |
||
| 306 | $originalStartTime = $shifts[0]['startTime']; |
||
| 307 | $str = $shifts[0]['startTime']->format('c'); |
||
| 308 | $start = date_parse($str); |
||
| 309 | $lastShift = $shifts[$count - 1]; |
||
| 310 | $interval = $lastShift['endTime']->diff($shifts[0]['startTime']); |
||
| 311 | $hourCount = ($interval->d*24) + $interval->h; |
||
| 312 | $simpleHours = array(); |
||
| 313 | $militaryHours = array(); |
||
| 314 | $hour = $start['hour']; |
||
| 315 | for($i = 0; $i < $hourCount; $i++) |
||
| 316 | { |
||
| 317 | if($hour < 12) |
||
| 318 | { |
||
| 319 | if($hour === 0) |
||
| 320 | { |
||
| 321 | array_push($simpleHours, '12a'); |
||
| 322 | } |
||
| 323 | else |
||
| 324 | { |
||
| 325 | array_push($simpleHours, $hour.'a'); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | else |
||
| 329 | { |
||
| 330 | if($hour === 12) |
||
| 331 | { |
||
| 332 | array_push($simpleHours, $hour.'p'); |
||
| 333 | } |
||
| 334 | else |
||
| 335 | { |
||
| 336 | array_push($simpleHours, ($hour - 12).'p'); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | array_push($militaryHours, $hour.':00'); |
||
| 340 | $hour++; |
||
| 341 | if($hour === 24) |
||
| 342 | { |
||
| 343 | $hour = 0; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | $sheat->fromArray($simpleHours, null, 'B2'); |
||
| 347 | $sheat->fromArray($militaryHours, null, 'B3'); |
||
| 348 | $mergeCount = 24 - $start['hour']; |
||
| 349 | if($mergeCount > $hourCount) |
||
| 350 | { |
||
| 351 | $mergeCount = $hourCount; |
||
| 352 | } |
||
| 353 | $days = array_keys($days); |
||
| 354 | $cellIndex = 2; |
||
| 355 | while($mergeCount) |
||
| 356 | { |
||
| 357 | $sheat->mergeCellsByColumnAndRow($cellIndex, 1, $cellIndex + $mergeCount - 1, 1); |
||
| 358 | $sheat->setCellValueByColumnAndRow($cellIndex, 1, array_shift($days)); |
||
| 359 | $cell = $sheat->getCellByColumnAndRow($cellIndex, 1); |
||
| 360 | $cell->getStyle()->getAlignment()->setHorizontal('center'); |
||
| 361 | $cellIndex += $mergeCount; |
||
| 362 | $hourCount -= $mergeCount; |
||
| 363 | $mergeCount = $hourCount; |
||
| 364 | if($mergeCount > 24) |
||
| 365 | { |
||
| 366 | $mergeCount = 24; |
||
| 367 | } |
||
| 368 | } |
||
| 369 | $i = 0; |
||
| 370 | $rows = array(); |
||
| 371 | foreach($roles as $role=>$hour) |
||
| 372 | { |
||
| 373 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 374 | array_push($rows, $role); |
||
| 375 | $overlaps = array(); |
||
| 376 | for($j = 0; $j < count($roles2[$role]) - 1; $j++) |
||
| 377 | { |
||
| 378 | $currRole = $roles2[$role][$j]; |
||
| 379 | $nextRole = $roles2[$role][$j + 1]; |
||
| 380 | if($currRole['end'] > $nextRole['start']) |
||
| 381 | { |
||
| 382 | $str = $currRole['start']->format('c'); |
||
| 383 | if(!isset($overlaps[$str])) |
||
| 384 | { |
||
| 385 | $overlaps[$str] = 0; |
||
| 386 | } |
||
| 387 | $overlaps[$str]++; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | if(!empty($overlaps)) |
||
| 391 | { |
||
| 392 | $overlapCount = max(array_values($overlaps)); |
||
| 393 | for($j = 0; $j < $overlapCount + 1; $j++) |
||
| 394 | { |
||
| 395 | $i++; |
||
| 396 | $sheat->setCellValueByColumnAndRow(1, 4 + $i, $this->getRoleNameFromID($role)); |
||
| 397 | if($j > 0) |
||
| 398 | { |
||
| 399 | array_push($rows, $role); |
||
| 400 | } |
||
| 401 | } |
||
| 402 | } |
||
| 403 | else |
||
| 404 | { |
||
| 405 | $i++; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | $shift = array_shift($shifts); |
||
| 409 | while($shift) |
||
| 410 | { |
||
| 411 | $i = 1; |
||
| 412 | $timeDiff = $originalStartTime->diff($shift['startTime']); |
||
| 413 | $hoursFromStart = ($timeDiff->d*24)+$timeDiff->h; |
||
| 414 | $firstRow = array_search($shift['roleID'], $rows); |
||
| 415 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4); |
||
| 416 | if($cell->isInMergeRange()) |
||
| 417 | { |
||
| 418 | while($rows[$firstRow+$i] === $shift['roleID']) |
||
| 419 | { |
||
| 420 | $cell = $sheat->getCellByColumnAndRow($hoursFromStart+2, $firstRow+4+$i); |
||
| 421 | if(!$cell->isInMergeRange()) |
||
| 422 | { |
||
| 423 | break; |
||
| 424 | } |
||
| 425 | $i++; |
||
| 426 | } |
||
| 427 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4+$i, $hoursFromStart+1+$shift['length'], $firstRow+4+$i); |
||
| 428 | } |
||
| 429 | else |
||
| 430 | { |
||
| 431 | $sheat->mergeCellsByColumnAndRow($hoursFromStart+2, $firstRow+4, $hoursFromStart+1+$shift['length'], $firstRow+4); |
||
| 432 | } |
||
| 433 | $shift = array_shift($shifts); |
||
| 434 | } |
||
| 435 | $rowCount = count($rows); |
||
| 436 | $style = $sheat->getStyleByColumnAndRow(2, 4, 1+count($simpleHours), 3 + $rowCount); |
||
| 437 | $style->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN); |
||
| 438 | $hourCount = count($simpleHours); |
||
| 439 | for($i = 0; $i < $hourCount; $i++) |
||
| 440 | { |
||
| 441 | for($j = 0; $j < $rowCount; $j++) |
||
| 442 | { |
||
| 443 | $cell = $sheat->getCellByColumnAndRow($i+2, $j+4); |
||
| 444 | if($cell->isInMergeRange()) |
||
| 445 | { |
||
| 446 | continue; |
||
| 447 | } |
||
| 448 | else |
||
| 449 | { |
||
| 450 | $style = $cell->getStyle(); |
||
| 451 | $style->getBorders()->getAllBorders()->setBorderStyle(false); |
||
| 452 | $style->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_LIGHTGRAY); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | $sheat->getColumnDimension('A')->setAutoSize(true); |
||
| 457 | if($type === 'XLSX') |
||
| 458 | { |
||
| 459 | $writer = new Xlsx($ssheat); |
||
| 460 | $content = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
||
| 461 | $extension = '.xlsx'; |
||
| 462 | } |
||
| 463 | else if($type === 'PDF') |
||
| 464 | { |
||
| 465 | $sheat->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE); |
||
| 466 | $writer = new mpdf($ssheat); |
||
| 467 | $writer->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE); |
||
| 468 | $content = 'application/pdf'; |
||
| 469 | $extension = '.pdf'; |
||
| 470 | } |
||
| 471 | else |
||
| 472 | { |
||
| 473 | return $response->withJson(array('msg'=>'Unknown type specified: '.$type), 400); |
||
| 474 | } |
||
| 475 | ob_start(); |
||
| 476 | $writer->save('php://output'); |
||
| 477 | $str = ob_get_clean(); |
||
| 478 | $response = $response->withHeader('Content-Type', $content); |
||
| 479 | $response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$extension); |
||
| 480 | $response->getBody()->write($str); |
||
| 481 | return $response; |
||
| 482 | } |
||
| 483 | |||
| 527 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: