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

Cecilapp / Cecil / 7142649554

08 Dec 2023 02:37PM UTC coverage: 83.0% (+0.5%) from 82.534%
7142649554

push

github

web-flow
8.x (#1676)

186 of 231 new or added lines in 31 files covered. (80.52%)

17 existing lines in 6 files now uncovered.

2861 of 3447 relevant lines covered (83.0%)

0.83 hits per line

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

66.1
/src/Collection/Collection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil\Collection;
15

16
/**
17
 * Class Collection.
18
 */
19
class Collection implements CollectionInterface
20
{
21
    /** @var string Collection's identifier. */
22
    protected $id;
23

24
    /** @var array Collection's items. */
25
    protected $items = [];
26

27
    public function __construct(string $id, array $items = [])
28
    {
29
        $this->setId($id);
1✔
30
        $this->items = $items;
1✔
31
    }
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function setId(string $id): BaseInterface
37
    {
38
        $this->id = $id;
1✔
39

40
        return $this;
1✔
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getId(): string
47
    {
48
        return $this->id;
1✔
49
    }
50

51
    /**
52
     * Search an item by ID.
53
     */
54
    protected function searchItem(string $id): ?array
55
    {
56
        return array_filter($this->items, function (ItemInterface $item) use ($id) {
1✔
57
            return $item->getId() == $id;
1✔
58
        });
1✔
59
    }
60

61
    /**
62
     * {@inheritdoc}
63
     *
64
     * @throws \DomainException
65
     */
66
    public function getPosition(string $id): int
67
    {
68
        $result = $this->searchItem($id);
1✔
69
        $position = key($result);
1✔
70
        if (!\is_int($position)) {
1✔
NEW
71
            throw new \DomainException(sprintf('"%s" does not exist in "%s" collection.', $id, $this->getId()));
×
72
        }
73

74
        return $position;
1✔
75
    }
76

77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function has(string $id): bool
81
    {
82
        $result = $this->searchItem($id);
1✔
83
        if (\is_array($result) && !empty($result)) {
1✔
84
            return true;
1✔
85
        }
86

87
        return false;
1✔
88
    }
89

90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @throws \DomainException
94
     */
95
    public function add(ItemInterface $item): CollectionInterface
96
    {
97
        if ($this->has($item->getId())) {
1✔
98
            throw new \DomainException(sprintf('Failed adding "%s" in "%s" collection: item already exists.', $item->getId(), $this->getId()));
1✔
99
        }
100
        $this->items[] = $item;
1✔
101

102
        return $this;
1✔
103
    }
104

105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @throws \DomainException
109
     */
110
    public function replace(string $id, ItemInterface $item): CollectionInterface
111
    {
112
        try {
113
            $this->items[$this->getPosition($id)] = $item;
1✔
NEW
114
        } catch (\DomainException $e) {
×
NEW
115
            throw new \DomainException(sprintf('Failed replacing "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
116
        }
117

118
        return $this;
1✔
119
    }
120

121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @throws \DomainException
125
     */
126
    public function remove(string $id): CollectionInterface
127
    {
128
        try {
129
            unset($this->items[$this->getPosition($id)]);
1✔
NEW
130
        } catch (\DomainException $e) {
×
UNCOV
131
            throw new \DomainException(sprintf('Failed removing "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
132
        }
133

134
        return $this;
1✔
135
    }
136

137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @throws \DomainException
141
     */
142
    public function get(string $id): ItemInterface
143
    {
144
        try {
145
            return $this->items[$this->getPosition($id)];
1✔
NEW
146
        } catch (\DomainException $e) {
×
UNCOV
147
            throw new \DomainException(sprintf('Failed getting "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
148
        }
149
    }
150

151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function keys(): array
155
    {
156
        return array_keys($this->items);
×
157
    }
158

159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function first(): ?ItemInterface
163
    {
164
        if (\count($this->items) < 1) {
1✔
165
            return null;
×
166
        }
167
        $items = $this->items;
1✔
168

169
        return array_shift($items);
1✔
170
    }
171

172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function last(): ?ItemInterface
176
    {
177
        if (\count($this->items) < 1) {
×
178
            return null;
×
179
        }
180
        $items = $this->items;
×
181

182
        return array_pop($items);
×
183
    }
184

185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function count(): int
189
    {
190
        return \count($this->items);
1✔
191
    }
192

193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function toArray(): array
197
    {
198
        return $this->items;
1✔
199
    }
200

201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function toJson(): string
205
    {
206
        return sprintf("%s\n", json_encode($this->items));
×
207
    }
208

209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getIterator(): \ArrayIterator
213
    {
214
        return new \ArrayIterator($this->items);
1✔
215
    }
216

217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function usort(\Closure $callback = null): CollectionInterface
221
    {
222
        $callback ? usort($this->items, $callback) : usort($this->items, function ($a, $b) {
1✔
223
            if ($a == $b) {
×
224
                return 0;
×
225
            }
226

227
            return ($a < $b) ? -1 : 1;
×
228
        });
1✔
229

230
        return new static($this->getId(), $this->items); /** @phpstan-ignore-line */
1✔
231
    }
232

233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function reverse(): CollectionInterface
237
    {
238
        return new static($this->getId(), array_reverse($this->items)); /** @phpstan-ignore-line */
×
239
    }
240

241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function filter(\Closure $callback): CollectionInterface
245
    {
246
        return new static($this->getId(), array_filter($this->items, $callback)); /** @phpstan-ignore-line */
1✔
247
    }
248

249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function map(\Closure $callback): CollectionInterface
253
    {
254
        return new static($this->getId(), array_map($callback, $this->items)); /** @phpstan-ignore-line */
1✔
255
    }
256

257
    /**
258
     * Implements \ArrayAccess.
259
     *
260
     * @param string $offset
261
     *
262
     * @return bool
263
     */
264
    #[\ReturnTypeWillChange]
265
    public function offsetExists($offset): bool
266
    {
267
        return $this->has((string) $offset);
1✔
268
    }
269

270
    /**
271
     * Implements \ArrayAccess.
272
     *
273
     * @param string $offset
274
     *
275
     * @return CollectionInterface|ItemInterface|null
276
     */
277
    #[\ReturnTypeWillChange]
278
    public function offsetGet($offset)
279
    {
280
        return $this->get((string) $offset);
1✔
281
    }
282

283
    /**
284
     * Implements \ArrayAccess.
285
     *
286
     * @param mixed         $offset
287
     * @param ItemInterface $value
288
     *
289
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
290
     */
291
    #[\ReturnTypeWillChange]
292
    public function offsetSet($offset, $value): void
293
    {
294
        $this->add($value);
×
295
    }
296

297
    /**
298
     * Implements \ArrayAccess.
299
     *
300
     * @param string $offset
301
     *
302
     * @return void
303
     */
304
    #[\ReturnTypeWillChange]
305
    public function offsetUnset($offset): void
306
    {
307
        $this->remove($offset);
×
308
    }
309

310
    /**
311
     * Returns the collection ID.
312
     *
313
     * @return string
314
     */
315
    public function __toString()
316
    {
317
        return $this->getId();
1✔
318
    }
319
}
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