Test Failed
Push — main ( d01f4c...5af89f )
by Bingo
15:56
created

TimerThread::mainLoop()   C

Complexity

Conditions 12
Paths 5

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 46
rs 6.9666
c 1
b 0
f 0
cc 12
nc 5
nop 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Jabe\Engine\Impl\Util\Timer;
4
5
class TimerThread extends \Thread
6
{
7
    /**
8
     * This flag is set to false by the reaper to inform us that there
9
     * are no more live references to our Timer object.  Once this flag
10
     * is true and there are no more tasks in our queue, there is no
11
     * work left for us to do, so we terminate gracefully.  Note that
12
     * this field is protected by queue's monitor!
13
     */
14
    public $newTasksMayBeScheduled = true;
15
16
    /**
17
     * Our Timer's queue.  We store this reference in preference to
18
     * a reference to the Timer so the reference graph remains acyclic.
19
     * Otherwise, the Timer would never be garbage-collected and this
20
     * thread would never go away.
21
     */
22
    private $queue;
23
24
    public function __construct(TaskQueue $queue)
25
    {
26
        $this->queue = $queue;
27
    }
28
29
    public function run(): void
30
    {
31
        try {
32
            $this->mainLoop();
33
        } finally {
34
            // Someone killed this Thread, behave as if Timer cancelled
35
            $this->queue->synchronized(function ($scope) {
36
                $scope->newTasksMayBeScheduled = false;
37
                $scope->queue->clear();  // Eliminate obsolete references
38
            }, $this);
39
        }
40
    }
41
42
    /**
43
     * The main timer loop.  (See class comment.)
44
     */
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $task is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

51
                $this->queue->synchronized(function ($scope, $taskFired, /** @scrutinizer ignore-unused */ $task) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $executionTime is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
                    $task->lock->synchronized(function ($scope, $task, $currentTime, /** @scrutinizer ignore-unused */ $executionTime, $taskFired) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $currentTime is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
                    $task->lock->synchronized(function ($scope, $task, /** @scrutinizer ignore-unused */ $currentTime, $executionTime, $taskFired) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $taskFired is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

64
                    $task->lock->synchronized(function ($scope, $task, $currentTime, $executionTime, /** @scrutinizer ignore-unused */ $taskFired) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $taskFired is dead and can be removed.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method run() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
                    $task->/** @scrutinizer ignore-call */ 
89
                           run();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
                }
90
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
91
            }
92
        }
93
    }
94
}
95