| Conditions | 4 |
| Paths | 3 |
| Total Lines | 57 |
| 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 |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | private function setHolidaysOrthodoxEaster($lngDate) { |
||
| 128 | $givenYear = date('Y', $lngDate); |
||
| 129 | $daying = []; |
||
| 130 | $statmentsArray = $this->readTypeFromJsonFile('RomanianBankHolidays'); |
||
| 131 | if (array_key_exists($givenYear, $statmentsArray)) { |
||
| 132 | foreach ($statmentsArray[$givenYear] as $value) { |
||
| 133 | $daying[] = strtotime($value); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | return $daying; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * returns bank holidays in a given month |
||
| 141 | * |
||
| 142 | * @param date $lngDate |
||
| 143 | * @param boolean $inclCatholicEaster |
||
| 144 | * @return int |
||
| 145 | */ |
||
| 146 | protected function setHolidaysInMonth($lngDate, $inclCatholicEaster = false) { |
||
| 147 | $holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster); |
||
| 148 | $thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate); |
||
| 149 | $holidays = 0; |
||
| 150 | foreach ($thisMonthDayArray as $value) { |
||
| 151 | if (in_array($value, $holidaysInGivenYear)) { |
||
| 152 | $holidays += 1; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | return $holidays; |
||
| 156 | } |
||
| 157 | |||
| 158 | protected function setMonthAllDaysIntoArray($lngDate) { |
||
| 159 | $firstDayGivenMonth = strtotime('first day of', $lngDate); |
||
| 160 | $lastDayInGivenMonth = strtotime('last day of', $lngDate); |
||
| 161 | $secondsInOneDay = 24 * 60 * 60; |
||
| 162 | return range($firstDayGivenMonth, $lastDayInGivenMonth, $secondsInOneDay); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * returns working days in a given month |
||
| 167 | * |
||
| 168 | * @param date $lngDate |
||
| 169 | * @param boolean $inclCatholicEaster |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | protected function setWorkingDaysInMonth($lngDate, $inclCatholicEaster = false) { |
||
| 173 | $holidaysInGivenYear = $this->setHolidays($lngDate, $inclCatholicEaster); |
||
| 174 | $thisMonthDayArray = $this->setMonthAllDaysIntoArray($lngDate); |
||
| 175 | $workingDays = 0; |
||
| 176 | foreach ($thisMonthDayArray as $value) { |
||
| 177 | if (!in_array(strftime('%w', $value), [0, 6]) && !in_array($value, $holidaysInGivenYear)) { |
||
| 178 | $workingDays += 1; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return $workingDays; |
||
| 182 | } |
||
| 184 |
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.