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.

DoormanQueuedJobTask   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 206
Duplicated Lines 11.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 24
loc 206
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshDescriptor() 0 6 2
A getId() 0 4 1
A setId() 0 6 1
A getDescriptor() 0 4 1
A __construct() 0 4 1
A serialize() 0 6 1
A unserialize() 0 18 3
A getHandler() 0 4 1
A getData() 0 6 1
A ignoresRules() 0 8 2
A stopsSiblings() 0 8 2
A getExpiresIn() 0 8 2
A shouldExpire() 0 8 2
A canRunTask() 12 12 1
A isCancelled() 12 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Symbiote\QueuedJobs\Jobs;
4
5
use AsyncPHP\Doorman\Cancellable;
6
use AsyncPHP\Doorman\Expires;
7
use AsyncPHP\Doorman\Process;
8
use AsyncPHP\Doorman\Task;
9
use InvalidArgumentException;
10
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
13
class DoormanQueuedJobTask implements Task, Expires, Process, Cancellable
14
{
15
    /**
16
     * @var int
17
     */
18
    protected $id;
19
20
    /**
21
     * @var QueuedJobDescriptor
22
     */
23
    protected $descriptor;
24
25
    /**
26
     * Reload descriptor from DB
27
     */
28
    protected function refreshDescriptor()
29
    {
30
        if ($this->descriptor) {
31
            $this->descriptor = QueuedJobDescriptor::get()->byID($this->descriptor->ID);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symbiote\QueuedJobs\Dat...($this->descriptor->ID) can also be of type object<SilverStripe\ORM\DataObject>. However, the property $descriptor is declared as type object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
32
        }
33
    }
34
35
    /**
36
     * @inheritdoc
37
     *
38
     * @return null|int
39
     */
40
    public function getId()
41
    {
42
        return $this->id;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     *
48
     * @param int $id
49
     *
50
     * @return $this
51
     */
52
    public function setId($id)
53
    {
54
        $this->id = $id;
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return QueuedJobDescriptor
61
     */
62
    public function getDescriptor()
63
    {
64
        return $this->descriptor;
65
    }
66
67
    /**
68
     * @param QueuedJobDescriptor $descriptor
69
     */
70
    public function __construct(QueuedJobDescriptor $descriptor)
71
    {
72
        $this->descriptor = $descriptor;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     *
78
     * @return string
79
     */
80
    public function serialize()
81
    {
82
        return serialize(array(
83
            'descriptor' => $this->descriptor->ID,
84
        ));
85
    }
86
87
    /**
88
     * @inheritdoc
89
     *
90
     * @throws InvalidArgumentException
91
     * @param string
92
     */
93
    public function unserialize($serialized)
94
    {
95
        $data = unserialize($serialized);
96
97
        if (!isset($data['descriptor'])) {
98
            throw new InvalidArgumentException('Malformed data');
99
        }
100
101
        $descriptor = QueuedJobDescriptor::get()
102
            ->filter('ID', $data['descriptor'])
103
            ->first();
104
105
        if (!$descriptor) {
106
            throw new InvalidArgumentException('Descriptor not found');
107
        }
108
109
        $this->descriptor = $descriptor;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getHandler()
116
    {
117
        return 'DoormanQueuedJobHandler';
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    public function getData()
124
    {
125
        return array(
126
            'descriptor' => $this->descriptor,
127
        );
128
    }
129
130
    /**
131
     * @return bool
132
     */
133
    public function ignoresRules()
134
    {
135
        if ($this->descriptor->hasMethod('ignoreRules')) {
136
            return $this->descriptor->ignoreRules();
0 ignored issues
show
Documentation Bug introduced by
The method ignoreRules does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
137
        }
138
139
        return false;
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function stopsSiblings()
146
    {
147
        if ($this->descriptor->hasMethod('stopsSiblings')) {
148
            return $this->descriptor->stopsSiblings();
0 ignored issues
show
Documentation Bug introduced by
The method stopsSiblings does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * @inheritdoc
156
     *
157
     * @return int
158
     */
159
    public function getExpiresIn()
160
    {
161
        if ($this->descriptor->hasMethod('getExpiresIn')) {
162
            return $this->descriptor->getExpiresIn();
0 ignored issues
show
Documentation Bug introduced by
The method getExpiresIn does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
163
        }
164
165
        return -1;
166
    }
167
168
    /**
169
     * @inheritdoc
170
     *
171
     * @param int $startedAt
172
     * @return bool
173
     */
174
    public function shouldExpire($startedAt)
175
    {
176
        if ($this->descriptor->hasMethod('shouldExpire')) {
177
            return $this->descriptor->shouldExpire($startedAt);
0 ignored issues
show
Documentation Bug introduced by
The method shouldExpire does not exist on object<Symbiote\QueuedJo...ts\QueuedJobDescriptor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     * @inheritdoc
185
     *
186
     * @return bool
187
     */
188 View Code Duplication
    public function canRunTask()
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...
189
    {
190
        $this->refreshDescriptor();
191
        return in_array(
192
            $this->descriptor->JobStatus,
193
            array(
194
                QueuedJob::STATUS_NEW,
195
                QueuedJob::STATUS_INIT,
196
                QueuedJob::STATUS_WAIT
197
            )
198
        );
199
    }
200
201
    /**
202
     * @inheritdoc
203
     *
204
     * @return bool
205
     */
206 View Code Duplication
    public function isCancelled()
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...
207
    {
208
        $this->refreshDescriptor();
209
210
        // Treat completed jobs as cancelled when it comes to how Doorman handles picking up jobs to run
211
        $cancelledStates = [
212
            QueuedJob::STATUS_CANCELLED,
213
            QueuedJob::STATUS_COMPLETE,
214
        ];
215
216
        return in_array($this->descriptor->JobStatus, $cancelledStates, true);
217
    }
218
}
219