ARevolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInterval() 0 3 1
A getFrame() 0 6 2
A __construct() 0 6 1
A shouldUpdate() 0 8 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Revolver\A;
6
7
use AlecRabbit\Spinner\Contract\IFrame;
8
use AlecRabbit\Spinner\Contract\IInterval;
9
use AlecRabbit\Spinner\Core\Contract\ITolerance;
10
use AlecRabbit\Spinner\Core\Revolver\Contract\IRevolver;
11
use AlecRabbit\Spinner\Core\Revolver\Tolerance;
0 ignored issues
show
Bug introduced by
The type AlecRabbit\Spinner\Core\Revolver\Tolerance was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
abstract class ARevolver implements IRevolver
14
{
15
    protected int $deltaTolerance;
16
    protected float $diff;
17
    protected float $intervalValue;
18
19
    public function __construct(
20
        protected IInterval $interval,
21
        protected ITolerance $tolerance = new Tolerance(),
22
    ) {
23
        $this->deltaTolerance = $this->tolerance->toMilliseconds();
24
        $this->diff = $this->intervalValue = $interval->toMilliseconds();
25
    }
26
27
    public function getInterval(): IInterval
28
    {
29
        return $this->interval;
30
    }
31
32
    public function getFrame(?float $dt = null): IFrame
33
    {
34
        if ($this->shouldUpdate($dt)) {
35
            $this->next($dt);
36
        }
37
        return $this->current();
38
    }
39
40
    /**
41
     * @param float|null $dt delta time(milliseconds), time passed since last update
42
     */
43
    protected function shouldUpdate(?float $dt = null): bool
44
    {
45
        if ($dt === null || $this->intervalValue <= ($dt + $this->deltaTolerance) || $this->diff <= 0) {
46
            $this->diff = $this->intervalValue;
47
            return true;
48
        }
49
        $this->diff -= $dt;
50
        return false;
51
    }
52
53
    abstract protected function next(?float $dt = null): void;
54
55
    abstract protected function current(): IFrame;
56
}
57