| Conditions | 11 |
| Paths | 17 |
| Total Lines | 65 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 75 | public function getArrayCalendar(Collection $workdays) |
||
| 76 | { |
||
| 77 | if ($workdays->isEmpty()) { |
||
| 78 | return []; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** @var Workday $current */ |
||
| 82 | $currentWorkday = $workdays->first(); |
||
| 83 | |||
| 84 | /** @var \DateTime $currentDate */ |
||
| 85 | $currentDate = $currentWorkday->getDate(); |
||
| 86 | |||
| 87 | $currentMonth = $currentDate->format('m'); |
||
| 88 | $currentYear = $currentDate->format('Y'); |
||
| 89 | $currentDate = new \DateTime(); |
||
| 90 | $currentDate->setDate($currentYear, $currentMonth, 1); |
||
| 91 | $currentDate->setTime(0, 0); |
||
| 92 | $dayOfWeek = $currentDate->format('w'); |
||
| 93 | $currentWeek = $currentDate->format('W'); |
||
| 94 | $numFillDays = (7 + (int) $dayOfWeek - 1) % 7; |
||
| 95 | |||
| 96 | $month = []; |
||
| 97 | $week = []; |
||
| 98 | while ($numFillDays--) { |
||
| 99 | $week[] = ['day' => '']; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** @var \ArrayIterator $iterator */ |
||
| 103 | $iterator = $workdays->getIterator(); |
||
| 104 | |||
| 105 | /** @var \DateTime $targetDate */ |
||
| 106 | while ($iterator->valid() && ($targetDate = $iterator->current()->getDate()) >= $currentDate) { |
||
| 107 | while ($targetDate >= $currentDate) { |
||
| 108 | if ($currentWeek != $currentDate->format('W') || $currentMonth != $currentDate->format('m')) { |
||
| 109 | $month[] = $week; |
||
| 110 | $week = []; |
||
| 111 | $currentWeek = $currentDate->format('W'); |
||
| 112 | if ($currentMonth != $currentDate->format('m')) { |
||
| 113 | $calendar[((int) $currentYear * 12 + (int) $currentMonth - 1)] = $month; |
||
|
|
|||
| 114 | $dayOfWeek = $currentDate->format('w'); |
||
| 115 | $numFillDays = (7 + (int) $dayOfWeek - 1) % 7; |
||
| 116 | $month = []; |
||
| 117 | while ($numFillDays--) { |
||
| 118 | $week[] = ['day' => '']; |
||
| 119 | } |
||
| 120 | $currentMonth = $currentDate->format('m'); |
||
| 121 | $currentYear = $currentDate->format('Y'); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($targetDate != $currentDate) { |
||
| 126 | $week[] = ['day' => $currentDate->format('d')]; |
||
| 127 | } |
||
| 128 | $currentDate->add(new \DateInterval('P1D')); |
||
| 129 | $currentDate->setTime(0, 0); |
||
| 130 | } |
||
| 131 | $week[] = ['day' => $targetDate->format('d'), 'data' => $iterator->current()]; |
||
| 132 | $iterator->next(); |
||
| 133 | } |
||
| 134 | $month[] = $week; |
||
| 135 | |||
| 136 | $calendar[((int) $currentYear*12 + (int) $currentMonth - 1)] = $month; |
||
| 137 | |||
| 138 | return $calendar; |
||
| 139 | } |
||
| 140 | |||
| 171 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.