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 ( 96e00f...2a90dc )
by Brent
11s
created

Tuple.php$0 ➔ rewind()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\Typed;
6
7
use Iterator;
8
use ArrayAccess;
9
10
class Tuple implements ArrayAccess
11
{
12
    use ValidatesType;
13
14
    /** @var \Spatie\Typed\Type[] */
15
    private $types;
16
17
    /** @var @var array */
0 ignored issues
show
Documentation Bug introduced by
The doc comment @var at position 0 could not be parsed: Unknown type name '@var' at position 0 in @var.
Loading history...
18
    private $data;
19
20
    public function __construct(Type ...$types)
21
    {
22
        $this->types = $types;
23
    }
24
25
    public function set(array $data): self
26
    {
27
        $iterator = $this->createIterator($data);
28
29
        foreach ($iterator as $key => ['type' => $type, 'value' => $value]) {
30
            $data[$key] = $this->validateType($type, $value);
31
        }
32
33
        $this->data = $data;
34
35
        return $this;
36
    }
37
38
    public function offsetGet($offset)
39
    {
40
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
41
    }
42
43
    public function offsetSet($offset, $value)
44
    {
45
        if ($offset === null || ! is_numeric($offset)) {
46
            throw WrongType::fromMessage('You must specify a numeric offset');
47
        }
48
49
        $type = $this->types[$offset] ?? null;
50
51
        if (! $type) {
52
            throw WrongType::fromMessage("No type was configured for this tuple at offset {$offset}");
53
        }
54
55
        $this->data[$offset] = $this->validateType($type, $value);
56
    }
57
58
    public function offsetExists($offset)
59
    {
60
        return array_key_exists($offset, $this->data);
61
    }
62
63
    public function offsetUnset($offset)
64
    {
65
        throw WrongType::fromMessage('Tuple values cannot be unset');
66
    }
67
68
    public function toArray(): array
69
    {
70
        return $this->data;
71
    }
72
73
    private function createIterator(array $data): Iterator
74
    {
75
        return new class($this->types, $data) implements Iterator {
76
            /** @var array */
77
            private $types;
78
79
            /** @var array */
80
            private $data;
81
82
            /** @var int */
83
            private $position;
84
85
            public function __construct(array $types, array $data)
86
            {
87
                $typeCount = count($types);
88
89
                $dataCount = count($data);
90
91
                if ($typeCount !== $dataCount) {
92
                    throw WrongType::fromMessage("Tuple count mismatch, excpected exactly {$typeCount} elements, and got {$dataCount}");
93
                }
94
95
                $this->types = $types;
96
                $this->data = $data;
97
                $this->position = 0;
98
            }
99
100
            public function current(): array
101
            {
102
                return ['type' => current($this->types), 'value' => current($this->data)];
103
            }
104
105
            public function next(): void
106
            {
107
                $this->position++;
108
            }
109
110
            public function key(): int
111
            {
112
                return $this->position;
113
            }
114
115
            public function valid(): bool
116
            {
117
                return isset($this->types[$this->position]) && array_key_exists($this->position, $this->data);
118
            }
119
120
            public function rewind(): void
121
            {
122
                $this->position = 0;
123
            }
124
        };
125
    }
126
}
127