Completed
Push — 2.0 ( ee0168...76e968 )
by Marco
11:26
created

Schedule   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 252
Duplicated Lines 10.32 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 26
loc 252
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 5 1
A setDescription() 0 7 1
A getExpression() 0 5 1
A setExpression() 0 14 1
A getEnabled() 0 5 1
A setEnable() 0 9 1
A getFirstrun() 0 5 1
A setFirstrun() 0 7 1
A getLastrun() 0 5 1
A setLastrun() 0 7 1
A shouldRunJob() 13 13 2
A getNextPlannedRun() 13 13 2
A buildExpression() 0 14 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 namespace Comodojo\Extender\Orm\Entities;
2
3
use \Doctrine\ORM\Mapping as ORM;
4
use \Comodojo\Extender\Traits\BaseEntityTrait;
5
use \Comodojo\Extender\Traits\ProcessEntityTrait;
6
use \Cron\CronExpression;
7
use \Comodojo\Foundation\Validation\DataFilter;
8
use \DateTime;
9
10
/**
11
 * @package     Comodojo Extender
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 * @ORM\Table(name="extender_schedule")
26
 * @ORM\Entity
27
 */
28
29
class Schedule {
30
31
    use BaseEntityTrait;
32
    use ProcessEntityTrait;
33
34
    /**
35
     * @var string
36
     *
37
     * @ORM\Column(name="description", type="text", length=65535, nullable=true)
38
     */
39
    protected $description;
40
41
    /**
42
     * @var string
43
     *
44
     * @ORM\Column(name="min", type="string", length=16, nullable=false)
45
     */
46
    protected $min;
47
48
    /**
49
     * @var string
50
     *
51
     * @ORM\Column(name="hour", type="string", length=16, nullable=false)
52
     */
53
    protected $hour;
54
55
    /**
56
     * @var string
57
     *
58
     * @ORM\Column(name="day", type="string", length=16, nullable=false)
59
     */
60
    protected $day;
61
62
    /**
63
     * @var string
64
     *
65
     * @ORM\Column(name="month", type="string", length=16, nullable=false)
66
     */
67
    protected $month;
68
69
    /**
70
     * @var string
71
     *
72
     * @ORM\Column(name="weekday", type="string", length=16, nullable=false)
73
     */
74
    protected $weekday;
75
76
    /**
77
     * @var string
78
     *
79
     * @ORM\Column(name="year", type="string", length=16, nullable=false)
80
     */
81
    protected $year;
82
83
    /**
84
     * @var boolean
85
     *
86
     * @ORM\Column(name="enabled", type="boolean", nullable=false)
87
     */
88
    protected $enabled = 0;
89
90
    /**
91
     * @var datetime
92
     *
93
     * @ORM\Column(name="firstrun", type="datetime", nullable=true)
94
     */
95
    protected $firstrun;
96
97
    /**
98
     * @var datetime
99
     *
100
     * @ORM\Column(name="lastrun", type="datetime", nullable=true)
101
     */
102
    protected $lastrun;
103
104
    /**
105
     * Get a brief job description
106
     *
107
     * @return string
108
     */
109
    public function getDescription() {
110
111
        return $this->description;
112
113
    }
114
115
    /**
116
     * Set brief job description
117
     *
118
     * @param string $description
119
     * @return Schedule
120
     */
121
    public function setDescription($description) {
122
123
        $this->description = $description;
124
125
        return $this;
126
127
    }
128
129
    /**
130
     * Get cron expression of this schedule
131
     *
132
     * @return string
133
     */
134
    public function getExpression() {
135
136
        return (string) $this->buildExpression();
137
138
    }
139
140
    /**
141
     * set cron expression for this schedule
142
     *
143
     * @param srting $expression A cron-compatible expression
144
     * @return Schedule
145
     */
146
    public function setExpression($expression) {
147
148
        $exp = CronExpression::factory($expression);
149
150
        $this->minute = $exp->getExpression(0);
0 ignored issues
show
Bug introduced by
The property minute does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
151
        $this->hour = $exp->getExpression(1);
152
        $this->day = $exp->getExpression(2);
153
        $this->month = $exp->getExpression(3);
154
        $this->weekday = $exp->getExpression(4);
155
        $this->year = $exp->getExpression(5);
156
157
        return $this;
158
159
    }
160
161
    /**
162
     * True if job is currently enabled
163
     *
164
     * @return bool
165
     */
166
    public function getEnabled() {
167
168
        return $this->enabled;
169
170
    }
171
172
    /**
173
     * Set enable/disable status
174
     *
175
     * @param bool $enable
176
     * @return Schedule
177
     */
178
    public function setEnable($enable) {
179
180
        $ena = DataFilter::filterBoolean($enable);
181
182
        $this->enable = $ena;
0 ignored issues
show
Bug introduced by
The property enable does not seem to exist. Did you mean enabled?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
183
184
        return $this;
185
186
    }
187
188
    /**
189
     * Get the first-run-date of job
190
     *
191
     * @return DateTime
192
     */
193
    public function getFirstrun() {
194
195
        return $this->firstrun;
196
197
    }
198
199
    /**
200
     * Set the first-run-date of job
201
     *
202
     * @param DateTime $datetime
203
     * @return Schedule
204
     */
205
    public function setFirstrun(DateTime $datetime) {
206
207
        $this->firstrun = $datetime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $datetime of type object<DateTime> is incompatible with the declared type object<Comodojo\Extender\Orm\Entities\datetime> of property $firstrun.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
208
209
        return $this;
210
211
    }
212
213
    /**
214
     * Get the first-run-date of job
215
     *
216
     * @return DateTime
217
     */
218
    public function getLastrun() {
219
220
        return $this->lastrun;
221
222
    }
223
224
    /**
225
     * Set the first-run-date of job
226
     *
227
     * @param DateTime $datetime
228
     * @return Schedule
229
     */
230
    public function setLastrun(DateTime $datetime) {
231
232
        $this->lastrun = $datetime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $datetime of type object<DateTime> is incompatible with the declared type object<Comodojo\Extender\Orm\Entities\datetime> of property $lastrun.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
233
234
        return $this;
235
236
    }
237
238 View Code Duplication
    public function shouldRunJob(DateTime $time) {
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...
239
240
        $last_run = $this->getLastrun();
241
242
        if ( is_null($lastrun) ) {
0 ignored issues
show
Bug introduced by
The variable $lastrun does not exist. Did you mean $last_run?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
243
            $next_run = $this->getFirstrun();
244
        } else {
245
            $next_run = $this->buildExpression()->getNextRunDate($last_run);
246
        }
247
248
        return $time < $next_run;
249
250
    }
251
252 View Code Duplication
    public function getNextPlannedRun(DateTime $time) {
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...
253
254
        $last_run = $this->getLastrun();
0 ignored issues
show
Unused Code introduced by
$last_run is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
255
256
        if ( is_null($lastrun) ) {
0 ignored issues
show
Bug introduced by
The variable $lastrun does not exist. Did you mean $last_run?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
257
            $next_run = $this->getFirstrun();
258
        } else {
259
            $next_run = $this->buildExpression()->getNextRunDate($time);
260
        }
261
262
        return $next_run;
263
264
    }
265
266
    protected function buildExpression() {
267
268
        $exp_string = implode(" ", [
269
            $this->minute,
270
            $this->hour,
271
            $this->day,
272
            $this->month,
273
            $this->weekday,
274
            $this->year
275
        ]);
276
277
        return CronExpression::factory($exp_string);
278
279
    }
280
}
281