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

voku / Arrayy / 3787581697

pending completion
3787581697

push

github

Lars Moelleken
[*]: update the changelog

2479 of 2793 relevant lines covered (88.76%)

30.03 hits per line

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

73.77
/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
use phpDocumentor\Reflection\Type;
11

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

24
    /**
25
     * @var string
26
     */
27
    private $property_name;
28

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

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

52
            $property = $propertyTmp;
×
53
        }
54

55
        $tmpObject = new \stdClass();
4✔
56
        $tmpObject->{$property} = null;
4✔
57

58
        $tmpReflection = new self((new \ReflectionProperty($tmpObject, $property))->getName());
4✔
59

60
        $type = $phpDocumentorReflectionProperty->getType();
4✔
61

62
        /** @noinspection PhpSillyAssignmentInspection */
63
        /** @var Type|null $type */
64
        $type = $type;
4✔
65

66
        if ($type) {
4✔
67
            $tmpReflection->hasTypeDeclaration = true;
4✔
68

69
            $docTypes = self::parseDocTypeObject($type);
4✔
70
            if (\is_array($docTypes) === true) {
4✔
71
                foreach ($docTypes as $docType) {
2✔
72
                    $tmpReflection->types[] = $docType;
2✔
73
                }
74
            } else {
75
                $tmpReflection->types[] = $docTypes;
4✔
76
            }
77

78
            if (\in_array('null', $tmpReflection->types, true)) {
4✔
79
                $tmpReflection->isNullable = true;
2✔
80
            }
81
        }
82

83
        return $tmpReflection;
4✔
84
    }
85

86
    /**
87
     * @param \phpDocumentor\Reflection\Type $type
88
     *
89
     * @return string|string[]
90
     */
91
    public static function parseDocTypeObject($type)
92
    {
93
        if ($type instanceof \phpDocumentor\Reflection\Types\Object_) {
4✔
94
            $tmpObject = (string) $type->getFqsen();
2✔
95
            if ($tmpObject) {
2✔
96
                return $tmpObject;
2✔
97
            }
98

99
            return 'object';
×
100
        }
101

102
        if ($type instanceof \phpDocumentor\Reflection\Types\Compound) {
3✔
103
            $types = [];
2✔
104
            foreach ($type as $subType) {
2✔
105
                $typeTmp = self::parseDocTypeObject($subType);
2✔
106

107
                /** @noinspection PhpSillyAssignmentInspection - hack for phpstan */
108
                /** @var string $typeTmp */
109
                $typeTmp = $typeTmp;
2✔
110

111
                $types[] = $typeTmp;
2✔
112
            }
113

114
            return $types;
2✔
115
        }
116

117
        if ($type instanceof \phpDocumentor\Reflection\Types\Array_) {
3✔
118
            $valueTypeTmp = $type->getValueType()->__toString();
2✔
119
            if ($valueTypeTmp !== 'mixed') {
2✔
120
                return $valueTypeTmp . '[]';
2✔
121
            }
122

123
            return 'array';
×
124
        }
125

126
        if ($type instanceof \phpDocumentor\Reflection\Types\Null_) {
3✔
127
            return 'null';
2✔
128
        }
129

130
        if ($type instanceof \phpDocumentor\Reflection\Types\Mixed_) {
3✔
131
            return 'mixed';
×
132
        }
133

134
        if ($type instanceof \phpDocumentor\Reflection\Types\Scalar) {
3✔
135
            return 'string|int|float|bool';
×
136
        }
137

138
        if ($type instanceof \phpDocumentor\Reflection\Types\Boolean) {
3✔
139
            return 'bool';
×
140
        }
141

142
        if ($type instanceof \phpDocumentor\Reflection\Types\Callable_) {
3✔
143
            return 'callable';
×
144
        }
145

146
        if ($type instanceof \phpDocumentor\Reflection\Types\Float_) {
3✔
147
            return 'float';
×
148
        }
149

150
        if ($type instanceof \phpDocumentor\Reflection\Types\String_) {
3✔
151
            return 'string';
3✔
152
        }
153

154
        if ($type instanceof \phpDocumentor\Reflection\Types\Integer) {
1✔
155
            return 'int';
1✔
156
        }
157

158
        if ($type instanceof \phpDocumentor\Reflection\Types\Void_) {
×
159
            return 'void';
×
160
        }
161

162
        if ($type instanceof \phpDocumentor\Reflection\Types\Resource_) {
×
163
            return 'resource';
×
164
        }
165

166
        return $type->__toString();
×
167
    }
168

169
    /**
170
     * @param string $expectedTypes
171
     * @param mixed  $value
172
     * @param string $type
173
     *
174
     * @return \TypeError
175
     */
176
    public function throwException($expectedTypes, $value, $type): \Throwable
177
    {
178
        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}}.");
5✔
179
    }
180
}
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

© 2025 Coveralls, Inc