Passed
Push — master ( e4531b...a6f95b )
by Smoren
02:40
created

Sequence   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
eloc 26
c 0
b 0
f 0
dl 0
loc 113
ccs 32
cts 32
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 14 4
A getStartValue() 0 3 1
A offsetGet() 0 19 4
A offsetSet() 0 3 1
A isInfinite() 0 3 1
A getIterator() 0 3 1
A offsetUnset() 0 3 1
A count() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace Smoren\Sequence\Structs;
4
5
use Smoren\Sequence\Exceptions\OutOfRangeException;
6
use Smoren\Sequence\Exceptions\ReadOnlyException;
7
use Smoren\Sequence\Interfaces\SequenceInterface;
8
use Smoren\Sequence\Interfaces\SequenceIteratorInterface;
9
use Smoren\Sequence\Iterators\SequenceIterator;
10
11
/**
12
 * @template T
13
 * @implements SequenceInterface<T>
14
 */
15
abstract class Sequence implements SequenceInterface
16
{
17
    /**
18
     * @var T
0 ignored issues
show
Bug introduced by
The type Smoren\Sequence\Structs\T 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...
19
     */
20
    protected $start;
21
    /**
22
     * @var int<0, max>|null
23
     */
24
    protected ?int $size;
25
26
    /**
27
     * @param T $start
28
     * @param int<0, max>|null $size
29
     */
30 11
    public function __construct($start, ?int $size)
31
    {
32 11
        $this->start = $start;
33 11
        $this->size = $size;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 10
    public function offsetExists($offset): bool
40
    {
41 10
        if(!is_int($offset)) {
42 7
            return false;
43
        }
44
45 10
        if(!$this->isInfinite()) {
46 8
            if($offset >= 0) {
47 8
                return $offset < count($this);
48
            }
49 8
            return abs($offset) <= count($this);
50
        }
51
52 2
        return true;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 5
    public function offsetSet($offset, $value): void
59
    {
60 5
        throw new ReadOnlyException();
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 5
    public function offsetUnset($offset): void
67
    {
68 5
        throw new ReadOnlyException();
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 11
    public function count(): int
75
    {
76 11
        return $this->size ?? -1;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 11
    public function isInfinite(): bool
83
    {
84 11
        return $this->size === null;
85
    }
86
87
    /**
88
     * @return T
89
     */
90 7
    public function getStartValue()
91
    {
92 7
        return $this->getValueByIndex(0);
93
    }
94
95
    /**
96
     * @param int $offset
97
     * @return T
98
     * @throws OutOfRangeException
99
     */
100 10
    public function offsetGet($offset)
101
    {
102 10
        if(!$this->offsetExists($offset)) {
103 8
            throw new OutOfRangeException();
104
        }
105
106
        /** @var int $offset */
107
108 10
        if($this->isInfinite()) {
109 2
            return $this->getValueByIndex($offset);
110
        }
111
112 8
        if($offset < 0) {
113 8
            $offset = $this->size + ($offset % $this->size);
114
        }
115
116 8
        $offset = ($offset % $this->size);
117
118 8
        return $this->getValueByIndex($offset);
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     * @return SequenceIteratorInterface<T>
124
     */
125 7
    public function getIterator(): SequenceIteratorInterface
126
    {
127 7
        return new SequenceIterator($this);
128
    }
129
}
130