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 |
||
30 | class HasManyRelation extends AbstractRelation implements \IteratorAggregate, \Countable |
||
31 | { |
||
32 | use MatchTrait, PartialTrait; |
||
33 | |||
34 | /** |
||
35 | * Loaded list of records. SplObjectStorage? |
||
36 | * |
||
37 | * @var RecordInterface[] |
||
38 | */ |
||
39 | private $instances = []; |
||
40 | |||
41 | /** |
||
42 | * Records deleted from list. Potentially pre-schedule command? |
||
43 | * |
||
44 | * @var RecordInterface[] |
||
45 | */ |
||
46 | private $deletedInstances = []; |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | public function hasRelated(): bool |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function withContext( |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | * |
||
85 | * @throws RelationException |
||
86 | */ |
||
87 | public function setRelated($value) |
||
113 | |||
114 | /** |
||
115 | * Has many relation represent itself (see getIterator method). |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function getRelated() |
||
123 | |||
124 | /** |
||
125 | * Iterate over instance set. |
||
126 | * |
||
127 | * @return \ArrayIterator |
||
128 | */ |
||
129 | public function getIterator() |
||
133 | |||
134 | /** |
||
135 | * @return int |
||
136 | */ |
||
137 | public function count() |
||
141 | |||
142 | /** |
||
143 | * Iterate over deleted instances. |
||
144 | * |
||
145 | * @return \ArrayIterator |
||
146 | */ |
||
147 | public function getDeleted() |
||
151 | |||
152 | /** |
||
153 | * Method will autoload data. |
||
154 | * |
||
155 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function has($query): bool |
||
163 | |||
164 | /** |
||
165 | * Add new record into entity set. Attention, usage of this method WILL load relation data |
||
166 | * unless partial. |
||
167 | * |
||
168 | * @param RecordInterface $record |
||
169 | * |
||
170 | * @return self |
||
171 | * |
||
172 | * @throws RelationException |
||
173 | */ |
||
174 | public function add(RecordInterface $record): self |
||
181 | |||
182 | /** |
||
183 | * Delete one record, strict compaction, make sure exactly same instance is given. |
||
184 | * |
||
185 | * @param RecordInterface $record |
||
186 | * |
||
187 | * @return self |
||
188 | * |
||
189 | * @throws RelationException |
||
190 | */ |
||
191 | public function delete(RecordInterface $record): self |
||
209 | |||
210 | /** |
||
211 | * Fine one entity for a given query or return null. Method will autoload data. |
||
212 | * |
||
213 | * Example: ->matchOne(['value' => 'something', ...]); |
||
214 | * |
||
215 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
216 | * |
||
217 | * @return RecordInterface|null |
||
218 | */ |
||
219 | public function matchOne($query) |
||
229 | |||
230 | /** |
||
231 | * Return only instances matched given query, performed in memory! Only simple conditions are |
||
232 | * allowed. Not "find" due trademark violation. Method will autoload data. |
||
233 | * |
||
234 | * Example: ->matchMultiple(['value' => 'something', ...]); |
||
235 | * |
||
236 | * @param array|RecordInterface|mixed $query Fields, entity or PK. |
||
237 | * |
||
238 | * @return \ArrayIterator |
||
239 | */ |
||
240 | public function matchMultiple($query) |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function queueCommands(ContextualCommandInterface $command): CommandInterface |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | * |
||
285 | * @return self |
||
286 | * |
||
287 | * @throws SelectorException |
||
288 | * @throws QueryException (needs wrapping) |
||
289 | */ |
||
290 | protected function loadData(bool $autoload = true): self |
||
309 | |||
310 | /** |
||
311 | * Fetch data from database. Lazy load. |
||
312 | * |
||
313 | * @return array |
||
314 | */ |
||
315 | protected function loadRelated(): array |
||
327 | |||
328 | /** |
||
329 | * Init pre-loaded data. |
||
330 | * |
||
331 | * @return HasManyRelation |
||
332 | */ |
||
333 | private function initInstances(): self |
||
354 | |||
355 | /** |
||
356 | * @param ContextualCommandInterface $command |
||
357 | * @param RecordInterface $instance |
||
358 | * |
||
359 | * @return CommandInterface |
||
360 | */ |
||
361 | private function queueRelated( |
||
388 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.