Complex classes like Relation 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 Relation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class Relation extends Component implements |
||
29 | RelationInterface, |
||
30 | \Countable, |
||
31 | \IteratorAggregate, |
||
32 | \JsonSerializable |
||
33 | { |
||
34 | /** |
||
35 | * {@} table aliases. |
||
36 | */ |
||
37 | use AliasTrait; |
||
38 | |||
39 | /** |
||
40 | * Relation type, required to fetch record class from relation definition. |
||
41 | */ |
||
42 | const RELATION_TYPE = null; |
||
43 | |||
44 | /** |
||
45 | * Indication that relation represent multiple records (HAS_MANY relations). |
||
46 | */ |
||
47 | const MULTIPLE = false; |
||
48 | |||
49 | /** |
||
50 | * Indication that relation data has been loaded from databases. |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $loaded = false; |
||
55 | |||
56 | /** |
||
57 | * Pre-loaded relation data, can be loaded while parent record, or later. Real data instance |
||
58 | * will be constructed on demand and will keep it pre-loaded context between calls. |
||
59 | * |
||
60 | * @see Record::setContext() |
||
61 | * @var array|null |
||
62 | */ |
||
63 | protected $data = []; |
||
64 | |||
65 | /** |
||
66 | * Instance of constructed EntityInterface of RecordIterator. |
||
67 | * |
||
68 | * @invisible |
||
69 | * @var mixed|EntityInterface|RecordIterator |
||
70 | */ |
||
71 | protected $instance = null; |
||
72 | |||
73 | /** |
||
74 | * Parent Record caused relation to be created. |
||
75 | * |
||
76 | * @var RecordInterface |
||
77 | */ |
||
78 | protected $parent = null; |
||
79 | |||
80 | /** |
||
81 | * Relation definition fetched from ORM schema. Must already be normalized by RelationSchema. |
||
82 | * |
||
83 | * @invisible |
||
84 | * @var array |
||
85 | */ |
||
86 | protected $definition = []; |
||
87 | |||
88 | /** |
||
89 | * @invisible |
||
90 | * @var ORM |
||
91 | */ |
||
92 | protected $orm = null; |
||
93 | |||
94 | /** |
||
95 | * @param ORM $orm |
||
96 | * @param RecordInterface $parent |
||
97 | * @param array $definition Relation definition, must be normalized by relation |
||
98 | * schema. |
||
99 | * @param mixed $data Pre-loaded relation data. |
||
100 | * @param bool $loaded Indication that relation data has been loaded. |
||
101 | */ |
||
102 | public function __construct( |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function isLoaded() |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | * |
||
127 | * Relation will automatically create related record if relation is not nullable. Usually |
||
128 | * applied for has one relations ($user->profile). |
||
129 | */ |
||
130 | public function getRelated() |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function associate(EntityInterface $related = null) |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | public function saveAssociation($validate = true) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function reset(array $data = [], $loaded = false) |
||
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | public function isValid() |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function hasErrors() |
||
274 | |||
275 | /** |
||
276 | * List of errors associated with parent field, every field must have only one error assigned. |
||
277 | * |
||
278 | * @param bool $reset Clean errors after receiving every message. |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getErrors($reset = false) |
||
305 | |||
306 | /** |
||
307 | * Get selector associated with relation. |
||
308 | * |
||
309 | * @param array $where |
||
310 | * @return RecordSelector |
||
311 | */ |
||
312 | public function find(array $where = []) |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * |
||
320 | * Use getRelation() method to count pre-loaded data. |
||
321 | * |
||
322 | * @return int |
||
323 | */ |
||
324 | public function count() |
||
328 | |||
329 | /** |
||
330 | * Perform iterator on pre-loaded data. Use relation selector to iterate thought custom relation |
||
331 | * query. |
||
332 | * |
||
333 | * @return RecordEntity|RecordEntity[]|RecordIterator |
||
334 | */ |
||
335 | public function getIterator() |
||
339 | |||
340 | /** |
||
341 | * Bypassing call to created selector. |
||
342 | * |
||
343 | * @param string $method |
||
344 | * @param array $arguments |
||
345 | * @return mixed |
||
346 | */ |
||
347 | public function __call($method, array $arguments) |
||
351 | |||
352 | /** |
||
353 | * {@inheritdoc} |
||
354 | */ |
||
355 | public function jsonSerialize() |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | protected function container() |
||
367 | |||
368 | /** |
||
369 | * Class name of outer record. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | protected function getClass() |
||
377 | |||
378 | /** |
||
379 | * Mount relation keys to parent or children records to ensure their connection. Method called |
||
380 | * when record requests relation save. |
||
381 | * |
||
382 | * @param EntityInterface $record |
||
383 | * @return EntityInterface |
||
384 | */ |
||
385 | abstract protected function mountRelation(EntityInterface $record); |
||
386 | |||
387 | /** |
||
388 | * Convert pre-loaded relation data to record iterator record. |
||
389 | * |
||
390 | * @return RecordIterator |
||
391 | */ |
||
392 | protected function createIterator() |
||
396 | |||
397 | /** |
||
398 | * Convert pre-loaded relation data to active record record. |
||
399 | * |
||
400 | * @return RecordEntity |
||
401 | */ |
||
402 | protected function createRecord() |
||
406 | |||
407 | /** |
||
408 | * Create empty record to be associated with non nullable relation. |
||
409 | * |
||
410 | * @return RecordEntity |
||
411 | */ |
||
412 | protected function emptyRecord() |
||
419 | |||
420 | /** |
||
421 | * Load relation data based on created selector. |
||
422 | * |
||
423 | * @return array|null |
||
424 | */ |
||
425 | protected function loadData() |
||
444 | |||
445 | /** |
||
446 | * Internal ORM relation method used to create valid selector used to pre-load relation data or |
||
447 | * create custom query based on relation options. |
||
448 | * |
||
449 | * Must be redeclarated in child implementations. |
||
450 | * |
||
451 | * @return RecordSelector |
||
452 | */ |
||
453 | protected function createSelector() |
||
457 | |||
458 | /** |
||
459 | * Save simple related entity. |
||
460 | * |
||
461 | * @param EntityInterface $entity |
||
462 | * @param bool $validate |
||
463 | * @return bool|void |
||
464 | */ |
||
465 | private function saveEntity(EntityInterface $entity, $validate) |
||
486 | } |
||
487 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.