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

azjezz / psl / 17432557268

03 Sep 2025 11:52AM UTC coverage: 98.446% (-0.07%) from 98.513%
17432557268

push

github

web-flow
chore: migrate from `psalm` to `mago` (#527)

232 of 241 new or added lines in 81 files covered. (96.27%)

14 existing lines in 12 files now uncovered.

5510 of 5597 relevant lines covered (98.45%)

52.23 hits per line

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

83.67
/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
 * @mago-expect analysis:mixed-assignment
24
 */
25
final readonly class NonEmptyVecType extends Type\Type
26
{
27
    /**
28
     * @psalm-mutation-free
29
     *
30
     * @param Type\TypeInterface<Tv> $value_type
31
     */
32
    public function __construct(
33
        private readonly Type\TypeInterface $value_type,
34
    ) {}
65✔
35

36
    /**
37
     * @psalm-assert-if-true non-empty-list<Tv> $value
38
     */
39
    #[\Override]
40
    public function matches(mixed $value): bool
41
    {
42
        if (!is_array($value)) {
24✔
43
            return false;
11✔
44
        }
45

46
        if ([] === $value) {
13✔
47
            return false;
1✔
48
        }
49

50
        $index = 0;
12✔
51
        foreach ($value as $k => $v) {
12✔
52
            if ($index !== $k) {
12✔
53
                return false;
1✔
54
            }
55

56
            if (!$this->value_type->matches($v)) {
11✔
57
                return false;
2✔
58
            }
59

60
            $index++;
9✔
61
        }
62

63
        return true;
9✔
64
    }
65

66
    /**
67
     * @throws CoercionException
68
     *
69
     * @return non-empty-list<Tv>
70
     */
71
    #[\Override]
72
    public function coerce(mixed $value): iterable
73
    {
74
        if (is_iterable($value)) {
17✔
75
            /** @var Type\Type<Tv> $value_type */
76
            $value_type = $this->value_type;
10✔
77

78
            /**
79
             * @var list<Tv> $entries
80
             */
81
            $result = [];
10✔
82

83
            $i = null;
10✔
84
            $v = null;
10✔
85
            /** @var bool $iterating */
86
            $iterating = true;
10✔
87

88
            try {
89
                /**
90
                 * @var Tv $v
91
                 * @var array-key $i
92
                 */
93
                foreach ($value as $i => $v) {
10✔
94
                    $iterating = false;
9✔
95
                    $result[] = $value_type->coerce($v);
9✔
96
                    $iterating = true;
9✔
97
                }
UNCOV
98
            } catch (Throwable $e) {
×
99
                throw match (true) {
UNCOV
100
                    $iterating => CoercionException::withValue(
×
101
                        null,
×
102
                        $this->toString(),
×
103
                        PathExpression::iteratorError($i),
×
104
                        $e,
×
105
                    ),
×
106
                    default => CoercionException::withValue($v, $this->toString(), PathExpression::path($i), $e),
×
107
                };
108
            }
109

110
            if ($result === []) {
10✔
111
                throw CoercionException::withValue($value, $this->toString());
1✔
112
            }
113

114
            return $result;
9✔
115
        }
116

117
        throw CoercionException::withValue($value, $this->toString());
7✔
118
    }
119

120
    /**
121
     * @throws AssertException
122
     *
123
     * @return non-empty-list<Tv>
124
     *
125
     * @psalm-assert non-empty-list<Tv> $value
126
     */
127
    #[\Override]
128
    public function assert(mixed $value): array
129
    {
130
        if (!is_array($value) || !array_is_list($value)) {
24✔
131
            throw AssertException::withValue($value, $this->toString());
12✔
132
        }
133

134
        /** @var Type\Type<Tv> $value_type */
135
        $value_type = $this->value_type;
12✔
136

137
        $result = [];
12✔
138

139
        $i = null;
12✔
140
        $v = null;
12✔
141

142
        try {
143
            /**
144
             * @var Tv $v
145
             * @var array-key $i
146
             */
147
            foreach ($value as $i => $v) {
12✔
148
                $result[] = $value_type->assert($v);
11✔
149
            }
150
        } catch (AssertException $e) {
2✔
151
            throw AssertException::withValue($v, $this->toString(), PathExpression::path($i), $e);
2✔
152
        }
153

154
        if ($result === []) {
10✔
155
            throw AssertException::withValue($value, $this->toString());
1✔
156
        }
157

158
        return $result;
9✔
159
    }
160

161
    #[\Override]
162
    public function toString(): string
163
    {
164
        return Str\format('non-empty-vec<%s>', $this->value_type->toString());
26✔
165
    }
166
}
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