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:40
created

Tuple.php$0 ➔ offsetExists()   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 = new class($this->types, $data) implements Iterator {
28
            /** @var array */
29
            private $types;
30
31
            /** @var array */
32
            private $data;
33
34
            /** @var int */
35
            private $position;
36
37
            public function __construct(array $types, array $data)
38
            {
39
                $typeCount = count($types);
40
41
                $dataCount = count($data);
42
43
                if ($typeCount !== $dataCount) {
44
                    throw WrongType::fromMessage("Tuple count mismatch, excpected exactly {$typeCount} elements, and got {$dataCount}");
45
                }
46
47
                $this->types = $types;
48
                $this->data = $data;
49
                $this->position = 0;
50
            }
51
52
            public function current(): array
53
            {
54
                return ['type' => current($this->types), 'value' => current($this->data)];
55
            }
56
57
            public function next(): void
58
            {
59
                $this->position++;
60
            }
61
62
            public function key(): int
63
            {
64
                return $this->position;
65
            }
66
67
            public function valid(): bool
68
            {
69
                return isset($this->types[$this->position]) && array_key_exists($this->position, $this->data);
70
            }
71
72
            public function rewind(): void
73
            {
74
                $this->position = 0;
75
            }
76
        };
77
78
        foreach ($iterator as $key => ['type' => $type, 'value' => $value]) {
79
            $data[$key] = $this->validateType($type, $value);
80
        }
81
82
        $this->data = $data;
83
84
        return $this;
85
    }
86
87
    public function offsetGet($offset)
88
    {
89
        return isset($this->data[$offset]) ? $this->data[$offset] : null;
90
    }
91
92
    public function offsetSet($offset, $value)
93
    {
94
        if ($offset === null || ! is_numeric($offset)) {
95
            throw WrongType::fromMessage('You must specify a numeric offset');
96
        }
97
98
        $type = $this->types[$offset] ?? null;
99
100
        if (! $type) {
101
            throw WrongType::fromMessage("No type was configured for this tuple at offset {$offset}");
102
        }
103
104
        $this->data[$offset] = $this->validateType($type, $value);
105
    }
106
107
    public function offsetExists($offset)
108
    {
109
        return array_key_exists($offset, $this->data);
110
    }
111
112
    public function offsetUnset($offset)
113
    {
114
        throw WrongType::fromMessage('Tuple values cannot be unset');
115
    }
116
117
    public function toArray(): array
118
    {
119
        return $this->data;
120
    }
121
}
122