1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace BinaryCube\CarrotMQ\Extension; |
||
6 | |||
7 | use BinaryCube\CarrotMQ\Event; |
||
8 | |||
9 | use function vsprintf; |
||
10 | use function microtime; |
||
11 | |||
12 | /** |
||
13 | * Class IdleExtension |
||
14 | */ |
||
15 | class IdleExtension extends Extension |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var integer |
||
20 | */ |
||
21 | protected $idleTimeout = 0; |
||
22 | |||
23 | /** |
||
24 | * @var float |
||
25 | */ |
||
26 | protected $tick; |
||
27 | |||
28 | /** |
||
29 | * Returns an array of event names this subscriber wants to listen to. |
||
30 | * |
||
31 | * The array keys are event names and the value can be: |
||
32 | * |
||
33 | * * The method name to call (priority defaults to 0) |
||
34 | * * An array composed of the method name to call and the priority |
||
35 | * * An array of arrays composed of the method names to call and respective |
||
36 | * priorities, or 0 if unset |
||
37 | * |
||
38 | * For instance: |
||
39 | * |
||
40 | * * ['eventName' => 'methodName'] |
||
41 | * * ['eventName' => ['methodName', $priority]] |
||
42 | * * ['eventName' => [['methodName1', $priority], ['methodName2']]] |
||
43 | * |
||
44 | * @return array The event names to listen to |
||
45 | */ |
||
46 | public static function getSubscribedEvents() |
||
47 | { |
||
48 | return [ |
||
49 | Event\Consumer\Start::name() => 'onTick', |
||
50 | Event\Consumer\MessageReceived::name() => 'onTick', |
||
51 | Event\Consumer\AfterMessageReceived::name() => 'onTick', |
||
52 | Event\Consumer\Idle::name() => 'onIdle', |
||
53 | ]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | public static function name(): string |
||
60 | { |
||
61 | return 'IdleExtension'; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | public static function description(): string |
||
68 | { |
||
69 | return ''; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | * |
||
75 | * @param int|float $idleTimeout Default 30 seconds |
||
76 | */ |
||
77 | public function __construct($idleTimeout = 30) |
||
78 | { |
||
79 | parent::__construct(); |
||
80 | |||
81 | $this->idleTimeout = $idleTimeout; |
||
82 | $this->tick = 0; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return void |
||
87 | */ |
||
88 | protected function tick(): void |
||
89 | { |
||
90 | $this->tick = microtime(true); |
||
0 ignored issues
–
show
|
|||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param Event\Event $event |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function onTick(Event\Event $event): void |
||
99 | { |
||
100 | $this->tick(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param Event\Consumer\Idle $event |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function onIdle(Event\Consumer\Idle $event): void |
||
109 | { |
||
110 | if ( |
||
111 | false === ( |
||
112 | $this->idleTimeout > 0 && |
||
113 | $this->tick > 0 && |
||
114 | ((microtime(true) - $this->tick) >= $this->idleTimeout) |
||
115 | ) |
||
116 | ) { |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $this |
||
121 | ->logger |
||
122 | ->debug( |
||
123 | vsprintf( |
||
124 | '[%s] Interrupt execution. Reached the limit of %s seconds', |
||
125 | [ |
||
126 | self::name(), |
||
127 | $this->idleTimeout, |
||
128 | ] |
||
129 | ) |
||
130 | ); |
||
131 | |||
132 | $event->interruptExecution(); |
||
133 | } |
||
134 | |||
135 | } |
||
136 |
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 theid
property of an instance of theAccount
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.