Completed
Pull Request — master (#262)
by Vasily
04:35
created

Timer::__construct()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 5
eloc 15
c 3
b 1
f 0
nc 16
nop 4
dl 0
loc 21
rs 8.7624
1
<?php
2
namespace PHPDaemon\Core;
3
4
use PHPDaemon\Traits\EventLoopContainer;
5
6
/**
7
 * Timed event
8
 * @package PHPDaemon\Core
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
class Timer
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
    use EventLoopContainer;
16
17
    /**
18
     * @var Timer[] List of timers
19
     */
20
    protected static $list = [];
21
    /**
22
     * @var integer Counter
23
     */
24
    protected static $counter = 1;
25
    /**
26
     * @var integer|null Timer id
27
     */
28
    public $id;
29
    /**
30
     * @var integer Current timeout holder
31
     */
32
    public $lastTimeout;
33
    /**
34
     * @var boolean Is the timer finished?
35
     */
36
    public $finished = false;
37
    /**
38
     * @var callable Callback
39
     */
40
    public $cb;
41
    /**
42
     * @var integer Priority
43
     */
44
    public $priority;
45
    /**
46
     * @var \EventBufferEvent Event resource
47
     */
48
    protected $ev;
49
50
    /**
51
     * Constructor
52
     * @param  callable $cb Callback
53
     * @param  integer $timeout Timeout
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timeout not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
54
     * @param  integer|string $id Timer ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
55
     * @param  integer $priority Priority
0 ignored issues
show
Documentation introduced by
Should the type for parameter $priority not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
     */
57
    public function __construct($cb, $timeout = null, $id = null, $priority = null)
58
    {
59
        if ($id === null) {
60
            $id = ++self::$counter;
61
        } else {
62
            $id = (string)$id;
63
        }
64
        $this->id = $id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $id can also be of type string. However, the property $id is declared as type integer|null. 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...
65
        $this->cb = $cb;
66
        if ($this->eventLoop === null) {
67
            $this->eventLoop = EventLoop::$instance;
68
        }
69
        $this->ev = $this->eventLoop->timer([$this, 'eventCall']);
70
        if ($priority !== null) {
71
            $this->setPriority($priority);
72
        }
73
        if ($timeout !== null) {
74
            $this->timeout($timeout);
75
        }
76
        self::$list[$id] = $this;
77
    }
78
79
    /**
80
     * Set prioriry
81
     * @param  integer $priority Priority
82
     * @return void
83
     */
84
    public function setPriority($priority)
85
    {
86
        $this->priority = $priority;
87
        $this->ev->priority = $priority;
88
    }
89
90
    /**
91
     * Sets timeout
92
     * @param  integer $timeout Timeout
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timeout not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
     * @return void
94
     */
95
    public function timeout($timeout = null)
96
    {
97
        if ($timeout !== null) {
98
            $this->lastTimeout = $timeout;
99
        }
100
        $this->ev->add($this->lastTimeout / 1e6);
101
    }
102
103
    /**
104
     * Adds timer
105
     * @param  callable $cb Callback
106
     * @param  integer $timeout Timeout
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timeout not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
107
     * @param  integer|string $id Timer ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be integer|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
108
     * @param  integer $priority Priority
0 ignored issues
show
Documentation introduced by
Should the type for parameter $priority not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
109
     * @return integer|string           Timer ID
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
110
     */
111
    public static function add($cb, $timeout = null, $id = null, $priority = null)
112
    {
113
        $obj = new self($cb, $timeout, $id, $priority);
114
        return $obj->id;
115
    }
116
117
    /**
118
     * Sets timeout
119
     * @param  integer|string $id Timer ID
120
     * @param  integer $timeout Timeout
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timeout not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
121
     * @return boolean
122
     */
123
    public static function setTimeout($id, $timeout = null)
124
    {
125
        if (isset(self::$list[$id])) {
126
            self::$list[$id]->timeout($timeout);
127
            return true;
128
        }
129
        return false;
130
    }
131
132
    /**
133
     * Removes timer by ID
134
     * @param  integer|string $id Timer ID
135
     * @return void
136
     */
137
    public static function remove($id)
138
    {
139
        if (isset(self::$list[$id])) {
140
            self::$list[$id]->free();
141
        }
142
    }
143
144
    /**
145
     * Frees the timer
146
     * @return void
147
     */
148
    public function free()
149
    {
150
        unset(self::$list[$this->id]);
151
        if ($this->ev !== null) {
152
            $this->ev->free();
153
            $this->ev = null;
154
        }
155
    }
156
157
    /**
158
     * Cancels timer by ID
159
     * @param  integer|string $id Timer ID
160
     * @return void
161
     */
162
    public static function cancelTimeout($id)
163
    {
164
        if (isset(self::$list[$id])) {
165
            self::$list[$id]->cancel();
166
        }
167
    }
168
169
    /**
170
     * Cancels timer
171
     * @return void
172
     */
173
    public function cancel()
174
    {
175
        $this->ev->del();
176
    }
177
178
    /**
179
     * Called when timer is triggered
180
     * @return void
181
     */
182
    public function eventCall()
183
    {
184
        try {
185
            //Daemon::log('cb - '.Debug::zdump($this->cb));
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
186
            $func = $this->cb;
187
            $func($this);
188
        } catch (\Exception $e) {
189
            Daemon::uncaughtExceptionHandler($e);
190
        }
191
    }
192
193
    /**
194
     * Finishes timer
195
     * @return void
196
     */
197
    public function finish()
198
    {
199
        $this->free();
200
    }
201
202
    /**
203
     * Destructor
204
     * @return void
205
     */
206
    public function __destruct()
207
    {
208
        $this->free();
209
    }
210
}
211