• 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

97.22
/src/Psl/Type/Internal/SetType.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Type\Internal;
6

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

14
use function is_iterable;
15
use function is_object;
16

17
/**
18
 * @template T of array-key
19
 *
20
 * @extends Type\Type<Collection\SetInterface<T>>
21
 *
22
 * @internal
23
 */
24
final readonly class SetType extends Type\Type
25
{
26
    /**
27
     * @psalm-mutation-free
28
     *
29
     * @param Type\TypeInterface<T> $type
30
     */
31
    public function __construct(
32
        private readonly Type\TypeInterface $type,
33
    ) {}
52✔
34

35
    /**
36
     * @throws CoercionException
37
     *
38
     * @return Collection\SetInterface<T>
39
     */
40
    #[\Override]
41
    public function coerce(mixed $value): Collection\SetInterface
42
    {
43
        if (is_iterable($value)) {
21✔
44
            /** @var Type\Type<T> $type */
45
            $type = $this->type;
14✔
46
            /** @var array<T, T> $set */
47
            $set = [];
14✔
48
            $k = null;
14✔
49
            $v = null;
14✔
50
            /** @var bool $iterating */
51
            $iterating = true;
14✔
52
            try {
53
                /**
54
                 * @var array-key $k
55
                 * @var T $v
56
                 */
57
                foreach ($value as $k => $v) {
14✔
58
                    $iterating = false;
13✔
59
                    $v = $type->coerce($v);
13✔
60
                    $set[$v] = $v;
9✔
61
                    $iterating = true;
9✔
62
                }
63
            } catch (Throwable $e) {
7✔
64
                if ($iterating) {
7✔
65
                    throw CoercionException::withValue(null, $this->toString(), PathExpression::iteratorError($k), $e);
3✔
66
                }
67

68
                throw CoercionException::withValue($v, $this->toString(), PathExpression::path($k), $e);
4✔
69
            }
70

71
            /** @var Collection\Set<T> */
72
            return new Collection\Set($set);
7✔
73
        }
74

75
        throw CoercionException::withValue($value, $this->toString());
7✔
76
    }
77

78
    /**
79
     * @throws AssertException
80
     *
81
     * @return Collection\SetInterface<T>
82
     *
83
     * @psalm-assert Collection\SetInterface<T> $value
84
     */
85
    #[\Override]
86
    public function assert(mixed $value): Collection\SetInterface
87
    {
88
        if (is_object($value) && $value instanceof Collection\SetInterface) {
40✔
89
            /** @var Type\Type<T> $type */
90
            $type = $this->type;
16✔
91
            /** @var array<T, T> $set */
92
            $set = [];
16✔
93
            $v = null;
16✔
94
            $k = null;
16✔
95
            /** @var bool $iterating */
96
            $iterating = true;
16✔
97
            try {
98
                /**
99
                 * @var T $v
100
                 */
101
                foreach ($value as $k => $v) {
16✔
102
                    $iterating = false;
16✔
103
                    $v = $type->assert($v);
16✔
104
                    $set[$v] = $v;
14✔
105
                    $iterating = true;
14✔
106
                }
107
            } catch (Throwable $e) {
2✔
108
                if ($iterating) {
2✔
UNCOV
109
                    throw AssertException::withValue(null, $this->toString(), PathExpression::iteratorError($k), $e);
×
110
                }
111

112
                throw AssertException::withValue($v, $this->toString(), PathExpression::path($k), $e);
2✔
113
            }
114

115
            /** @var Collection\Set<T> */
116
            return new Collection\Set($set);
14✔
117
        }
118

119
        throw AssertException::withValue($value, $this->toString());
24✔
120
    }
121

122
    #[\Override]
123
    public function toString(): string
124
    {
125
        return Str\format('%s<%s>', Collection\SetInterface::class, $this->type->toString());
42✔
126
    }
127
}
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