• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jojo1981 / php-types / 12707605573

10 Jan 2025 10:18AM UTC coverage: 88.119%. Remained the same
12707605573

push

github

jojo1981
Use GitHub actions workflow for CI instead of Travis CI.

267 of 303 relevant lines covered (88.12%)

16.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/ArrayType.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the jojo1981/php-types package
4
 *
5
 * Copyright (c) 2020 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
namespace Jojo1981\PhpTypes;
11

12
use Jojo1981\PhpTypes\Exception\TypeException;
13
use function is_array;
14

15
/**
16
 * @package Jojo1981\PhpTypes
17
 */
18
class ArrayType extends AbstractCompoundType
19
{
20
    /** @var TypeInterface|null */
21
    private ?TypeInterface $valueType;
22

23
    /** @var TypeInterface|null */
24
    private ?TypeInterface $keyType;
25

26
    /**
27
     * @param TypeInterface|null $valueType
28
     * @param TypeInterface|null $keyType
29
     * @throws TypeException
30
     */
31
    public function __construct(?TypeInterface $valueType = null, ?TypeInterface $keyType = null)
32
    {
33
        $this->valueType = $this->assertValueType($valueType);
65✔
34
        $this->keyType = $this->assertKeyType($keyType);
65✔
35
    }
36

37
    /**
38
     * @return string
39
     */
40
    public function getName(): string
41
    {
42
        return 'array';
3✔
43
    }
44

45
    /**
46
     * @return TypeInterface|null
47
     */
48
    public function getValueType(): ?TypeInterface
49
    {
50
        return $this->valueType;
1✔
51
    }
52

53
    /**
54
     * @return TypeInterface|null
55
     */
56
    public function getKeyType(): ?TypeInterface
57
    {
58
        return $this->keyType;
1✔
59
    }
60

61
    /**
62
     * @return bool
63
     */
64
    public function isArray(): bool
65
    {
66
        return true;
1✔
67
    }
68

69
    /**
70
     * @return bool
71
     */
72
    public function isIterable(): bool
73
    {
74
        return true;
1✔
75
    }
76

77
    /**
78
     * @return bool
79
     */
80
    public function isPseudoType(): bool
81
    {
82
        return null !== $this->valueType || null !== $this->keyType;
1✔
83
    }
84

85
    /**
86
     * @param mixed $value
87
     * @return bool
88
     */
89
    public function isAssignableValue($value): bool
90
    {
91
        return is_array($value);
1✔
92
    }
93

94
    /**
95
     * @return string
96
     */
97
    public function __toString(): string
98
    {
99
        if (null !== $this->valueType && null !== $this->keyType && !$this->keyType->isInteger()) {
1✔
100
            return $this->getName() . '<' . $this->keyType . ',' . $this->valueType . '>';
1✔
101
        }
102
        if (null !== $this->valueType) {
1✔
103
            return $this->valueType->getName() . '[]';
1✔
104
        }
105

106
        return $this->getName();
1✔
107
    }
108

109
    /**
110
     * @param TypeInterface|null $keyType
111
     * @return TypeInterface|null
112
     * @throws TypeException
113
     */
114
    private function assertKeyType(?TypeInterface $keyType): ?TypeInterface
115
    {
116
        if (null !== $keyType && $keyType->isVoid()) {
65✔
117
            throw new TypeException('Array key type can not be of type void');
1✔
118
        }
119

120
        return $keyType;
65✔
121
    }
122

123
    /**
124
     * @param TypeInterface|null $valueType
125
     * @return TypeInterface|null
126
     * @throws TypeException
127
     */
128
    private function assertValueType(?TypeInterface $valueType): ?TypeInterface
129
    {
130
        if (null !== $valueType && $valueType->isVoid()) {
65✔
131
            throw new TypeException('Array value type can not be of type void');
1✔
132
        }
133

134
        return $valueType;
65✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc