• 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/MutableSetType.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\MutableSetInterface<T>>
21
 *
22
 * @internal
23
 */
24
final readonly class MutableSetType 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
    ) {}
59✔
34

35
    /**
36
     * @throws CoercionException
37
     *
38
     * @return Collection\MutableSetInterface<T>
39
     */
40
    #[\Override]
41
    public function coerce(mixed $value): Collection\MutableSetInterface
42
    {
43
        if (is_iterable($value)) {
22✔
44
            /** @var Type\Type<T> $type */
45
            $type = $this->type;
15✔
46
            /** @var array<T, T> $set */
47
            $set = [];
15✔
48
            $k = null;
15✔
49
            $v = null;
15✔
50
            /** @var bool $iterating */
51
            $iterating = true;
15✔
52
            try {
53
                /**
54
                 * @var array-key $k
55
                 * @var T $v
56
                 */
57
                foreach ($value as $k => $v) {
15✔
58
                    $iterating = false;
14✔
59
                    $v = $type->coerce($v);
14✔
60
                    $set[$v] = $v;
10✔
61
                    $iterating = true;
10✔
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\MutableSet<T> */
72
            return new Collection\MutableSet($set);
8✔
73
        }
74

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

78
    /**
79
     * @throws AssertException
80
     *
81
     * @return Collection\MutableSetInterface<T>
82
     *
83
     * @psalm-assert Collection\MutableSetInterface<T> $value
84
     */
85
    #[\Override]
86
    public function assert(mixed $value): Collection\MutableSetInterface
87
    {
88
        if (is_object($value) && $value instanceof Collection\MutableSetInterface) {
46✔
89
            /** @var Type\Type<T> $type */
90
            $type = $this->type;
18✔
91
            /** @var array<T, T> $set */
92
            $set = [];
18✔
93
            $k = null;
18✔
94
            $v = null;
18✔
95
            /** @var bool $iterating */
96
            $iterating = true;
18✔
97
            try {
98
                /**
99
                 * @var T $v
100
                 */
101
                foreach ($value as $k => $v) {
18✔
102
                    $iterating = false;
18✔
103
                    $v = $type->assert($v);
18✔
104
                    $set[$v] = $v;
16✔
105
                    $iterating = true;
16✔
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\MutableSet<T> */
116
            return new Collection\MutableSet($set);
16✔
117
        }
118

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

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