Issues (590)

src/Collection/Indexer/EntityIndexer.php (2 issues)

1
<?php
2
3
namespace Bdf\Prime\Collection\Indexer;
4
5
use Bdf\Prime\Mapper\Mapper;
6
7
/**
8
 * Base implementation of EntityIndexer
9
 *
10
 * @template E as object
11
 * @implements EntityIndexerInterface<E>
12
 */
13
final class EntityIndexer implements EntityIndexerInterface
14
{
15
    /**
16
     * @var Mapper<E>
17
     */
18
    private $mapper;
19
20
    /**
21
     * All indexed entities
22
     *
23
     * @var E[]
24
     */
25
    private $entities = [];
26
27
    /**
28
     * Map of indexes
29
     * Indexes are indexed by the key name, and store entities in mode "group by combine"
30
     *
31
     * @var E[][][]
32
     */
33
    private $indexed = [];
34
35
36
    /**
37
     * EntityIndexer constructor.
38
     *
39
     * @param Mapper<E> $mapper The entity mapper. Used for extract attribute value
40
     * @param list<string> $indexes List of initial indexes keys to use. Entities will be indexed with theses keys when pushed
0 ignored issues
show
The type Bdf\Prime\Collection\Indexer\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42 474
    public function __construct(Mapper $mapper, $indexes = [])
43
    {
44 474
        $this->mapper = $mapper;
45 474
        $this->indexed = array_fill_keys($indexes, []);
0 ignored issues
show
It seems like $indexes can also be of type Bdf\Prime\Collection\Indexer\list; however, parameter $keys of array_fill_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $this->indexed = array_fill_keys(/** @scrutinizer ignore-type */ $indexes, []);
Loading history...
46
    }
47
48
    /**
49
     * Push the entity to the indexer
50
     * Active indexes will be updated
51
     *
52
     * @param E $entity Entity to add
53
     *
54
     * @return void
55
     */
56 457
    public function push($entity): void
57
    {
58 457
        $this->entities[] = $entity;
59
60 457
        foreach ($this->indexed as $key => &$indexed) {
61 237
            $indexed[$this->mapper->extractOne($entity, $key)][] = $entity;
62
        }
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 250
    public function by(string $key): array
69
    {
70 250
        if (isset($this->indexed[$key])) {
71 247
            return $this->indexed[$key];
72
        }
73
74 192
        $indexed = [];
75
76 192
        foreach ($this->entities as $entity) {
77 190
            $indexed[$this->mapper->extractOne($entity, $key)][] = $entity;
78
        }
79
80 192
        return $this->indexed[$key] = $indexed;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 215
    public function byOverride(string $key): array
87
    {
88 215
        $result = [];
89
90 215
        foreach ($this->by($key) as $key => $value) {
91 206
            $result[$key] = end($value);
92
        }
93
94 215
        return $result;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 388
    public function all(): array
101
    {
102 388
        return $this->entities;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 194
    public function empty(): bool
109
    {
110 194
        return empty($this->entities);
111
    }
112
113
    /**
114
     * Create an indexer with list of entities
115
     *
116
     * @param Mapper<T> $mapper
117
     * @param T[] $entities
118
     *
119
     * @return EntityIndexer<T>
120
     *
121
     * @template T as object
122
     */
123 142
    public static function fromArray(Mapper $mapper, array $entities): self
124
    {
125 142
        $indexer = new EntityIndexer($mapper);
126 142
        $indexer->entities = $entities;
127
128 142
        return $indexer;
129
    }
130
}
131