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
Pull Request — master (#7)
by Andreas
01:42
created

Tuple.php$0 ➔ key()   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
        {
77
            /** @var array */
78
            private $types;
79
80
            /** @var array */
81
            private $data;
82
83
            /** @var int */
84
            private $position;
85
86
            public function __construct(array $types, array $data)
87
            {
88
                $typeCount = count($types);
89
90
                $dataCount = count($data);
91
92
                if ($typeCount !== $dataCount) {
93
                    throw WrongType::fromMessage("Tuple count mismatch, excpected exactly {$typeCount} elements, and got {$dataCount}");
94
                }
95
96
                $this->types = $types;
97
                $this->data = $data;
98
                $this->position = 0;
99
            }
100
101
            public function current(): array
102
            {
103
                return ['type' => current($this->types), 'value' => current($this->data)];
104
            }
105
106
            public function next(): void
107
            {
108
                $this->position++;
109
            }
110
111
            public function key(): int
112
            {
113
                return $this->position;
114
            }
115
116
            public function valid(): bool
117
            {
118
                return isset($this->types[$this->position]) && array_key_exists($this->position, $this->data);
119
            }
120
121
            public function rewind(): void
122
            {
123
                $this->position = 0;
124
            }
125
        };
126
    }
127
}
128