• 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/AbstractType.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 Jojo1981\PhpTypes\Parser\Parser;
14
use Jojo1981\PhpTypes\Value\ClassName;
15
use Jojo1981\PhpTypes\Value\Exception\ValueException;
16
use function get_class;
17
use function gettype;
18
use function is_a;
19
use function is_callable;
20
use function is_iterable;
21
use function is_object;
22

23
/**
24
 * Abstract factory pattern implemented here.
25
 *
26
 * @package Jojo1981\PhpTypes
27
 */
28
abstract class AbstractType implements TypeInterface
29
{
30
    /**
31
     * @return bool
32
     */
33
    public function isScalar(): bool
34
    {
35
        return false;
10✔
36
    }
37

38
    /**
39
     * @return bool
40
     */
41
    public function isCompound(): bool
42
    {
43
        return false;
8✔
44
    }
45

46
    /**
47
     * @return bool
48
     */
49
    public function isNumber(): bool
50
    {
51
        return false;
12✔
52
    }
53

54
    /**
55
     * @return bool
56
     */
57
    public function isPseudoType(): bool
58
    {
59
        return false;
11✔
60
    }
61

62
    /**
63
     * @return bool
64
     */
65
    public function isBoolean(): bool
66
    {
67
        return false;
13✔
68
    }
69

70
    /**
71
     * @return bool
72
     */
73
    public function isString(): bool
74
    {
75
        return false;
13✔
76
    }
77

78
    /**
79
     * @return bool
80
     */
81
    public function isObject(): bool
82
    {
83
        return false;
12✔
84
    }
85

86
    /**
87
     * @return bool
88
     */
89
    public function isClass(): bool
90
    {
91
        return false;
13✔
92
    }
93

94
    /**
95
     * @return bool
96
     */
97
    public function isInteger(): bool
98
    {
99
        return false;
14✔
100
    }
101

102
    /**
103
     * @return bool
104
     */
105
    public function isFloat(): bool
106
    {
107
        return false;
13✔
108
    }
109

110
    /**
111
     * @return bool
112
     */
113
    public function isArray(): bool
114
    {
115
        return false;
13✔
116
    }
117

118
    /**
119
     * @return bool
120
     */
121
    public function isIterable(): bool
122
    {
123
        return false;
12✔
124
    }
125

126
    /**
127
     * @return bool
128
     */
129
    public function isCallable(): bool
130
    {
131
        return false;
13✔
132
    }
133

134
    /**
135
     * @return bool
136
     */
137
    public function isResource(): bool
138
    {
139
        return false;
13✔
140
    }
141

142
    /**
143
     * @return bool
144
     */
145
    public function isNull(): bool
146
    {
147
        return false;
13✔
148
    }
149

150
    /**
151
     * @return bool
152
     */
153
    public function isVoid(): bool
154
    {
155
        return false;
37✔
156
    }
157

158
    /**
159
     * @param TypeInterface $type
160
     * @return bool
161
     */
162
    public function isEqual(TypeInterface $type): bool
163
    {
164
        return get_class($this) === get_class($type);
14✔
165
    }
166

167
    /**
168
     * @param TypeInterface $type
169
     * @return bool
170
     */
171
    public function isAssignableType(TypeInterface $type): bool
172
    {
173
        return is_a($type, get_class($this));
12✔
174
    }
175

176
    /**
177
     * @param string $typeName
178
     * @return TypeInterface
179
     * @throws TypeException
180
     */
181
    final public static function createFromTypeName(string $typeName): TypeInterface
182
    {
183
        return (new Parser())->parse($typeName);
64✔
184
    }
185

186
    /**
187
     * @param mixed $value
188
     * @return TypeInterface
189
     * @throws ValueException
190
     * @throws TypeException
191
     */
192
    final public static function createFromValue($value): TypeInterface
193
    {
194
        if (is_object($value)) {
20✔
195
            if (is_iterable($value)) {
5✔
196
                return self::createFromTypeName('iterable');
1✔
197
            }
198
            if (is_callable($value)) {
4✔
199
                return self::createFromTypeName('callable');
2✔
200
            }
201

202
            return new ClassType(new ClassName(get_class($value)));
2✔
203
        }
204

205
        return self::createFromTypeName(gettype($value));
15✔
206
    }
207

208
    /**
209
     * @param TypeInterface[] $types
210
     * @throws TypeException
211
     * @return MultiType
212
     */
213
    final public static function createFromTypes(array $types): MultiType
214
    {
215
        return new MultiType($types);
4✔
216
    }
217

218
    /**
219
     * @return string
220
     */
221
    public function __toString(): string
222
    {
223
        return $this->getName();
2✔
224
    }
225
}
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