GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — user-entity ( 6db575...67b804 )
by Luis Ramón
02:52
created

WorkdayRepository::getNext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace AppBundle\Entity;
4
5
use AppBundle\Form\Model\Calendar;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\Common\Collections\Collection;
8
use Doctrine\ORM\EntityRepository;
9
10
class WorkdayRepository extends EntityRepository
11
{
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
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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$calendar was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calendar = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The variable $calendar does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
137
138
        return $calendar;
139
    }
140
141 View Code Duplication
    public function getNext(Workday $workday)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $query = $this->createQueryBuilder('w')
144
            ->where('w.agreement = :agreement')
145
            ->andWhere('w.date > :date')
146
            ->orderBy('w.date', 'ASC')
147
            ->setParameter('date', $workday->getDate())
148
            ->setParameter('agreement', $workday->getAgreement())
149
            ->getQuery();
150
151
        $result = $query->setMaxResults(1)->getResult();
152
153
        return $result ? $result[0] : null;
154
    }
155
156 View Code Duplication
    public function getPrevious(Workday $workday)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $query = $this->createQueryBuilder('w')
159
            ->where('w.agreement = :agreement')
160
            ->andWhere('w.date < :date')
161
            ->orderBy('w.date', 'DESC')
162
            ->setParameter('date', $workday->getDate())
163
            ->setParameter('agreement', $workday->getAgreement())
164
            ->getQuery();
165
166
        $result = $query->setMaxResults(1)->getResult();
167
168
        return $result ? $result[0] : null;
169
    }
170
}
171