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

smartemailing / types / 38097019585

11 Oct 2026 12:01AM UTC coverage: 93.707% (+1.1%) from 92.657%
38097019585

push

github

web-flow
Merge pull request #151 from smartemailing/m_invalid_type_public_message

InvalidTypeException: add public message and error details (getPublic…

79 of 80 new or added lines in 3 files covered. (98.75%)

2 existing lines in 2 files now uncovered.

1370 of 1462 relevant lines covered (93.71%)

0.94 hits per line

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

95.74
/src/Helpers/UniqueToStringArray.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace SmartEmailing\Types\Helpers;
6

7
use SmartEmailing\Types\ExtractableTraits\ArrayExtractableTrait;
8
use SmartEmailing\Types\InvalidTypeException;
9
use SmartEmailing\Types\ToStringInterface;
10
use SmartEmailing\Types\UniqueArrayFeatures;
11

12
/*
13
 * This is not type, just helper for easier work with
14
 * unique arrays of \SmartEmailing\Types\ToStringInterface.
15
 */
16

17
/**
18
 * @implements \IteratorAggregate<\SmartEmailing\Types\ToStringInterface>
19
 */
20
final class UniqueToStringArray implements \Countable, \IteratorAggregate
21
{
22

23
        use UniqueArrayFeatures;
24
        use ArrayExtractableTrait;
25

26
        /**
27
         * @var array<\SmartEmailing\Types\ToStringInterface>
28
         */
29
        private array $objects;
30

31
        private ?string $type = null;
32

33
        /**
34
         * @param array<\SmartEmailing\Types\ToStringInterface|mixed> $data
35
         * @throws \SmartEmailing\Types\InvalidTypeException
36
         */
37
        private function __construct(
38
                array $data = []
39
        )
40
        {
41
                $this->objects = [];
1 ✔
42

43
                foreach ($data as $value) {
1 ✔
44
                        if (!$value instanceof ToStringInterface) {
1 ✔
45
                                throw InvalidTypeException::typeError(
1 ✔
46
                                        'all members of array must implement ' . ToStringInterface::class,
1 ✔
47
                                        $value
1 ✔
48
                                );
49
                        }
50

51
                        $this->add($value);
1 ✔
52
                }
53
        }
1 ✔
54

55
        /**
56
         * @param array<\SmartEmailing\Types\Helpers\UniqueToStringArray> $uniqueToStringArrays
57
         * @return \SmartEmailing\Types\Helpers\UniqueToStringArray
58
         */
59
        public static function intersect(
60
                array $uniqueToStringArrays
61
        ): self {
62
                if (\count($uniqueToStringArrays) === 1) {
1 ✔
63
                        return \reset($uniqueToStringArrays);
×
64
                }
65

66
                $plainIntArrays = [];
1 ✔
67

68
                foreach ($uniqueToStringArrays as $uniqueToStringArray) {
1 ✔
69
                        $plainIntArrays[] = $uniqueToStringArray->objects;
1 ✔
70
                }
71

72
                $result = \array_intersect_key(
1 ✔
73
                        ...$plainIntArrays
1 ✔
74
                );
75

76
                $output = new UniqueToStringArray([]);
1 ✔
77
                $output->objects = $result;
1 ✔
78

79
                return $output;
1 ✔
80
        }
81

82
        /**
83
         * @param array<\SmartEmailing\Types\Helpers\UniqueToStringArray> $uniqueIntArrays
84
         * @return \SmartEmailing\Types\Helpers\UniqueToStringArray
85
         */
86
        public static function union(
87
                array $uniqueIntArrays
88
        ): self {
89
                $result = [];
1 ✔
90

91
                foreach ($uniqueIntArrays as $uniqueIntArray) {
1 ✔
92
                        foreach ($uniqueIntArray->objects as $key => $object) {
1 ✔
93
                                $result[$key] = $object;
1 ✔
94
                        }
95
                }
96

97
                $output = new self([]);
1 ✔
98
                $output->objects = $result;
1 ✔
99

100
                return $output;
1 ✔
101
        }
102

103
        /**
104
         * @return \Traversable<\SmartEmailing\Types\ToStringInterface>
105
         */
106
        public function getIterator(): \Traversable
107
        {
108
                return new \RecursiveArrayIterator($this->getValues());
1 ✔
109
        }
110

111
        public function count(): int
112
        {
113
                return \count($this->objects);
1 ✔
114
        }
115

116
        /**
117
         * @return array<\SmartEmailing\Types\ToStringInterface>
118
         */
119
        public function getValues(): array
120
        {
121
                return \array_values($this->objects);
1 ✔
122
        }
123

124
        /**
125
         * @return array<\SmartEmailing\Types\ToStringInterface>
126
         */
127
        public function toArray(): array
128
        {
129
                return $this->getValues();
1 ✔
130
        }
131

132
        public function add(
133
                ToStringInterface $valueObject
134
        ): bool {
135
                $type = $valueObject::class;
1 ✔
136

137
                if ($this->type === null) {
1 ✔
138
                        $this->type = $type;
1 ✔
139
                }
140

141
                if ($this->type !== $type) {
1 ✔
142
                        throw InvalidTypeException::typeError(
1 ✔
143
                                'all members of array must be of type ' . $this->type,
1 ✔
UNCOV
144
                                $valueObject
×
145
                        );
146
                }
147

148
                $key = $valueObject->__toString();
1 ✔
149

150
                if (!isset($this->objects[$key])) {
1 ✔
151
                        $this->objects[$key] = $valueObject;
1 ✔
152

153
                        return true;
1 ✔
154
                }
155

156
                return false;
1 ✔
157
        }
158

159
        public function remove(
160
                ToStringInterface $valueObject
161
        ): void {
162
                $key = $valueObject->__toString();
1 ✔
163
                unset($this->objects[$key]);
1 ✔
164
        }
1 ✔
165

166
        public function isEmpty(): bool
167
        {
168
                return $this->objects === [];
1 ✔
169
        }
170

171
        public function contains(
172
                ToStringInterface $valueObject
173
        ): bool {
174
                $key = $valueObject->__toString();
1 ✔
175

176
                return isset($this->objects[$key]);
1 ✔
177
        }
178

179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc