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 |
||
| 18 | abstract class ActiveRecordAbstract extends AccessLogged { |
||
| 19 | const FIELDS_TO_PROPERTIES = true; |
||
| 20 | const PROPERTIES_TO_FIELDS = false; |
||
| 21 | |||
| 22 | const IGNORE_PREFIX = 'Record'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \db_mysql $db |
||
| 26 | */ |
||
| 27 | protected static $db; |
||
| 28 | /** |
||
| 29 | * Table name for current Active Record |
||
| 30 | * |
||
| 31 | * Can be predefined in class or calculated in run-time |
||
| 32 | * |
||
| 33 | * ALWAYS SHOULD BE OVERRIDEN IN CHILD CLASSES! |
||
| 34 | * |
||
| 35 | * @var string $_tableName |
||
| 36 | */ |
||
| 37 | protected static $_tableName = ''; |
||
| 38 | /** |
||
| 39 | * Field name translations to property names |
||
| 40 | * |
||
| 41 | * @var string[] $_fieldsToProperties |
||
| 42 | */ |
||
| 43 | protected static $_fieldsToProperties = []; |
||
| 44 | |||
| 45 | // AR's service fields |
||
| 46 | /** |
||
| 47 | * Is this field - new field? |
||
| 48 | * |
||
| 49 | * @var bool $_isNew |
||
| 50 | */ |
||
| 51 | protected $_isNew = true; |
||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Get table name |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 1 | public static function tableName() { |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param \db_mysql $db |
||
| 70 | */ |
||
| 71 | 1 | public static function setDb(\db_mysql $db) { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get DB |
||
| 77 | * |
||
| 78 | * @return \db_mysql |
||
| 79 | */ |
||
| 80 | 1 | public static function db() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Instate ActiveRecord from array of field values - even if it is empty |
||
| 88 | * |
||
| 89 | * @param array $properties List of field values [$propertyName => $propertyValue] |
||
| 90 | * |
||
| 91 | * @return static |
||
| 92 | */ |
||
| 93 | 1 | public static function buildEvenEmpty(array $properties = []) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Instate ActiveRecord from array of field values |
||
| 102 | * |
||
| 103 | * @param array $properties List of field values [$propertyName => $propertyValue] |
||
| 104 | * |
||
| 105 | * @return static|bool |
||
| 106 | */ |
||
| 107 | 1 | public static function build(array $properties = []) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Finds records by property - equivalent of SELECT ... WHERE ... AND ... |
||
| 117 | * |
||
| 118 | * @param array|mixed $propertyFilter - [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
| 119 | * |
||
| 120 | * @return bool|\mysqli_result |
||
| 121 | */ |
||
| 122 | public static function find($propertyFilter) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets first record by $where |
||
| 133 | * |
||
| 134 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
| 135 | * |
||
| 136 | * @return string[] - [$field_name => $field_value] |
||
| 137 | */ |
||
| 138 | public static function findRecordFirst($propertyFilter) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Gets all records by $where |
||
| 147 | * |
||
| 148 | * @param array|mixed $propertyFilter - ID of record to find OR [$property_name => $property_value] |
||
| 149 | * |
||
| 150 | * @return array[] - [(int) => [$field_name => $field_value]] |
||
| 151 | */ |
||
| 152 | public static function findRecordsAll($propertyFilter) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Gets first ActiveRecord by $where |
||
| 158 | * |
||
| 159 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
| 160 | * |
||
| 161 | * @return static|bool |
||
| 162 | */ |
||
| 163 | public static function findFirst($propertyFilter) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets all ActiveRecords by $where |
||
| 178 | * |
||
| 179 | * @param array|mixed $propertyFilter - ID of record to find OR [$propertyName => $propertyValue]. Pass [] to find all records in table |
||
| 180 | * |
||
| 181 | * @return array|static[] - [(int) => static] |
||
| 182 | */ |
||
| 183 | public static function findAll($propertyFilter) { |
||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * ActiveRecord constructor. |
||
| 190 | * |
||
| 191 | * @param GlobalContainer|null $services |
||
| 192 | */ |
||
| 193 | 1 | public function __construct(GlobalContainer $services = null) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | // TODO - do a check that all fields present in stored data. I.e. no empty fields with no defaults |
||
| 201 | public function insert() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Normalize array |
||
| 223 | * |
||
| 224 | * Basically - uppercase all field names to make it use in PTL |
||
| 225 | * Can be override by descendants to make more convinient, clear and robust indexes |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public function ptlArray() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get default value for field |
||
| 240 | * |
||
| 241 | * @param string $propertyName |
||
| 242 | * |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | 2 | public function getDefault($propertyName) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Returns default value if original value not set |
||
| 256 | * |
||
| 257 | * @param string $propertyName |
||
| 258 | * |
||
| 259 | * @return mixed |
||
| 260 | */ |
||
| 261 | 1 | public function __get($propertyName) { |
|
| 264 | |||
| 265 | 1 | public function __set($propertyName, $value) { |
|
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | |||
| 280 | |||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Calculate table name by class name and fills internal property |
||
| 286 | * |
||
| 287 | * Namespaces does not count - only class name taken into account |
||
| 288 | * Class name converted from CamelCase to underscore_name |
||
| 289 | * Prefix "Record" is ignored - can be override |
||
| 290 | * |
||
| 291 | * Examples: |
||
| 292 | * Class \Namespace\ClassName will map to table `class_name` |
||
| 293 | * Class \NameSpace\RecordLongName will map to table `long_name` |
||
| 294 | * |
||
| 295 | * Can be overriden to provide different name |
||
| 296 | * |
||
| 297 | * @return string - table name in DB |
||
| 298 | * |
||
| 299 | */ |
||
| 300 | 1 | protected static function calcTableName() { |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Get table fields description |
||
| 312 | * |
||
| 313 | * @return DbFieldDescription[] |
||
| 314 | */ |
||
| 315 | protected static function dbGetFieldsDescription() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Prepares DbQuery object for further operations |
||
| 321 | * |
||
| 322 | * @return DbQuery |
||
| 323 | */ |
||
| 324 | 1 | protected static function dbPrepareQuery() { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Is there translation for this field name to property name |
||
| 330 | * |
||
| 331 | * @param string $fieldName |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | 1 | protected static function haveTranslationToProperty($fieldName) { |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Check if field exists |
||
| 341 | * |
||
| 342 | * @param string $fieldName |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | 1 | protected static function haveField($fieldName) { |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Returns field name by property name |
||
| 352 | * |
||
| 353 | * @param string $propertyName |
||
| 354 | * |
||
| 355 | * @return string Field name for property or '' if not field |
||
| 356 | */ |
||
| 357 | 4 | protected static function getFieldName($propertyName) { |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Does property exists? |
||
| 378 | * |
||
| 379 | * @param string $propertyName |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | 1 | protected static function haveProperty($propertyName) { |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Translate field name to property name |
||
| 389 | * |
||
| 390 | * @param string $fieldName |
||
| 391 | * |
||
| 392 | * @return string Property name for field if field exists or '' otherwise |
||
| 393 | */ |
||
| 394 | 4 | protected static function getPropertyName($fieldName) { |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Converts property-indexed value array to field-indexed via translation table |
||
| 406 | * |
||
| 407 | * @param array $names |
||
| 408 | * @param bool $fieldToProperties - translation direction: |
||
| 409 | * - self::FIELDS_TO_PROPERTIES - field to props. |
||
| 410 | * - self::PROPERTIES_TO_FIELDS - prop to fields |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | // TODO - Throw exception on incorrect field |
||
| 415 | 1 | protected static function translateNames(array $names, $fieldToProperties = self::FIELDS_TO_PROPERTIES) { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Makes array of object from field/property list array |
||
| 435 | * |
||
| 436 | * Empty records and non-records (non-subarrays) are ignored |
||
| 437 | * Function maintains record indexes |
||
| 438 | * |
||
| 439 | * @param array[] $records - array of DB records [(int) => [$name => $value]] |
||
| 440 | * @param bool $fieldToProperties - should names be translated (true - for field records, false - for property records) |
||
| 441 | * |
||
| 442 | * @return array|static[] |
||
| 443 | */ |
||
| 444 | 1 | protected static function fromRecordList($records, $fieldToProperties = self::FIELDS_TO_PROPERTIES) { |
|
| 469 | |||
| 470 | 1 | protected function defaultValues() { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Set AR properties from array of PROPERTIES |
||
| 492 | * |
||
| 493 | * DOES NOT override existing values |
||
| 494 | * DOES set default values for empty fields |
||
| 495 | * |
||
| 496 | * @param array $properties |
||
| 497 | */ |
||
| 498 | 1 | protected function fromProperties(array $properties) { |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Set AR properties from array of FIELDS |
||
| 508 | * |
||
| 509 | * DOES NOT override existing values |
||
| 510 | * DOES set default values for empty fields |
||
| 511 | * |
||
| 512 | * @param array $fields List of field values [$fieldName => $fieldValue] |
||
| 513 | */ |
||
| 514 | 1 | protected function fromFields(array $fields) { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | protected function dbInsert() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Protects object from setting non-existing property |
||
| 530 | * |
||
| 531 | * @param string $propertyName |
||
| 532 | */ |
||
| 533 | 2 | protected function shieldName($propertyName) { |
|
| 540 | |||
| 541 | } |
||
| 542 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.