Complex classes like Row 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 Row, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Row extends AbstractRow |
||
11 | { |
||
12 | private $values = []; |
||
13 | private $relations = []; |
||
14 | private $changed = false; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | public function __construct(Table $table) |
||
31 | |||
32 | /** |
||
33 | * Clear the current cache. |
||
34 | */ |
||
35 | public function clearCache() |
||
39 | |||
40 | /** |
||
41 | * Initialize the row with the data from database. |
||
42 | * |
||
43 | * @param array $values |
||
44 | * @param array $relations |
||
45 | */ |
||
46 | public function init(array $values, array $relations = []) |
||
52 | |||
53 | /** |
||
54 | * Magic method to return properties or load them automatically. |
||
55 | * |
||
56 | * @param string $name |
||
57 | */ |
||
58 | public function __get($name) |
||
79 | |||
80 | /** |
||
81 | * Magic method to store properties. |
||
82 | * |
||
83 | * @param string $name |
||
84 | * @param mixed $value |
||
85 | */ |
||
86 | public function __set($name, $value) |
||
118 | |||
119 | /** |
||
120 | * Magic method to check if a property is defined or not. |
||
121 | * |
||
122 | * @param string $name Property name |
||
123 | * |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function __isset($name) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function toArray(array $bannedEntities = []) |
||
153 | |||
154 | /** |
||
155 | * Saves this row in the database. |
||
156 | * |
||
157 | * @param bool|array $relations Set true to save the relations with other entities |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function save($relations = false) |
||
251 | } |
||
252 |
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.