Complex classes like ActiveRecordAbstract 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 ActiveRecordAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class ActiveRecordAbstract extends AccessLogged { |
||
13 | const FIELDS_TO_PROPERTIES = true; |
||
14 | const PROPERTIES_TO_FIELDS = false; |
||
15 | |||
16 | const IGNORE_PREFIX = 'Record'; |
||
17 | const ID_PROPERTY_NAME = 'id'; |
||
18 | |||
19 | /** |
||
20 | * @var \db_mysql $db |
||
21 | */ |
||
22 | protected static $db; |
||
23 | |||
24 | /** |
||
25 | * Table name for current Active Record |
||
26 | * |
||
27 | * Can be predefined in class or calculated in run-time |
||
28 | * |
||
29 | * ALWAYS SHOULD BE OVERRIDEN IN CHILD CLASSES! |
||
30 | * |
||
31 | * @var string $_tableName |
||
32 | */ |
||
33 | protected static $_tableName = ''; |
||
34 | /** |
||
35 | * Autoincrement index field name in DB |
||
36 | * Would be normalized to 'id' ($id class property) |
||
37 | * |
||
38 | * @var string $_primaryIndexField |
||
39 | */ |
||
40 | protected static $_primaryIndexField = 'id'; |
||
41 | |||
42 | /** |
||
43 | * Field name translations to property names |
||
44 | * |
||
45 | * @var string[] $_fieldsToProperties |
||
46 | */ |
||
47 | protected static $_fieldsToProperties = []; |
||
48 | /** |
||
49 | * Property name translations to field names |
||
50 | * |
||
51 | * Cached structure |
||
52 | * |
||
53 | * @var string[] $_propertiesToFields |
||
54 | */ |
||
55 | private static $_propertiesToFields = []; |
||
|
|||
56 | |||
57 | // AR's service fields |
||
58 | /** |
||
59 | * Is this field - new field? |
||
60 | * |
||
61 | * @var bool $_isNew |
||
62 | */ |
||
63 | protected $_isNew = true; |
||
64 | |||
65 | // ABSTRACT METHODS - need override ================================================================================== |
||
66 | |||
67 | /** |
||
68 | * @return bool |
||
69 | */ |
||
70 | abstract protected function dbInsert(); |
||
71 | |||
72 | /** |
||
73 | * Asks DB for last insert ID |
||
74 | * |
||
75 | * @return int|string |
||
76 | */ |
||
77 | abstract protected function dbLastInsertId(); |
||
78 | |||
79 | /** |
||
80 | * @return bool |
||
81 | */ |
||
82 | abstract protected function dbUpdate(); |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Get used DB |
||
87 | * |
||
88 | * @return \db_mysql |
||
89 | */ |
||
90 | public static function db() { |
||
95 | |||
96 | /** |
||
97 | * @return array[] |
||
98 | */ |
||
99 | protected static function dbGetTableFields() { |
||
102 | |||
103 | public static function setDb(\db_mysql $db) { |
||
106 | |||
107 | /** |
||
108 | * Get table name |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public static function tableName() { |
||
117 | |||
118 | /** |
||
119 | * Instate ActiveRecord from array of field values - even if it is empty |
||
120 | * |
||
121 | * @param array $fields List of field values [$propertyName => $propertyValue] |
||
122 | * |
||
123 | * @return static |
||
124 | */ |
||
125 | public static function buildEvenEmpty(array $properties = []) { |
||
131 | |||
132 | /** |
||
133 | * Instate ActiveRecord from array of field values |
||
134 | * |
||
135 | * @param array $fields List of field values [$propertyName => $propertyValue] |
||
136 | * |
||
137 | * @return static|bool |
||
138 | */ |
||
139 | public static function build(array $properties = []) { |
||
146 | |||
147 | /** |
||
148 | * Finds records by property - equivalent of SELECT ... WHERE ... AND ... |
||
149 | * |
||
150 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
151 | * |
||
152 | * @return bool|\mysqli_result |
||
153 | */ |
||
154 | public static function find($propertyFilter) { |
||
166 | |||
167 | /** |
||
168 | * Gets first record by $where |
||
169 | * |
||
170 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
171 | * |
||
172 | * @return string[] - [$field_name => $field_value] |
||
173 | */ |
||
174 | public static function findRecordFirst($propertyFilter) { |
||
180 | |||
181 | /** |
||
182 | * Gets all records by $where |
||
183 | * |
||
184 | * @param array|mixed $propertyFilter - ID of record to find OR [$property_name => $property_value] |
||
185 | * |
||
186 | * @return string[][] - [(int) => [$field_name => $field_value]] |
||
187 | */ |
||
188 | public static function findRecordsAll($propertyFilter) { |
||
191 | |||
192 | /** |
||
193 | * Gets all records by $where - array indexes is a record IDs |
||
194 | * |
||
195 | * @param array|mixed $propertyFilter - ID of record to find OR [$property_name => $property_value] |
||
196 | * |
||
197 | * @return string[][] - [$record_db_id => [$field_name => $field_value]] |
||
198 | */ |
||
199 | public static function findRecordsAllIndexed($propertyFilter) { |
||
209 | |||
210 | /** |
||
211 | * Gets first ActiveRecord by $where |
||
212 | * |
||
213 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
214 | * |
||
215 | * @return static|bool |
||
216 | */ |
||
217 | public static function findFirst($propertyFilter) { |
||
229 | |||
230 | /** |
||
231 | * Gets all ActiveRecords by $where |
||
232 | * |
||
233 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
234 | * |
||
235 | * @return array|static[] - [(int) => static] |
||
236 | */ |
||
237 | public static function findAll($propertyFilter) { |
||
240 | |||
241 | /** |
||
242 | * Gets all ActiveRecords by $where - array indexed by record IDs |
||
243 | * |
||
244 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
245 | * |
||
246 | * @return array|static[] - [$record_db_id => static] |
||
247 | */ |
||
248 | public static function findAllIndexed($propertyFilter) { |
||
251 | |||
252 | |||
253 | /** |
||
254 | * ActiveRecord constructor. |
||
255 | * |
||
256 | * @param GlobalContainer|null $services |
||
257 | */ |
||
258 | public function __construct(GlobalContainer $services = null) { |
||
261 | |||
262 | public function acceptChanges() { |
||
266 | |||
267 | /** |
||
268 | * Reload current record from ID |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function reload() { |
||
290 | |||
291 | /** |
||
292 | * @return array|bool|\mysqli_result|null |
||
293 | */ |
||
294 | // TODO - do a check that all fields present in stored data. I.e. no empty fields with no defaults |
||
295 | public function insert() { |
||
316 | |||
317 | /** |
||
318 | * @return array|bool|\mysqli_result|null |
||
319 | */ |
||
320 | public function update() { |
||
335 | |||
336 | /** |
||
337 | * Normalize array |
||
338 | * |
||
339 | * Basically - uppercase all field names to make it use in PTL |
||
340 | * Can be override by descendants to make more convinient, clear and robust indexes |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | public function ptlArray() { |
||
352 | |||
353 | |||
354 | /** |
||
355 | * Returns default value if original value not set |
||
356 | * |
||
357 | * @param string $propertyName |
||
358 | * |
||
359 | * @return mixed |
||
360 | */ |
||
361 | public function __get($propertyName) { |
||
364 | |||
365 | /** |
||
366 | * Protects object from setting non-existing property |
||
367 | * |
||
368 | * @param string $propertyName |
||
369 | */ |
||
370 | protected function shieldName($propertyName) { |
||
377 | |||
378 | public function __set($propertyName, $value) { |
||
382 | |||
383 | /** |
||
384 | * Get default value for field |
||
385 | * |
||
386 | * @param string $propertyName |
||
387 | * |
||
388 | * @return mixed |
||
389 | */ |
||
390 | public function getDefault($propertyName) { |
||
398 | |||
399 | /** |
||
400 | * Calculate table name by class name and fills internal property |
||
401 | * |
||
402 | * Namespaces does not count - only class name taken into account |
||
403 | * Class name converted from CamelCase to underscore_name |
||
404 | * Prefix "Record" is ignored - can be override |
||
405 | * |
||
406 | * Examples: |
||
407 | * Class \Namespace\ClassName will map to table `class_name` |
||
408 | * Class \NameSpace\RecordLongName will map to table `long_name` |
||
409 | * |
||
410 | * Can be overriden to provide different name |
||
411 | * |
||
412 | * @return string - table name in DB |
||
413 | * |
||
414 | */ |
||
415 | protected static function calcTableName() { |
||
424 | |||
425 | /** |
||
426 | * Is there translation for this field name to property name |
||
427 | * |
||
428 | * @param string $fieldName |
||
429 | * |
||
430 | * @return bool |
||
431 | */ |
||
432 | protected static function haveTranslationToProperty($fieldName) { |
||
435 | |||
436 | /** |
||
437 | * Check if field exists |
||
438 | * |
||
439 | * @param string $fieldName |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | protected static function haveField($fieldName) { |
||
446 | |||
447 | /** |
||
448 | * Returns field name by property name |
||
449 | * |
||
450 | * @param string $propertyName |
||
451 | * |
||
452 | * @return string Field name for property or '' if not field |
||
453 | */ |
||
454 | protected static function getFieldName($propertyName) { |
||
472 | |||
473 | /** |
||
474 | * Does property exists? |
||
475 | * |
||
476 | * @param string $propertyName |
||
477 | * |
||
478 | * @return bool |
||
479 | */ |
||
480 | protected static function haveProperty($propertyName) { |
||
483 | |||
484 | /** |
||
485 | * Translate field name to property name |
||
486 | * |
||
487 | * @param string $fieldName |
||
488 | * |
||
489 | * @return string Property name for field if field exists or '' otherwise |
||
490 | */ |
||
491 | protected static function getPropertyName($fieldName) { |
||
500 | |||
501 | /** |
||
502 | * Converts property-indexed value array to field-indexed via translation table |
||
503 | * |
||
504 | * @param array $names |
||
505 | * @param bool $fieldToProperties - translation direction: |
||
506 | * - self::FIELDS_TO_PROPERTIES - field to props. |
||
507 | * - self::PROPERTIES_TO_FIELDS - prop to fields |
||
508 | * |
||
509 | * @return array |
||
510 | */ |
||
511 | // TODO - Throw exception on incorrect field |
||
512 | protected static function translateNames(array $names, $fieldToProperties = self::FIELDS_TO_PROPERTIES) { |
||
530 | |||
531 | /** |
||
532 | * Makes array of object from field/property list array |
||
533 | * |
||
534 | * Empty records and non-records (non-subarrays) are ignored |
||
535 | * Function maintains record indexes |
||
536 | * |
||
537 | * @param array $records - array of DB records [(int) => [$name => $value]] |
||
538 | * @param bool $fieldToProperties - should names be translated (true - for field records, false - for property records) |
||
539 | * |
||
540 | * @return array|static[] |
||
541 | */ |
||
542 | protected static function fromRecordList($records, $fieldToProperties = self::FIELDS_TO_PROPERTIES) { |
||
564 | |||
565 | /** |
||
566 | * Prepares DbQuery object for further operations |
||
567 | * |
||
568 | * @return DbQuery |
||
569 | */ |
||
570 | protected static function dbPrepareQuery() { |
||
573 | |||
574 | protected static function dbFetch(\mysqli_result $mysqliResult) { |
||
577 | |||
578 | |||
579 | protected function defaultValues() { |
||
598 | |||
599 | /** |
||
600 | * Set AR properties from array of PROPERTIES |
||
601 | * |
||
602 | * DOES NOT override existing values |
||
603 | * DOES set default values for empty fields |
||
604 | * |
||
605 | * @param array $properties |
||
606 | */ |
||
607 | protected function fromProperties(array $properties) { |
||
614 | |||
615 | /** |
||
616 | * Set AR properties from array of FIELDS |
||
617 | * |
||
618 | * DOES NOT override existing values |
||
619 | * DOES set default values for empty fields |
||
620 | * |
||
621 | * @param array $fields List of field values [$fieldName => $fieldValue] |
||
622 | */ |
||
623 | protected function fromFields(array $fields) { |
||
626 | |||
627 | } |
||
628 |
This check marks private properties in classes that are never used. Those properties can be removed.