1 | <?php |
||
22 | class ManyToManyRelation extends AbstractRelation implements \IteratorAggregate, \Countable |
||
23 | { |
||
24 | use MatchTrait, PartialTrait; |
||
25 | |||
26 | /** |
||
27 | * @var \SplObjectStorage |
||
28 | */ |
||
29 | private $pivotData; |
||
30 | |||
31 | /** |
||
32 | * Linked records. |
||
33 | * |
||
34 | * @var RecordInterface[] |
||
35 | */ |
||
36 | private $linked = []; |
||
37 | |||
38 | /** |
||
39 | * Record which pivot data was updated, record must still present in linked array. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $updated = []; |
||
44 | |||
45 | /** |
||
46 | * Records scheduled to be de-associated. |
||
47 | * |
||
48 | * @var RecordInterface[] |
||
49 | */ |
||
50 | private $unlinked = []; |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function hasRelated(): bool |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function withContext( |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function setRelated($value) |
||
95 | |||
96 | /** |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function getRelated() |
||
103 | |||
104 | /** |
||
105 | * Iterate over linked instances, will force pre-loading unless partial. |
||
106 | * |
||
107 | * @return \ArrayIterator |
||
108 | */ |
||
109 | public function getIterator() |
||
113 | |||
114 | /** |
||
115 | * @return int |
||
116 | */ |
||
117 | public function count() |
||
121 | |||
122 | /** |
||
123 | * Get all unlinked records. |
||
124 | * |
||
125 | * @return \ArrayIterator |
||
126 | */ |
||
127 | public function getUnlinked() |
||
131 | |||
132 | /** |
||
133 | * Get pivot data associated with specific instance. |
||
134 | * |
||
135 | * @param RecordInterface $record |
||
136 | * |
||
137 | * @return array |
||
138 | * |
||
139 | * @throws RelationException |
||
140 | */ |
||
141 | public function getPivot(RecordInterface $record): array |
||
149 | |||
150 | /** |
||
151 | * Link record with parent entity. Only record instances is accepted. |
||
152 | * |
||
153 | * @param RecordInterface $record |
||
154 | * @param array $pivotData |
||
155 | * |
||
156 | * @return self |
||
157 | * |
||
158 | * @throws RelationException |
||
159 | */ |
||
160 | public function link(RecordInterface $record, array $pivotData = []): self |
||
182 | |||
183 | /** |
||
184 | * Unlink specific entity from relation. |
||
185 | * |
||
186 | * @param RecordInterface $record |
||
187 | * |
||
188 | * @return self |
||
189 | * |
||
190 | * @throws RelationException When entity not linked. |
||
191 | */ |
||
192 | public function unlink(RecordInterface $record): self |
||
207 | |||
208 | /** |
||
209 | * Check if given query points to linked entity. |
||
210 | * |
||
211 | * Example: |
||
212 | * echo $post->tags->has(1); |
||
213 | * echo $post->tags->has(['name'=>'tag a']); |
||
214 | * |
||
215 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function has($query) |
||
223 | |||
224 | /** |
||
225 | * Fine one entity for a given query or return null. Method will autoload data. |
||
226 | * |
||
227 | * Example: ->matchOne(['value' => 'something', ...]); |
||
228 | * |
||
229 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
230 | * |
||
231 | * @return RecordInterface|null |
||
232 | */ |
||
233 | public function matchOne($query) |
||
243 | |||
244 | /** |
||
245 | * Return only instances matched given query, performed in memory! Only simple conditions are |
||
246 | * allowed. Not "find" due trademark violation. Method will autoload data. |
||
247 | * |
||
248 | * Example: ->matchMultiple(['value' => 'something', ...]); |
||
249 | * |
||
250 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
251 | * |
||
252 | * @return \ArrayIterator |
||
253 | */ |
||
254 | public function matchMultiple($query) |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function queueCommands(ContextualCommandInterface $command): CommandInterface |
||
273 | |||
274 | /** |
||
275 | * Load related records from database. |
||
276 | * |
||
277 | * @param bool $autoload |
||
278 | * |
||
279 | * @return self |
||
280 | * |
||
281 | * @throws SelectorException |
||
282 | * @throws QueryException (needs wrapping) |
||
283 | */ |
||
284 | protected function loadData(bool $autoload = true): self |
||
303 | |||
304 | /** |
||
305 | * Fetch data from database. Lazy load. |
||
306 | * |
||
307 | * @return array |
||
308 | */ |
||
309 | protected function loadRelated(): array |
||
317 | |||
318 | /** |
||
319 | * Init relations and populate pivot map. |
||
320 | * |
||
321 | * @return ManyToManyRelation |
||
322 | */ |
||
323 | private function initInstances(): self |
||
345 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.