TypeMap   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 15
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A throw() 0 8 1
A offsetGet() 0 6 2
A offsetSet() 0 9 3
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics\Generator\Type;
4
5
use ArrayObject;
6
use TypeError;
7
use function gettype;
8
use function is_string;
9
use function sprintf;
10
11
final class TypeMap extends ArrayObject
12
{
13
    /**
14
     * @param string $offset
15
     * @return TypeArgument
16
     */
17
    public function offsetGet($offset): TypeArgument
18
    {
19
        if (!is_string($offset)) {
0 ignored issues
show
introduced by
The condition is_string($offset) is always true.
Loading history...
20
            $this->throw($offset, 'string', __METHOD__, 0);
21
        }
22
        return parent::offsetGet($offset);
23
    }
24
25
    /**
26
     * @param string $offset
27
     * @param TypeArgument $value
28
     */
29
    public function offsetSet($offset, $value): void
30
    {
31
        if (!is_string($offset)) {
0 ignored issues
show
introduced by
The condition is_string($offset) is always true.
Loading history...
32
            $this->throw($offset, 'string', __METHOD__, 0);
33
        }
34
        if (!$value instanceof TypeArgument) {
0 ignored issues
show
introduced by
$value is always a sub-type of Stratadox\PhpGenerics\Generator\Type\TypeArgument.
Loading history...
35
            $this->throw($value, TypeArgument::class, __METHOD__, 1);
36
        }
37
        parent::offsetSet($offset, $value);
38
    }
39
40
    private function throw($value, string $type, string $source, int $argument): void
41
    {
42
        throw new TypeError(sprintf(
43
            'Argument %d passed to %s must be of type %s, %s given',
44
            $argument,
45
            $source,
46
            $type,
47
            gettype($value)
48
        ));
49
    }
50
}
51