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 |
||
| 19 | abstract class ActiveRecord extends AccessLogged { |
||
| 20 | const IGNORE_PREFIX = 'Table'; |
||
| 21 | |||
| 22 | const FIELDS_TO_PROPERTIES = true; |
||
| 23 | const PROPERTIES_TO_FIELDS = false; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Autoincrement index field name in DB |
||
| 27 | * Would be normalized to 'id' ($id class property) |
||
| 28 | * |
||
| 29 | * @var string $_primaryIndexField |
||
| 30 | */ |
||
| 31 | protected static $_primaryIndexField = 'id'; |
||
| 32 | protected static $_tableName = ''; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Field name translations to property names |
||
| 36 | * |
||
| 37 | * @var string[] $_fieldsToProperties |
||
| 38 | */ |
||
| 39 | protected static $_fieldsToProperties = []; |
||
| 40 | |||
| 41 | // AR's service fields |
||
| 42 | protected $_isNew = true; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * ActiveRecord constructor. |
||
| 46 | * |
||
| 47 | * @param GlobalContainer|null $services |
||
| 48 | */ |
||
| 49 | public function __construct(GlobalContainer $services = null) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Finds records by params in DB |
||
| 55 | * |
||
| 56 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value]. Pass [] to find all records in DB |
||
| 57 | * |
||
| 58 | * @return bool|\mysqli_result |
||
| 59 | */ |
||
| 60 | public static function find($where) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Gets first ActiveRecord by $where |
||
| 75 | * |
||
| 76 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 77 | * |
||
| 78 | * @return static|bool |
||
| 79 | */ |
||
| 80 | public static function findOne($where) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Gets all ActiveRecords by $where |
||
| 91 | * |
||
| 92 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 93 | * |
||
| 94 | * @return array|static[] - [(int) => static] |
||
| 95 | */ |
||
| 96 | public static function findAll($where) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Gets all ActiveRecords by $where - array indexed by record IDs |
||
| 102 | * |
||
| 103 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 104 | * |
||
| 105 | * @return array|static[] - [$record_db_id => static] |
||
| 106 | */ |
||
| 107 | public static function findAllIndexedObjects($where) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get table name |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public static function tableName() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get used DB |
||
| 126 | * |
||
| 127 | * @return \db_mysql |
||
| 128 | */ |
||
| 129 | public static function db() { |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Gets first record by $where |
||
| 136 | * |
||
| 137 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 138 | * |
||
| 139 | * @return string[] - [$field_name => $field_value] |
||
| 140 | */ |
||
| 141 | public static function findOneArray($where) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Gets all records by $where |
||
| 149 | * |
||
| 150 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 151 | * |
||
| 152 | * @return string[][] - [(int) => [$field_name => $field_value]] |
||
| 153 | */ |
||
| 154 | public static function findAllArray($where) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Gets all records by $where - array indexes is a record IDs |
||
| 160 | * |
||
| 161 | * @param array|mixed $where - ID of found record OR [$field_name => $field_value] |
||
| 162 | * |
||
| 163 | * @return string[][] - [$record_db_id => [$field_name => $field_value]] |
||
| 164 | */ |
||
| 165 | public static function findAllIndexedArray($where) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Normalize array |
||
| 178 | * |
||
| 179 | * Basically - uppercase all field names to make it use in PTL |
||
| 180 | * Can be override by descendants to make more convinient, clear and robust indexes |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function ptlArray() { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Converts property-indexed value array to field-indexed via translation table |
||
| 195 | * |
||
| 196 | * @param array $propertyNamed |
||
| 197 | * @param bool $fieldToProperties - translation direction: true - field to props. false - prop to fields |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | protected static function translateNames(array $propertyNamed, $fieldToProperties = self::FIELDS_TO_PROPERTIES) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array|bool|\mysqli_result|null |
||
| 217 | */ |
||
| 218 | public function update() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return array|bool|\mysqli_result|null |
||
| 237 | */ |
||
| 238 | public function insert() { |
||
| 241 | |||
| 242 | public function save() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Calculate table name by class name and fills internal property |
||
| 248 | * |
||
| 249 | * Namespaces does not count - only class name taken into account |
||
| 250 | * Class name converted from CamelCase to underscore_name |
||
| 251 | * Prefix "Table" is ignored - can be override |
||
| 252 | * |
||
| 253 | * Examples: |
||
| 254 | * Class \Namespace\ClassName will map to table `class_name` |
||
| 255 | * Class \NameSpace\TableLongName will map to table `long_name` |
||
| 256 | * |
||
| 257 | * Can be override to provide different name |
||
| 258 | * |
||
| 259 | */ |
||
| 260 | protected static function fillTableName() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return DbQuery |
||
| 271 | */ |
||
| 272 | protected static function prepareDbQuery() { |
||
| 275 | |||
| 276 | protected function fillProperties($array) { |
||
| 286 | |||
| 287 | |||
| 288 | /** |
||
| 289 | * Instate ActiveRecord from array |
||
| 290 | * |
||
| 291 | * @param $array |
||
| 292 | * |
||
| 293 | * @return static|bool |
||
| 294 | */ |
||
| 295 | public static function fromArray($array) { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $records |
||
| 309 | * |
||
| 310 | * @return array|static[] |
||
| 311 | */ |
||
| 312 | protected static function fromArrayList($records) { |
||
| 324 | |||
| 325 | } |
||
| 326 |