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

azjezz / psl / 9977367388

17 Jul 2024 03:47PM UTC coverage: 97.158% (-0.1%) from 97.271%
9977367388

push

github

web-flow
feat(collections): introduce `Set`, `SetInterface`, `MutableSet`, and `MutableSetInterface` (#482)

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

207 of 217 new or added lines in 8 files covered. (95.39%)

2 existing lines in 1 file now uncovered.

5230 of 5383 relevant lines covered (97.16%)

50.96 hits per line

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

97.44
/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
    ) {
34
    }
59✔
35

36
    /**
37
     * @throws CoercionException
38
     *
39
     * @return Collection\MutableSetInterface<T>
40
     */
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
            $iterating = true;
15✔
51
            try {
52
                /**
53
                 * @var array-key $k
54
                 * @var T $v
55
                 */
56
                foreach ($value as $k => $v) {
15✔
57
                    $iterating = false;
14✔
58
                    $v = $type->coerce($v);
14✔
59
                    $set[$v] = $v;
10✔
60
                    $iterating = true;
10✔
61
                }
62
            } catch (Throwable $e) {
7✔
63
                if ($iterating) {
7✔
64
                    throw CoercionException::withValue(null, $this->toString(), PathExpression::iteratorError($k), $e);
3✔
65
                }
66

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

70
            /** @var Collection\MutableSet<T> */
71
            return new Collection\MutableSet($set);
8✔
72
        }
73

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

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

108
                throw AssertException::withValue($v, $this->toString(), PathExpression::path($k), $e);
2✔
109
            }
110

111
            /** @var Collection\MutableSet<T> */
112
            return new Collection\MutableSet($set);
16✔
113
        }
114

115
        throw AssertException::withValue($value, $this->toString());
28✔
116
    }
117

118
    public function toString(): string
119
    {
120
        return Str\format(
46✔
121
            '%s<%s>',
46✔
122
            Collection\MutableSetInterface::class,
46✔
123
            $this->type->toString(),
46✔
124
        );
46✔
125
    }
126
}
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