Complex classes like ActiveRecord 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 ActiveRecord, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class ActiveRecord extends BaseActiveRecord |
||
21 | { |
||
22 | /** |
||
23 | * Returns the database connection used by this AR class. |
||
24 | * By default, the "hiart" application component is used as the database connection. |
||
25 | * You may override this method if you want to use a different database connection. |
||
26 | * |
||
27 | * @return AbstractConnection the database connection used by this AR class |
||
28 | */ |
||
29 | public static function getDb() |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | * @return ActiveQuery the newly created [[ActiveQuery]] instance |
||
37 | */ |
||
38 | 2 | public static function find() |
|
44 | |||
45 | public function isScenarioDefault() |
||
49 | |||
50 | /** |
||
51 | * This method defines the attribute that uniquely identifies a record. |
||
52 | * |
||
53 | * The primaryKey for HiArt objects is the `id` field by default. This field is not part of the |
||
54 | * ActiveRecord attributes so you should never add `_id` to the list of [[attributes()|attributes]]. |
||
55 | * |
||
56 | * You may override this method to define the primary key name. |
||
57 | * |
||
58 | * Note that HiArt only supports _one_ attribute to be the primary key. However to match the signature |
||
59 | * of the [[\yii\db\ActiveRecordInterface|ActiveRecordInterface]] this methods returns an array instead of a |
||
60 | * single string. |
||
61 | * |
||
62 | * @return string[] array of primary key attributes. Only the first element of the array will be used. |
||
63 | */ |
||
64 | public static function primaryKey() |
||
68 | |||
69 | /** |
||
70 | * Returns the list of attribute names. |
||
71 | * By default, this method returns all attributes mentioned in rules. |
||
72 | * You may override this method to change the default behavior. |
||
73 | * @return string[] list of attribute names |
||
74 | */ |
||
75 | 2 | public function attributes() |
|
76 | { |
||
77 | 2 | $attributes = []; |
|
78 | 2 | foreach ($this->rules() as $rule) { |
|
79 | 2 | $names = reset($rule); |
|
80 | 2 | if (is_string($names)) { |
|
81 | 2 | $names = [$names]; |
|
82 | } |
||
83 | 2 | foreach ($names as $name) { |
|
|
|||
84 | 2 | if (substr_compare($name, '!', 0, 1) === 0) { |
|
85 | $name = mb_substr($name, 1); |
||
86 | } |
||
87 | 2 | $attributes[$name] = $name; |
|
88 | } |
||
89 | } |
||
90 | |||
91 | 2 | return array_values($attributes); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Creates an active record instance. |
||
96 | * |
||
97 | * This method is called together with [[populateRecord()]] by [[ActiveQuery]]. |
||
98 | * It is not meant to be used for creating new records directly. |
||
99 | * |
||
100 | * You may override this method if the instance being created |
||
101 | * depends on the row data to be populated into the record. |
||
102 | * For example, by creating a record based on the value of a column, |
||
103 | * you may implement the so-called single-table inheritance mapping. |
||
104 | * |
||
105 | * @return static the newly created active record |
||
106 | */ |
||
107 | 2 | public static function instantiate($row) |
|
108 | { |
||
109 | 2 | return new static(); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return string the name of the entity of this record |
||
114 | */ |
||
115 | 2 | public static function tableName() |
|
119 | |||
120 | /** |
||
121 | * Declares the name of the model associated with this class. |
||
122 | * By default this method returns the class name by calling [[Inflector::camel2id()]]. |
||
123 | * |
||
124 | * @return string the module name |
||
125 | */ |
||
126 | public static function modelName() |
||
130 | |||
131 | public function insert($runValidation = true, $attributes = null, $options = []) |
||
132 | { |
||
133 | if ($runValidation && !$this->validate($attributes)) { |
||
134 | return false; |
||
135 | } |
||
136 | |||
137 | if (!$this->beforeSave(true)) { |
||
138 | return false; |
||
139 | } |
||
140 | |||
141 | $values = $this->getDirtyAttributes($attributes); |
||
142 | $data = array_merge($values, $options, ['id' => $this->getOldPrimaryKey()]); |
||
143 | $result = $this->query('insert', $data); |
||
144 | |||
145 | $pk = static::primaryKey()[0]; |
||
146 | $this->$pk = $result['id']; |
||
147 | if ($pk !== 'id') { |
||
148 | $values[$pk] = $result['id']; |
||
149 | } |
||
150 | $changedAttributes = array_fill_keys(array_keys($values), null); |
||
151 | $this->setOldAttributes($values); |
||
152 | $this->afterSave(true, $changedAttributes); |
||
153 | |||
154 | return true; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function delete($options = []) |
||
174 | |||
175 | public function update($runValidation = true, $attributeNames = null, $options = []) |
||
183 | |||
184 | protected function updateInternal($attributes = null, $options = []) |
||
185 | { |
||
186 | if (!$this->beforeSave(false)) { |
||
209 | |||
210 | /** |
||
211 | * Perform batch query. |
||
212 | * Attention: takes bulk data and returns bulk result. |
||
213 | * @param string $defaultScenario |
||
214 | * @param array $data bulk data |
||
215 | * @param array $options |
||
216 | * @return array bulk results |
||
217 | */ |
||
218 | public function batchQuery($defaultScenario, $data = [], array $options = []) |
||
237 | |||
238 | /** |
||
239 | * Perform query. |
||
240 | * @param string $defaultScenario |
||
241 | * @param array $data data |
||
242 | * @param array $options |
||
243 | * @return array result |
||
244 | */ |
||
245 | public function query($defaultScenario, $data = [], array $options = []) |
||
251 | |||
252 | public static function batchPerform($action, $data = [], array $options = []) |
||
258 | |||
259 | public static function perform($action, $data = [], array $options = []) |
||
263 | |||
264 | /** |
||
265 | * Converts scenario name to action. |
||
266 | * @param string $default default action name |
||
267 | * @throws InvalidConfigException |
||
268 | * @throws NotSupportedException |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getScenarioAction($default = '') |
||
285 | |||
286 | /** |
||
287 | * Provides a correspondance array: scenario -> API action. |
||
288 | * E.g. ['update-name' => 'set-name']. |
||
289 | * @return array |
||
290 | */ |
||
291 | public function scenarioActions() |
||
295 | |||
296 | /** |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function getIsNewRecord() |
||
303 | |||
304 | /** |
||
305 | * This method has no effect in HiArt ActiveRecord. |
||
306 | */ |
||
307 | public function optimisticLock() |
||
311 | |||
312 | /** |
||
313 | * Destroys the relationship in current model. |
||
314 | * |
||
315 | * This method is not supported by HiArt. |
||
316 | */ |
||
317 | public function unlinkAll($name, $delete = false) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | * |
||
325 | * @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist |
||
326 | * and `$throwException` is false, null will be returned. |
||
327 | */ |
||
328 | public function getRelation($name, $throwException = true) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | * @return ActiveQuery the relational query object |
||
336 | */ |
||
337 | public function hasOne($class, $link) |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | * @return ActiveQuery the relational query object |
||
345 | */ |
||
346 | public function hasMany($class, $link) |
||
350 | } |
||
351 |
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.