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.

Tuple::createIterator()
last analyzed

Size

Total Lines 50
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
c 0
b 0
f 0
eloc 16
nc 1
nop 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Tuple.php$0 ➔ current() 0 3 1
A Tuple.php$0 ➔ next() 0 3 1
A Tuple.php$0 ➔ rewind() 0 3 1
A Tuple.php$0 ➔ __construct() 0 13 2
A Tuple.php$0 ➔ key() 0 3 1
A Tuple.php$0 ➔ valid() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spatie\Typed;
6
7
use Iterator;
8
use ArrayAccess;
9
use Spatie\Typed\Excpetions\WrongType;
10
use Spatie\Typed\Excpetions\UninitialisedError;
11
12
class Tuple implements ArrayAccess
13
{
14
    use ValidatesType;
15
16
    /** @var \Spatie\Typed\Type[] */
17
    private $types = [];
18
19
    /** @var array */
20
    private $values = [];
21
22
    public function __construct(...$types)
23
    {
24
        foreach ($types as $field => $type) {
25
            if (! $type instanceof Type) {
26
                $this->values[$field] = $type;
27
28
                $type = T::infer($type);
29
            }
30
31
            $this->types[$field] = $type;
32
        }
33
    }
34
35
    public function set(...$values): self
36
    {
37
        $iterator = $this->createIterator($values);
38
39
        foreach ($iterator as $key => ['type' => $type, 'value' => $value]) {
40
            $values[$key] = $this->validateType($type, $value);
41
        }
42
43
        $this->values = $values;
44
45
        return $this;
46
    }
47
48
    public function offsetGet($offset)
49
    {
50
        if (! array_key_exists($offset, $this->values)) {
51
            throw UninitialisedError::forField("index {$offset}");
52
        }
53
54
        return $this->values[$offset];
55
    }
56
57
    public function offsetSet($offset, $value)
58
    {
59
        if ($offset === null || ! is_numeric($offset)) {
60
            throw WrongType::withMessage('You must specify a numeric offset');
61
        }
62
63
        $type = $this->types[$offset] ?? null;
64
65
        if (! $type) {
66
            throw WrongType::withMessage("No type was configured for this tuple at offset {$offset}");
67
        }
68
69
        $this->values[$offset] = $this->validateType($type, $value);
70
    }
71
72
    public function offsetExists($offset)
73
    {
74
        return array_key_exists($offset, $this->values);
75
    }
76
77
    public function offsetUnset($offset)
78
    {
79
        throw WrongType::withMessage('Tuple values cannot be unset');
80
    }
81
82
    public function toArray(): array
83
    {
84
        return $this->values;
85
    }
86
87
    private function createIterator(array $values): Iterator
88
    {
89
        return new class($this->types, $values) implements Iterator {
90
            /** @var array */
91
            private $types;
92
93
            /** @var array */
94
            private $values;
95
96
            /** @var int */
97
            private $position;
98
99
            public function __construct(array $types, array $values)
100
            {
101
                $typeCount = count($types);
102
103
                $dataCount = count($values);
104
105
                if ($typeCount !== $dataCount) {
106
                    throw WrongType::withMessage("Tuple count mismatch, expected exactly {$typeCount} elements, and got {$dataCount}");
107
                }
108
109
                $this->types = $types;
110
                $this->values = $values;
111
                $this->position = 0;
112
            }
113
114
            public function current(): array
115
            {
116
                return ['type' => $this->types[$this->position], 'value' => $this->values[$this->position]];
117
            }
118
119
            public function next(): void
120
            {
121
                $this->position++;
122
            }
123
124
            public function key(): int
125
            {
126
                return $this->position;
127
            }
128
129
            public function valid(): bool
130
            {
131
                return isset($this->types[$this->position]) && array_key_exists($this->position, $this->values);
132
            }
133
134
            public function rewind(): void
135
            {
136
                $this->position = 0;
137
            }
138
        };
139
    }
140
}
141