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

voku / Arrayy / 25167597779

30 Apr 2026 01:17PM UTC coverage: 92.313% (-0.06%) from 92.372%
25167597779

Pull #165

github

web-flow
Merge 7bafc7f9a into d1a0c6cda
Pull Request #165: Expand PHPStan coverage for `meta()` and array-shape-backed `Arrayy` models

15 of 16 new or added lines in 1 file covered. (93.75%)

2 existing lines in 1 file now uncovered.

2762 of 2992 relevant lines covered (92.31%)

35.59 hits per line

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

83.22
/src/TypeCheck/TypeCheckPhpDoc.php
1
<?php
2

3
/** @noinspection TransitiveDependenciesUsageInspection */
4
/** @noinspection ClassReImplementsParentInterfaceInspection */
5

6
declare(strict_types=1);
7

8
namespace Arrayy\TypeCheck;
9

10
/**
11
 * inspired by https://github.com/spatie/value-object
12
 *
13
 * @internal
14
 */
15
final class TypeCheckPhpDoc extends AbstractTypeCheck implements TypeCheckInterface
16
{
17
    /**
18
     * @var bool
19
     */
20
    private $hasTypeDeclaration = false;
21

22
    /**
23
     * @var string
24
     */
25
    private $property_name;
26

27
    /**
28
     * @param string $reflectionPropertyName
29
     */
30
    public function __construct($reflectionPropertyName)
31
    {
32
        $this->property_name = $reflectionPropertyName;
26✔
33
    }
34

35
    /**
36
     * @param \phpDocumentor\Reflection\DocBlock\Tags\Property $phpDocumentorReflectionProperty
37
     * @param string                                           $property
38
     *
39
     * @return self|null
40
     */
41
    public static function fromPhpDocumentorProperty(\phpDocumentor\Reflection\DocBlock\Tags\Property $phpDocumentorReflectionProperty, string $property = '')
42
    {
43
        if (!$property) {
8✔
44
            /** @var string|null $propertyTmp */
45
            $propertyTmp = $phpDocumentorReflectionProperty->getVariableName();
2✔
46
            if ($propertyTmp === null) {
2✔
47
                return null;
×
48
            }
49

50
            $property = $propertyTmp;
2✔
51
        }
52

53
        return self::fromDocTypeObject($property, $phpDocumentorReflectionProperty->getType());
8✔
54
    }
55

56
    /**
57
     * Create a type checker from a phpDocumentor type object and an explicit property name.
58
     *
59
     * @param string                              $property
60
     * @param \phpDocumentor\Reflection\Type|null $type
61
     *
62
     * @phpstan-param \phpDocumentor\Reflection\Type|null $type
63
     *
64
     * @return self
65
     */
66
    public static function fromDocTypeObject(string $property, $type)
67
    {
68
        $tmpReflection = new self($property);
20✔
69

70
        if ($type) {
20✔
71
            $tmpReflection->hasTypeDeclaration = true;
19✔
72

73
            $docTypes = self::parseDocTypeObject($type);
19✔
74
            if (\is_array($docTypes) === true) {
19✔
75
                foreach ($docTypes as $docType) {
6✔
76
                    $tmpReflection->types[] = $docType;
6✔
77
                }
78
            } else {
79
                $tmpReflection->types[] = $docTypes;
18✔
80
            }
81

82
            if (\in_array('null', $tmpReflection->types, true)) {
19✔
83
                $tmpReflection->isNullable = true;
7✔
84
            }
85
        }
86

87
        return $tmpReflection;
20✔
88
    }
89

90
    public static function fromReflectionProperty(\ReflectionProperty $reflectionProperty): self
91
    {
92
        $tmpReflection = new self($reflectionProperty->getName());
6✔
93
        $type = $reflectionProperty->getType();
6✔
94
        $docTypes = self::getTypesFromReflectionPropertyDocBlock($reflectionProperty);
6✔
95

96
        if ($docTypes !== null) {
6✔
97
            $tmpReflection->hasTypeDeclaration = true;
4✔
98

99
            if (\is_array($docTypes) === true) {
4✔
100
                foreach ($docTypes as $docType) {
1✔
101
                    $tmpReflection->types[] = $docType;
1✔
102
                }
103
            } else {
104
                $tmpReflection->types[] = $docTypes;
4✔
105
            }
106
        } elseif ($type === null) {
4✔
107
            $tmpReflection->types[] = 'mixed';
1✔
108
            $tmpReflection->isNullable = true;
1✔
109

110
            return $tmpReflection;
1✔
111
        } else {
112
            $tmpReflection->hasTypeDeclaration = true;
3✔
113

114
            $docTypes = self::parseReflectionTypeObject($type);
3✔
115
            if (\is_array($docTypes) === true) {
3✔
116
                foreach ($docTypes as $docType) {
2✔
117
                    $tmpReflection->types[] = $docType;
2✔
118
                }
119
            } else {
120
                $tmpReflection->types[] = $docTypes;
3✔
121
            }
122
        }
123

124
        if ($type !== null && self::typeAllowsNull($type) && \in_array('null', $tmpReflection->types, true) === false) {
5✔
125
            $tmpReflection->types[] = 'null';
3✔
126
        }
127

128
        if (\in_array('null', $tmpReflection->types, true)) {
5✔
129
            $tmpReflection->isNullable = true;
3✔
130
        }
131

132
        return $tmpReflection;
5✔
133
    }
134

135
    /**
136
     * @param \phpDocumentor\Reflection\Type $type
137
     *
138
     * @return string|string[]
139
     */
140
    public static function parseDocTypeObject($type)
141
    {
142
        if ($type instanceof \phpDocumentor\Reflection\Types\Object_) {
23✔
143
            $tmpObject = (string) $type->getFqsen();
8✔
144
            if ($tmpObject) {
8✔
145
                return $tmpObject;
8✔
146
            }
147

148
            return 'object';
×
149
        }
150

151
        if ($type instanceof \phpDocumentor\Reflection\Types\Compound) {
21✔
152
            $types = [];
6✔
153
            foreach ($type as $subType) {
6✔
154
                $typeTmp = self::parseDocTypeObject($subType);
6✔
155

156
                /** @noinspection PhpSillyAssignmentInspection - hack for phpstan */
157
                /** @var string $typeTmp */
158
                $typeTmp = $typeTmp;
6✔
159

160
                $types[] = $typeTmp;
6✔
161
            }
162

163
            return $types;
6✔
164
        }
165

166
        if ($type instanceof \phpDocumentor\Reflection\Types\Nullable) {
21✔
167
            $typeTmp = self::parseDocTypeObject($type->getActualType());
1✔
168
            if (\is_array($typeTmp) === true) {
1✔
169
                $typeTmp[] = 'null';
×
170

171
                return $typeTmp;
×
172
            }
173

174
            return [$typeTmp, 'null'];
1✔
175
        }
176

177
        if (
178
            \class_exists('\phpDocumentor\Reflection\PseudoTypes\ArrayShape')
20✔
179
            &&
180
            $type instanceof \phpDocumentor\Reflection\PseudoTypes\ArrayShape
20✔
181
        ) {
182
            return 'array';
1✔
183
        }
184

185
        if ($type instanceof \phpDocumentor\Reflection\Types\Array_) {
19✔
186
            $valueTypeTmp = $type->getValueType()->__toString();
7✔
187
            if ($valueTypeTmp !== 'mixed') {
7✔
188
                return $valueTypeTmp . '[]';
7✔
189
            }
190

191
            return 'array';
×
192
        }
193

194
        if ($type instanceof \phpDocumentor\Reflection\Types\Null_) {
17✔
195
            return 'null';
6✔
196
        }
197

198
        if ($type instanceof \phpDocumentor\Reflection\Types\Mixed_) {
17✔
199
            return 'mixed';
1✔
200
        }
201

202
        foreach (self::getScalarPseudoTypeClasses() as $scalarTypeClass) {
17✔
203
            if ($type instanceof $scalarTypeClass) {
17✔
204
                return 'string|int|float|bool';
3✔
205
            }
206
        }
207

208
        if ($type instanceof \phpDocumentor\Reflection\Types\Boolean) {
16✔
209
            return 'bool';
1✔
210
        }
211

212
        if ($type instanceof \phpDocumentor\Reflection\Types\Callable_) {
16✔
213
            return 'callable';
1✔
214
        }
215

216
        if ($type instanceof \phpDocumentor\Reflection\Types\Float_) {
15✔
217
            return 'float';
1✔
218
        }
219

220
        if ($type instanceof \phpDocumentor\Reflection\Types\String_) {
15✔
221
            return 'string';
9✔
222
        }
223

224
        if ($type instanceof \phpDocumentor\Reflection\Types\Integer) {
11✔
225
            return 'int';
10✔
226
        }
227

228
        if ($type instanceof \phpDocumentor\Reflection\Types\Void_) {
1✔
229
            return 'void';
×
230
        }
231

232
        if ($type instanceof \phpDocumentor\Reflection\Types\Resource_) {
1✔
233
            return 'resource';
1✔
234
        }
235

UNCOV
236
        return $type->__toString();
×
237
    }
238

239
    /**
240
     * @return list<class-string>
241
     */
242
    private static function getScalarPseudoTypeClasses(): array
243
    {
244
        $classes = [];
17✔
245

246
        foreach (
247
            [
17✔
248
                '\phpDocumentor\Reflection\PseudoTypes\Scalar',
17✔
249
                '\phpDocumentor\Reflection\Types\Scalar',
17✔
250
            ] as $className
17✔
251
        ) {
252
            if (\class_exists($className)) {
17✔
253
                $classes[] = $className;
17✔
254
            }
255
        }
256

257
        return $classes;
17✔
258
    }
259

260
    /**
261
     * @return string|string[]|null
262
     */
263
    private static function getTypesFromReflectionPropertyDocBlock(\ReflectionProperty $reflectionProperty)
264
    {
265
        $docComment = $reflectionProperty->getDocComment();
6✔
266
        if ($docComment === false) {
6✔
267
            return null;
4✔
268
        }
269

270
        /** @var \phpDocumentor\Reflection\DocBlockFactoryInterface|null $factory cache factory to avoid recreating it per reflected property */
271
        static $factory = null;
4✔
272
        if ($factory === null) {
4✔
273
            $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
1✔
274
        }
275
        $docblock = $factory->create($docComment);
4✔
276
        foreach ($docblock->getTagsByName('var') as $tag) {
4✔
277
            if (!$tag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Var_) {
4✔
278
                continue;
×
279
            }
280

281
            /** @var \phpDocumentor\Reflection\Type|null $type */
282
            $type = $tag->getType();
4✔
283
            if ($type === null) {
4✔
284
                continue;
×
285
            }
286

287
            return self::parseDocTypeObject($type);
4✔
288
        }
289

290
        return null;
×
291
    }
292

293
    private static function typeAllowsNull(\ReflectionType $type): bool
294
    {
295
        if (
296
            \class_exists(\ReflectionIntersectionType::class)
4✔
297
            &&
298
            $type instanceof \ReflectionIntersectionType
4✔
299
        ) {
UNCOV
300
            return false;
×
301
        }
302

303
        return $type->allowsNull();
4✔
304
    }
305

306
    /**
307
     * @param \ReflectionType $type
308
     *
309
     * @return string|string[]
310
     */
311
    public static function parseReflectionTypeObject(\ReflectionType $type)
312
    {
313
        if ($type instanceof \ReflectionNamedType) {
3✔
314
            $typeName = $type->getName();
3✔
315

316
            if ($type->isBuiltin()) {
3✔
317
                return $typeName;
3✔
318
            }
319

320
            return '\\' . \ltrim($typeName, '\\');
2✔
321
        }
322

323
        if ($type instanceof \ReflectionUnionType) {
2✔
324
            $types = [];
2✔
325
            foreach ($type->getTypes() as $subType) {
2✔
326
                $typeTmp = self::parseReflectionTypeObject($subType);
2✔
327
                if (\is_array($typeTmp)) {
2✔
328
                    foreach ($typeTmp as $typeTmpInner) {
×
329
                        $types[] = $typeTmpInner;
×
330
                    }
331
                } else {
332
                    $types[] = $typeTmp;
2✔
333
                }
334
            }
335

336
            return $types;
2✔
337
        }
338

339
        if (
340
            \class_exists(\ReflectionIntersectionType::class)
×
341
            &&
342
            $type instanceof \ReflectionIntersectionType
×
343
        ) {
344
            $types = [];
×
345
            foreach ($type->getTypes() as $subType) {
×
346
                $typeTmp = self::parseReflectionTypeObject($subType);
×
347
                if (\is_array($typeTmp)) {
×
348
                    foreach ($typeTmp as $typeTmpInner) {
×
349
                        $types[] = $typeTmpInner;
×
350
                    }
351
                } else {
352
                    $types[] = $typeTmp;
×
353
                }
354
            }
355

356
            return \implode('&', $types);
×
357
        }
358

359
        return (string) $type;
×
360
    }
361

362
    /**
363
     * @param string $expectedTypes
364
     * @param mixed  $value
365
     * @param string $type
366
     *
367
     * @return \TypeError
368
     */
369
    public function throwException($expectedTypes, $value, $type): \Throwable
370
    {
371
        throw new \TypeError("Invalid type: expected \"{$this->property_name}\" to be of type {{$expectedTypes}}, instead got value \"" . $this->valueToString($value) . '" (' . \print_r($value, true) . ") with type {{$type}}.");
28✔
372
    }
373
}
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