| Conditions | 12 |
| Paths | 5 |
| Total Lines | 46 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 45 | private function mainLoop(): void |
||
| 46 | { |
||
| 47 | while (true) { |
||
| 48 | try { |
||
| 49 | $task = null; |
||
| 50 | $taskFired = false; |
||
| 51 | $this->queue->synchronized(function ($scope, $taskFired, $task) { |
||
|
|
|||
| 52 | // Wait for queue to become non-empty |
||
| 53 | while ($scope->queue->isEmpty() && $scope->newTasksMayBeScheduled) { |
||
| 54 | $scope->queue->wait(); |
||
| 55 | } |
||
| 56 | if ($scope->queue->isEmpty()) { |
||
| 57 | return; // Queue is empty and will forever remain; die |
||
| 58 | } |
||
| 59 | |||
| 60 | // Queue nonempty; look at first evt and do the right thing |
||
| 61 | $currentTime = null; |
||
| 62 | $executionTime = null; |
||
| 63 | $task = $scope->queue->getMin(); |
||
| 64 | $task->lock->synchronized(function ($scope, $task, $currentTime, $executionTime, $taskFired) { |
||
| 65 | if ($task->state == TimerTask::CANCELLED) { |
||
| 66 | $scope->queue->removeMin(); |
||
| 67 | $scope->mainLoop();// No action required, poll queue again |
||
| 68 | } |
||
| 69 | $currentTime = floor(microtime(true) * 1000); |
||
| 70 | $executionTime = $task->nextExecutionTime; |
||
| 71 | if ($taskFired = ($executionTime <= $currentTime)) { |
||
| 72 | if ($task->period == 0) { // Non-repeating, remove |
||
| 73 | $scope->queue->removeMin(); |
||
| 74 | $task->state = TimerTask::EXECUTED; |
||
| 75 | } else { // Repeating task, reschedule |
||
| 76 | $scope->queue->rescheduleMin( |
||
| 77 | $task->period < 0 ? $currentTime - $task->period |
||
| 78 | : $executionTime + $task->period |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | }, $scope, $task, $currentTime, $executionTime, $taskFired); |
||
| 83 | if (!$taskFired) {// Task hasn't yet fired; wait |
||
| 84 | $scope->queue->wait(($executionTime - $currentTime) / 1000); //wait(time) where time is in microseconds |
||
| 85 | } |
||
| 86 | }, $this, $taskFired, $task); |
||
| 87 | if ($taskFired) {// Task fired; run it, holding no locks |
||
| 88 | $task->run(); |
||
| 89 | } |
||
| 90 | } catch (\Exception $e) { |
||
| 91 | } |
||
| 95 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.