Completed
Push — master ( d7ee0c...02dfbd )
by Christopher
01:22
created

IsMeasurable::completedAfterSchedule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Traits;
4
5
trait IsMeasurable
6
{
7
    /**
8
     * Has the task got an expected target.
9
     *
10
     * @return bool
11
     */
12
    public function hasDateTarget() : bool
13
    {
14
        return $this->started_at !== null || $this->expected_at !== null;
0 ignored issues
show
Bug introduced by
The property started_at 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...
Bug introduced by
The property expected_at 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...
15
    }
16
17
    /**
18
     * Is the task still in process.
19
     *
20
     * @return bool
21
     */
22
    public function isInProcess() : bool
23
    {
24
        return $this->delivered_at === null;
0 ignored issues
show
Bug introduced by
The property delivered_at 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...
25
    }
26
27
    /**
28
     * Is the task not due yet.
29
     *
30
     * @return bool
31
     */
32
    public function notDueYet() : bool
33
    {
34
        return $this->expected_at->greaterThan(now());
35
    }
36
37
    /**
38
     * Is the task complete.
39
     *
40
     * @return bool
41
     */
42
    public function completed() : bool
43
    {
44
        if ( ! $this->hasDateTarget() || $this->isInProcess()) {
45
            return false;
46
        }
47
48
        return ($this->completedOnSchedule()
49
        || $this->completedAfterSchedule())
50
        && $this->completedAllTasks();
51
    }
52
53
    /**
54
     * Was the task completed after the given deadline.
55
     *
56
     * @return bool
57
     */
58
    public function completedAfterSchedule() : bool
59
    {
60
        return $this->delivered_at->greaterThan($this->expected_at);
61
    }
62
63
    /**
64
     * Was the task completed before the given deadline.
65
     *
66
     * @return bool
67
     */
68
    public function completedBeforeSchedule() : bool
69
    {
70
        return $this->delivered_at->lessThan($this->expected_at);
71
    }
72
73
    /**
74
     * Was the task completed before or on the given deadline.
75
     *
76
     * @return bool
77
     */
78
    public function completedOnSchedule() : bool
79
    {
80
        return $this->delivered_at->lessThanOrEqualTo($this->expected_at);
81
    }
82
83
    /**
84
     * Is the task overdue.
85
     *
86
     * @return bool
87
     */
88
    public function isOverdue() : bool
89
    {
90
        return $this->isInProcess() && ! $this->notDueYet();
91
    }
92
93
    /**
94
     * Have all tasks been completed.
95
     *
96
     * @return bool
97
     */
98
    public function completedAllTasks() : bool
99
    {
100
        $this->load('tasks');
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
102
        if ($this->tasks->isEmpty()) {
0 ignored issues
show
Bug introduced by
The property tasks 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...
103
            return true;
104
        }
105
106
        return $this->tasks->count() === $this->tasks->sum->completed();
107
    }
108
}
109