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

azjezz / psl / 12713643030

10 Jan 2025 04:47PM UTC coverage: 98.483% (-0.2%) from 98.691%
12713643030

push

github

web-flow
chore: switch to mago (#501)

* chore: switch from php-cs-fixer and phpcs to mago

Signed-off-by: azjezz <azjezz@protonmail.com>

682 of 699 new or added lines in 176 files covered. (97.57%)

3 existing lines in 2 files now uncovered.

5322 of 5404 relevant lines covered (98.48%)

50.89 hits per line

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

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

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

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

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

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

58
            $index++;
9✔
59
        }
60

61
        return true;
9✔
62
    }
63

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

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

80
            $i = $v = null;
10✔
81
            $iterating = true;
10✔
82

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

105
            if ($result === []) {
10✔
106
                throw CoercionException::withValue($value, $this->toString());
1✔
107
            }
108

109
            return $result;
9✔
110
        }
111

112
        throw CoercionException::withValue($value, $this->toString());
7✔
113
    }
114

115
    /**
116
     * @throws AssertException
117
     *
118
     * @return non-empty-list<Tv>
119
     *
120
     * @psalm-assert non-empty-list<Tv> $value
121
     */
122
    public function assert(mixed $value): array
123
    {
124
        if (!is_array($value) || !array_is_list($value)) {
24✔
125
            throw AssertException::withValue($value, $this->toString());
12✔
126
        }
127

128
        /** @var Type\Type<Tv> $value_type */
129
        $value_type = $this->value_type;
12✔
130

131
        $result = [];
12✔
132

133
        $i = $v = null;
12✔
134

135
        try {
136
            /**
137
             * @var Tv $v
138
             * @var array-key $i
139
             */
140
            foreach ($value as $i => $v) {
12✔
141
                $result[] = $value_type->assert($v);
11✔
142
            }
143
        } catch (AssertException $e) {
2✔
144
            throw AssertException::withValue($v, $this->toString(), PathExpression::path($i), $e);
2✔
145
        }
146

147
        if ($result === []) {
10✔
148
            throw AssertException::withValue($value, $this->toString());
1✔
149
        }
150

151
        return $result;
9✔
152
    }
153

154
    public function toString(): string
155
    {
156
        return Str\format('non-empty-vec<%s>', $this->value_type->toString());
26✔
157
    }
158
}
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