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

azjezz / psl / 8524527937

02 Apr 2024 02:26PM UTC coverage: 98.907% (-0.05%) from 98.955%
8524527937

push

github

veewee
Optimize visibility and documentation

11 of 11 new or added lines in 1 file covered. (100.0%)

3 existing lines in 1 file now uncovered.

4343 of 4391 relevant lines covered (98.91%)

58.12 hits per line

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

92.86
/src/Psl/Type/Internal/NonEmptyVecType.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Type\Internal;
6

7
use Psl\Str;
8
use Psl\Type;
9
use Psl\Type\Exception\AssertException;
10
use Psl\Type\Exception\CoercionException;
11
use Throwable;
12

13
use function is_array;
14
use function is_iterable;
15

16
/**
17
 * @template Tv
18
 *
19
 * @extends Type\Type<non-empty-list<Tv>>
20
 *
21
 * @internal
22
 */
23
final class NonEmptyVecType extends Type\Type
24
{
25
    /**
26
     * @param Type\TypeInterface<Tv> $value_type
27
     */
28
    public function __construct(
29
        private readonly Type\TypeInterface $value_type
30
    ) {
31
    }
65✔
32

33
    /**
34
     * @psalm-assert-if-true non-empty-list<Tv> $value
35
     */
36
    public function matches(mixed $value): bool
37
    {
38
        if (!is_array($value)) {
24✔
39
            return false;
11✔
40
        }
41

42
        if ([] === $value) {
13✔
43
            return false;
1✔
44
        }
45

46
        $index = 0;
12✔
47
        foreach ($value as $k => $v) {
12✔
48
            if ($index !== $k) {
12✔
49
                return false;
1✔
50
            }
51

52
            if (!$this->value_type->matches($v)) {
11✔
53
                return false;
2✔
54
            }
55

56
            $index++;
9✔
57
        }
58

59
        return true;
9✔
60
    }
61

62
    /**
63
     * @throws CoercionException
64
     *
65
     * @return non-empty-list<Tv>
66
     */
67
    public function coerce(mixed $value): iterable
68
    {
69
        if (is_iterable($value)) {
17✔
70
            /** @var Type\Type<Tv> $value_type */
71
            $value_type = $this->value_type;
10✔
72

73
            /**
74
             * @var list<Tv> $entries
75
             */
76
            $result = [];
10✔
77

78
            $i = $v = null;
10✔
79
            $iterating = true;
10✔
80

81
            try {
82
                /**
83
                 * @var Tv $v
84
                 * @var array-key $i
85
                 */
86
                foreach ($value as $i => $v) {
10✔
87
                    $iterating = false;
9✔
88
                    $result[] = $value_type->coerce($v);
9✔
89
                    $iterating = true;
9✔
90
                }
UNCOV
91
            } catch (Throwable $e) {
×
92
                throw match (true) {
UNCOV
93
                    $iterating => CoercionException::withValue(null, $this->toString(), PathExpression::iteratorError($i), $e),
×
UNCOV
94
                    default => CoercionException::withValue($v, $this->toString(), PathExpression::path($i), $e)
×
95
                };
96
            }
97

98
            if ($result === []) {
10✔
99
                throw CoercionException::withValue($value, $this->toString());
1✔
100
            }
101

102
            return $result;
9✔
103
        }
104

105
        throw CoercionException::withValue($value, $this->toString());
7✔
106
    }
107

108
    /**
109
     * @throws AssertException
110
     *
111
     * @return non-empty-list<Tv>
112
     *
113
     * @psalm-assert non-empty-list<Tv> $value
114
     */
115
    public function assert(mixed $value): array
116
    {
117
        if (!is_array($value) || !array_is_list($value)) {
24✔
118
            throw AssertException::withValue($value, $this->toString());
12✔
119
        }
120

121
        /** @var Type\Type<Tv> $value_type */
122
        $value_type = $this->value_type;
12✔
123

124
        $result = [];
12✔
125

126
        $i = $v = null;
12✔
127

128
        try {
129
            /**
130
             * @var Tv $v
131
             * @var array-key $i
132
             */
133
            foreach ($value as $i => $v) {
12✔
134
                $result[] = $value_type->assert($v);
11✔
135
            }
136
        } catch (AssertException $e) {
2✔
137
            throw AssertException::withValue($v, $this->toString(), PathExpression::path($i), $e);
2✔
138
        }
139

140
        if ($result === []) {
10✔
141
            throw AssertException::withValue($value, $this->toString());
1✔
142
        }
143

144
        return $result;
9✔
145
    }
146

147
    public function toString(): string
148
    {
149
        return Str\format('non-empty-vec<%s>', $this->value_type->toString());
26✔
150
    }
151
}
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