Complex classes like HasManyRelation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HasManyRelation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class HasManyRelation extends AbstractRelation implements \IteratorAggregate |
||
30 | { |
||
31 | use MatchTrait; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $autoload = true; |
||
37 | |||
38 | /** |
||
39 | * Loaded list of records. |
||
40 | * |
||
41 | * @var RecordInterface[] |
||
42 | */ |
||
43 | private $instances = []; |
||
44 | |||
45 | /** |
||
46 | * Records deleted from list. Potentially pre-schedule command? |
||
47 | * |
||
48 | * @var RecordInterface[] |
||
49 | */ |
||
50 | private $deletedInstances = []; |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function withContext( |
||
72 | |||
73 | /** |
||
74 | * Partial selections will not be autoloaded. |
||
75 | * |
||
76 | * Example: |
||
77 | * |
||
78 | * $post = $this->findPost(); //no comments |
||
79 | * $post->comments->partial(true); |
||
80 | * assert($post->comments->count() == 0); //never loaded |
||
81 | * |
||
82 | * $post->comments->add($comment); |
||
83 | * |
||
84 | * @param bool $partial |
||
85 | * |
||
86 | * @return HasManyRelation |
||
87 | */ |
||
88 | public function partial(bool $partial = true): self |
||
94 | |||
95 | /** |
||
96 | * @return bool |
||
97 | */ |
||
98 | public function isPartial(): bool |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | * |
||
106 | * @param bool $force When true all existed records will be loaded and removed. |
||
107 | * |
||
108 | * @throws RelationException |
||
109 | */ |
||
110 | public function setRelated($value, bool $force = false) |
||
137 | |||
138 | /** |
||
139 | * Has many relation represent itself (see getIterator method). |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function getRelated() |
||
147 | |||
148 | /** |
||
149 | * Iterate over instance set. |
||
150 | * |
||
151 | * @return \ArrayIterator |
||
152 | */ |
||
153 | public function getIterator() |
||
157 | |||
158 | /** |
||
159 | * Iterate over deleted instances. |
||
160 | * |
||
161 | * @return \ArrayIterator |
||
162 | */ |
||
163 | public function getDeleted() |
||
167 | |||
168 | /** |
||
169 | * Add new record into entity set. Attention, usage of this method WILL make relation partial. |
||
170 | * |
||
171 | * @param RecordInterface $record |
||
172 | * |
||
173 | * @throws RelationException |
||
174 | */ |
||
175 | public function add(RecordInterface $record) |
||
182 | |||
183 | /** |
||
184 | * Method will autoload data. |
||
185 | * |
||
186 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function has($query): bool |
||
194 | |||
195 | /** |
||
196 | * Delete one record, strict compaction, make sure exactly same instance is given. |
||
197 | * |
||
198 | * @param RecordInterface $record |
||
199 | */ |
||
200 | public function delete(RecordInterface $record) |
||
204 | |||
205 | /** |
||
206 | * Delete multiple records, strict compaction, make sure exactly same instance is given. Method |
||
207 | * would not autoload instance and will mark it as partial. |
||
208 | * |
||
209 | * @param array|\Traversable $records |
||
210 | */ |
||
211 | public function deleteMultiple($records) |
||
231 | |||
232 | /** |
||
233 | * Fine one entity for a given query or return null. Method will autoload data. |
||
234 | * |
||
235 | * Example: ->matchOne(['value' => 'something', ...]); |
||
236 | * |
||
237 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
238 | * |
||
239 | * @return RecordInterface|null |
||
240 | */ |
||
241 | public function matchOne($query) |
||
251 | |||
252 | /** |
||
253 | * Return only instances matched given query, performed in memory! Only simple conditions are |
||
254 | * allowed. Not "find" due trademark violation. Method will autoload data. |
||
255 | * |
||
256 | * Example: ->matchMultiple(['value' => 'something', ...]); |
||
257 | * |
||
258 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
259 | * |
||
260 | * @return \ArrayIterator |
||
261 | */ |
||
262 | public function matchMultiple($query) |
||
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function queueCommands(ContextualCommandInterface $command): CommandInterface |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | * |
||
307 | * @return self |
||
308 | * |
||
309 | * @throws SelectorException |
||
310 | * @throws QueryException (needs wrapping) |
||
311 | */ |
||
312 | protected function loadData(bool $autoload = true): self |
||
331 | |||
332 | /** |
||
333 | * Init pre-loaded data. |
||
334 | * |
||
335 | * @return HasManyRelation |
||
336 | */ |
||
337 | private function initInstances(): self |
||
360 | |||
361 | /** |
||
362 | * @param ContextualCommandInterface $command |
||
363 | * @param RecordInterface $instance |
||
364 | * |
||
365 | * @return CommandInterface |
||
366 | */ |
||
367 | private function queueRelated( |
||
401 | |||
402 | /** |
||
403 | * Fetch data from database. Lazy load. |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | protected function loadRelated(): array |
||
419 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..