| Conditions | 12 |
| Paths | 34 |
| Total Lines | 62 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 12 | public function createCalendar(Calendar $calendar, Agreement $agreement) |
||
| 13 | { |
||
| 14 | $date = $calendar->getStartDate(); |
||
| 15 | $hours = $calendar->getTotalHours(); |
||
| 16 | $nonSchoolDays = $this->getEntityManager()->getRepository('AppBundle:NonSchoolDay') |
||
| 17 | ->createQueryBuilder('n') |
||
| 18 | ->select('n.date') |
||
| 19 | ->getQuery()->getArrayResult(); |
||
| 20 | |||
| 21 | $nonSchoolDays = array_map('current', $nonSchoolDays); |
||
| 22 | |||
| 23 | $collection = new ArrayCollection(); |
||
| 24 | |||
| 25 | while ($hours > 0) { |
||
| 26 | if (false === in_array($date, $nonSchoolDays, false)) { |
||
| 27 | $current = $this->getEntityManager()->getRepository('AppBundle:Workday')->findOneBy([ |
||
| 28 | 'date' => $date, |
||
| 29 | 'agreement' => $agreement |
||
| 30 | ]); |
||
| 31 | if (null === $current) { |
||
| 32 | $current = new Workday(); |
||
| 33 | } |
||
| 34 | $current->setDate(clone $date); |
||
| 35 | $current->setAgreement($agreement); |
||
| 36 | $assignedHours = 0; |
||
| 37 | switch ($date->format('w')) { |
||
| 38 | case 0: |
||
| 39 | $assignedHours = min($hours, $calendar->getHoursSun()); |
||
| 40 | break; |
||
| 41 | case 1: |
||
| 42 | $assignedHours = min($hours, $calendar->getHoursMon()); |
||
| 43 | break; |
||
| 44 | case 2: |
||
| 45 | $assignedHours = min($hours, $calendar->getHoursTue()); |
||
| 46 | break; |
||
| 47 | case 3: |
||
| 48 | $assignedHours = min($hours, $calendar->getHoursWed()); |
||
| 49 | break; |
||
| 50 | case 4: |
||
| 51 | $assignedHours = min($hours, $calendar->getHoursThu()); |
||
| 52 | break; |
||
| 53 | case 5: |
||
| 54 | $assignedHours = min($hours, $calendar->getHoursFri()); |
||
| 55 | break; |
||
| 56 | case 6: |
||
| 57 | $assignedHours = min($hours, $calendar->getHoursSat()); |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | $current->setHours($current->getHours() + $assignedHours); |
||
| 61 | |||
| 62 | $hours -= $assignedHours; |
||
| 63 | |||
| 64 | if ($current->getHours()) { |
||
| 65 | $collection->add($current); |
||
| 66 | $this->getEntityManager()->persist($current); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $date->add(new \DateInterval('P1D')); |
||
| 71 | } |
||
| 72 | return $collection; |
||
| 73 | } |
||
| 74 | |||
| 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.