Conditions | 11 |
Paths | 20 |
Total Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
96 | public function wait(Closure $callback, array $priorities = []): void |
||
97 | { |
||
98 | $queues = array_reverse($this->queues, true); |
||
99 | while (true) { |
||
100 | $this->checkShutdown(); |
||
101 | if (!$this->shouldProcessNext()) { |
||
102 | break; |
||
103 | } |
||
104 | |||
105 | // check schedule |
||
106 | $messagesString = $this->redis->zrangebyscore($this->scheduleKey, '-inf', microtime(true), ['LIMIT' => ['OFFSET' => 0, 'COUNT' => 1]]); |
||
107 | if (count($messagesString)) { |
||
108 | foreach ($messagesString as $messageString) { |
||
109 | $this->redis->zrem($this->scheduleKey, $messageString); |
||
110 | $this->send($this->serializer->unserialize($messageString)); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $messageString = null; |
||
115 | $foundPriority = null; |
||
116 | |||
117 | foreach ($queues as $priority => $name) { |
||
118 | if (count($priorities) > 0 && !in_array($priority, $priorities)) { |
||
119 | continue; |
||
120 | } |
||
121 | if ($messageString !== null) { |
||
122 | break; |
||
123 | } |
||
124 | |||
125 | $key = $this->getKey($priority); |
||
126 | |||
127 | $messageString = $this->redis->spop($key); |
||
128 | $foundPriority = $priority; |
||
129 | } |
||
130 | |||
131 | if ($messageString !== null) { |
||
132 | $message = $this->serializer->unserialize($messageString); |
||
133 | $callback($message, $foundPriority); |
||
134 | $this->incrementProcessedItems(); |
||
135 | } else { |
||
136 | if ($this->refreshInterval) { |
||
137 | $this->checkShutdown(); |
||
138 | sleep($this->refreshInterval); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 |