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

Cecilapp / Cecil / 5046064611

pending completion
5046064611

push

github

GitHub
perf: native_function_invocation (#1697)

322 of 322 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

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

71.19
/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
    public function has(string $id): bool
65
    {
66
        $result = $this->searchItem($id);
1✔
67
        if (\is_array($result) && !empty($result)) {
1✔
68
            return true;
1✔
69
        }
70

71
        return false;
1✔
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws \DomainException
78
     */
79
    public function add(ItemInterface $item): CollectionInterface
80
    {
81
        if ($this->has($item->getId())) {
1✔
82
            throw new \DomainException(sprintf('Failed adding "%s" in "%s" collection: item already exists.', $item->getId(), $this->getId()));
1✔
83
        }
84
        $this->items[] = $item;
1✔
85

86
        return $this;
1✔
87
    }
88

89
    /**
90
     * {@inheritdoc}
91
     *
92
     * @throws \DomainException
93
     */
94
    public function replace(string $id, ItemInterface $item): CollectionInterface
95
    {
96
        if (!$this->has($id)) {
1✔
97
            throw new \DomainException(sprintf('Failed replacing "%s" in "%s" collection: item does not exist.', $item->getId(), $this->getId()));
×
98
        }
99
        $this->items[$this->getPosition($id)] = $item;
1✔
100

101
        return $this;
1✔
102
    }
103

104
    /**
105
     * {@inheritdoc}
106
     *
107
     * @throws \DomainException
108
     */
109
    public function remove(string $id): CollectionInterface
110
    {
111
        if (!$this->has($id)) {
1✔
112
            throw new \DomainException(sprintf('Failed removing "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
113
        }
114
        unset($this->items[$this->getPosition($id)]);
1✔
115

116
        return $this;
1✔
117
    }
118

119
    /**
120
     * {@inheritdoc}
121
     *
122
     * @throws \DomainException
123
     */
124
    public function get(string $id): ItemInterface
125
    {
126
        if (!$this->has($id)) {
1✔
127
            throw new \DomainException(sprintf('Failed getting "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
128
        }
129

130
        return $this->items[$this->getPosition($id)];
1✔
131
    }
132

133
    /**
134
     * {@inheritdoc}
135
     *
136
     * @throws \DomainException
137
     */
138
    public function getPosition(string $id): int
139
    {
140
        $result = $this->searchItem($id);
1✔
141
        $position = key($result);
1✔
142
        if (!\is_int($position)) {
1✔
143
            throw new \DomainException(sprintf('Failed getting position of "%s" in "%s" collection: item does not exist.', $id, $this->getId()));
×
144
        }
145

146
        return $position;
1✔
147
    }
148

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

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

167
        return array_shift($items);
1✔
168
    }
169

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

180
        return array_pop($items);
×
181
    }
182

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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