Completed
Push — master ( 3c7025...906c6b )
by Sheela
01:51
created

DutyList::setLastWorkedDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SET\Handlers\Duty;
4
5
use SET\Duty;
6
use SET\DutySwap;
7
8
class DutyList
9
{
10
    private $duty;
11
12
    public function __construct($duty)
13
    {
14
        if (is_int($duty)) {
15
            $duty = Duty::findOrFail($duty);
16
        }
17
        $this->duty = $duty;
18
    }
19
20
    public function htmlOutput()
21
    {
22
        return $this->processedList()->htmlOutput();
23
    }
24
25
    public function scheduledUpdate()
26
    {
27
        //build the list and write into the database the next record.
28
        $this->processedList()->iterateList();
29
        //build the list again (with the new record on top) and generate our email output.
30
        return $this->processedList()->emailOutput();
31
    }
32
33
    public function emailOutput()
34
    {
35
        $userGroup = $this->processedList();
36
37
        return $userGroup->emailOutput();
38
    }
39
40
    public function setLastWorkedDate()
41
    {
42
        return $this->processedList()->setLastWorkedDate();
0 ignored issues
show
Bug introduced by
The method setLastWorkedDate does only exist in SET\Handlers\Duty\DutyUsers, but not in SET\Handlers\Duty\DutyGroups.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
    }
44
45
    /**
46
     * @param array  $dateArray
47
     * @param array  $IDArray
48
     * @param string $type
49
     */
50
    public function processSwapRequest(array $dateArray, array $IDArray, $type)
51
    {
52
        for ($i = 0; $i < 2; $i++) {
53
            $futureSwap = DutySwap::where('date', $dateArray[$i])
54
                ->where('duty_id', $this->duty->id)->first();
55
            $flippedI = ($i == 0 ? 1 : 0);
56
57
            if (is_null($futureSwap)) {
58
                $this->createDutySwap($dateArray[$i], $IDArray[$flippedI], $type);
59
            } else {
60
                $futureSwap->imageable_id = $IDArray[$flippedI];
61
                $futureSwap->save();
62
            }
63
        }
64
    }
65
66
    private function processedList()
67
    {
68
        if ($this->duty->has_groups) {
69
            return new DutyGroups($this->duty);
70
        } else {
71
            return new DutyUsers($this->duty);
72
        }
73
    }
74
75
    /**
76
     * @param $date
77
     * @param $ID
78
     * @param $type
79
     */
80
    private function createDutySwap($date, $ID, $type)
81
    {
82
        DutySwap::create([
83
            'imageable_id'   => $ID,
84
            'imageable_type' => $type,
85
            'duty_id'        => $this->duty->id,
86
            'date'           => $date,
87
        ]);
88
    }
89
}
90