CollectionTrait::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace jschreuder\MiddleAuth\Util;
4
5
use Iterator;
6
7
/**
8
 * Trait for creating type-safe collections of entities.
9
 *
10
 * Implementing classes must:
11
 * 1. Store entities in a private array property named $collection
12
 * 2. Implement a constructor that accepts variadic entities and assigns them to $this->collection
13
 *
14
 * Example implementation:
15
 * ```
16
 * final class UserCollection implements IteratorAggregate
17
 * {
18
 *     use CollectionTrait;
19
 *
20
 *     public function __construct(
21
 *         private array $collection = [],
22
 *     ) {}
23
 * }
24
 * ```
25
 */
26
trait CollectionTrait
27
{
28
    private array $collection;
29
30 34
    public function getIterator(): Iterator
31
    {
32 34
        foreach ($this->collection as $item) {
33 25
            yield $item;
34
        }
35
    }
36
37 39
    public function count(): int
38
    {
39 39
        return count($this->collection);
40
    }
41
42 11
    public function isEmpty(): bool
43
    {
44 11
        return empty($this->collection);
45
    }
46
47 3
    public function toArray(): array
48
    {
49 3
        return $this->collection;
50
    }
51
}
52