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

voku / Arrayy / 3800146618

pending completion
3800146618

push

github

GitHub
Merge pull request #118 from voku/renovate/actions-cache-3.x

2276 of 2570 relevant lines covered (88.56%)

34.69 hits per line

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

89.13
/src/TypeCheck/AbstractTypeCheck.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Arrayy\TypeCheck;
6

7
abstract class AbstractTypeCheck implements TypeCheckInterface
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $isNullable = false;
13

14
    /**
15
     * @var string[]
16
     */
17
    protected $types = [];
18

19
    /**
20
     * @var array<string, string>
21
     */
22
    private static $typeMapping = [
23
        'int'   => 'integer',
24
        'bool'  => 'boolean',
25
        'float' => 'double',
26
    ];
27

28
    /**
29
     * @return string[]
30
     */
31
    public function getTypes(): array
32
    {
33
        return $this->types;
7✔
34
    }
35

36
    /**
37
     * @param mixed $value
38
     *
39
     * @return bool
40
     */
41
    public function checkType(&$value): bool
42
    {
43
        if ($this->isNullable && $value === null) {
130✔
44
            return true;
16✔
45
        }
46

47
        foreach ($this->types as $currentType) {
130✔
48
            $isValidType = $this->assertTypeEquals($currentType, $value);
130✔
49

50
            if ($isValidType) {
130✔
51
                return true;
130✔
52
            }
53
        }
54

55
        $type = \gettype($value);
40✔
56

57
        $expectedTypes = \implode('|', $this->types);
40✔
58

59
        $this->throwException($expectedTypes, $value, $type);
40✔
60

61
        return false;
×
62
    }
63

64
    /**
65
     * @param string $type
66
     * @param mixed  $value
67
     *
68
     * @return bool
69
     */
70
    protected function assertTypeEquals(string $type, &$value): bool
71
    {
72
        if (\strpos($type, '[]') !== false) {
130✔
73
            return $this->isValidGenericCollection($type, $value);
24✔
74
        }
75

76
        if ($type === 'mixed' && $value !== null) {
130✔
77
            return true;
4✔
78
        }
79

80
        return $value instanceof $type
130✔
81
               ||
82
               \gettype($value) === (self::$typeMapping[$type] ?? $type)
108✔
83
               ||
84
               (
85
                   $type === 'scalar'
49✔
86
                    &&
87
                    \is_scalar($value)
49✔
88
               )
89
               ||
90
               (
91
                   $type === 'callable'
47✔
92
                   &&
93
                   \is_callable($value)
47✔
94
               )
95
               ||
96
               (
97
                   $type === 'numeric'
46✔
98
                   &&
99
                   (
100
                       \is_float($value)
×
101
                       ||
102
                       \is_int($value)
46✔
103
                   )
104
               )
105
               ||
106
               (
107
                   $type === 'resource'
46✔
108
                   &&
109
                   \is_resource($value)
130✔
110
               );
111
    }
112

113
    /**
114
     * @param mixed $value
115
     *
116
     * @return string
117
     */
118
    protected function valueToString($value): string
119
    {
120
        // null
121
        if ($value === null) {
5✔
122
            return 'NULL';
1✔
123
        }
124

125
        // bool
126
        if (\is_bool($value)) {
4✔
127
            return $value ? 'TRUE' : 'FALSE';
×
128
        }
129

130
        // array
131
        if (\is_array($value)) {
4✔
132
            return 'Array';
1✔
133
        }
134

135
        // scalar types (integer, float, string)
136
        if (\is_scalar($value)) {
3✔
137
            return (string) $value;
2✔
138
        }
139

140
        // resource
141
        if (\is_resource($value)) {
1✔
142
            return \get_resource_type($value) . ' resource #' . (int) $value;
×
143
        }
144

145
        if (\is_object($value)) {
1✔
146
            return \get_class($value) . ' Object';
1✔
147
        }
148

149
        return '';
×
150
    }
151

152
    /**
153
     * @param string $type
154
     * @param mixed  $collection
155
     *
156
     * @return bool
157
     */
158
    private function isValidGenericCollection(string $type, &$collection): bool
159
    {
160
        if (!\is_array($collection)) {
24✔
161
            return false;
1✔
162
        }
163

164
        $valueType = \str_replace('[]', '', $type);
23✔
165

166
        foreach ($collection as $value) {
23✔
167
            if ($this->assertTypeEquals($valueType, $value)) {
23✔
168
                return true;
23✔
169
            }
170
        }
171

172
        return false;
2✔
173
    }
174
}
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