GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1e41a0...edb7c5 )
by Cees-Jan
03:36
created

HighPrecisionScheduler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 96
Duplicated Lines 14.58 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.5%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 14
loc 96
ccs 37
cts 40
cp 0.925
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A schedule() 0 8 1
A time() 0 4 2
A hasDrifted() 0 4 1
A tick() 0 12 3
B align() 14 38 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React;
4
5
use React\EventLoop\LoopInterface;
6
use React\EventLoop\TimerInterface;
7
8
final class HighPrecisionScheduler implements SchedulerInterface
9
{
10
    /** @var LoopInterface */
11
    private $loop;
12
13
    /** @var bool */
14
    private $useHighResolution = false;
15
16
    /** @var callable[] */
17
    private $ticks = [];
18
19
    /** @var TimerInterface */
20
    private $timer;
21
22
    /**
23
     * @param LoopInterface $loop
24
     */
25 2
    public function __construct(LoopInterface $loop)
26
    {
27 2
        $this->loop = $loop;
28 2
        $this->useHighResolution = \function_exists('hrtime');
29
30 2
        $this->align();
31 2
    }
32
33 2
    public function schedule(callable $tick): void
34
    {
35
        // Push this new tick on the stack with the rest, running it in the next minute
36 2
        $this->ticks[] = $tick;
37
38
        // Initial tick because some actions might want to run in this minute
39 2
        $tick();
40 2
    }
41
42 2
    private function time(): float
43
    {
44 2
        return $this->useHighResolution ? \hrtime(true) * 1e-9 : \microtime(true);
45
    }
46
47 2
    private function hasDrifted(float $time): bool
48
    {
49 2
        return (int)\date('s', (int)$time) > 0;
50
    }
51
52 2
    private function tick(): void
53
    {
54 2
        $startOfTick = $this->time();
55
56 2
        foreach ($this->ticks as $tick) {
57 1
            $tick();
58
        }
59
60 2
        if ($this->hasDrifted($startOfTick)) {
61
            $this->align();
62
        }
63 2
    }
64
65 2
    private function align(): void
66
    {
67 2
        if ($this->timer instanceof TimerInterface) {
68
            $this->loop->cancelTimer($this->timer);
69
        }
70
71 2
        $currentSecond = (int)\date('s', (int)$this->time());
72
73 2 View Code Duplication
        if ($currentSecond >= 1 && $currentSecond <= 55) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $this->loop->addTimer(55 - $currentSecond, function (): void {
75 1
                $this->align();
76 1
            });
77
78 1
            return;
79
        }
80
81 2 View Code Duplication
        if ($currentSecond > 55 && $currentSecond <= 58) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            $this->loop->addTimer(1, function (): void {
83 1
                $this->align();
84 1
            });
85
86 1
            return;
87
        }
88
89 2
        if ($currentSecond === 59) {
90
            $this->loop->addTimer(0.001, function (): void {
91 1
                $this->align();
92 1
            });
93
94 1
            return;
95
        }
96
97 2
        $this->tick();
98
99
        $this->timer = $this->loop->addPeriodicTimer(60, function (): void {
100
            $this->tick();
101 2
        });
102 2
    }
103
}
104