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 |
||
| 42 | abstract class ActiveRecord extends Arrayy |
||
| 43 | { |
||
| 44 | const BELONGS_TO = 'belongs_to'; |
||
| 45 | const HAS_MANY = 'has_many'; |
||
| 46 | const HAS_ONE = 'has_one'; |
||
| 47 | |||
| 48 | const PREFIX = ':active_record'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array <p>Mapping the function name and the operator, to build Expressions in WHERE condition.</p> |
||
| 52 | * |
||
| 53 | * call the function like this: |
||
| 54 | * <pre> |
||
| 55 | * $user->isNotNull()->eq('id', 1); |
||
| 56 | * </pre> |
||
| 57 | * |
||
| 58 | * the result in SQL: |
||
| 59 | * <pre> |
||
| 60 | * WHERE user.id IS NOT NULL AND user.id = :ph1 |
||
| 61 | * </pre> |
||
| 62 | */ |
||
| 63 | protected static $operators = [ |
||
| 64 | 'equal' => '=', |
||
| 65 | 'eq' => '=', |
||
| 66 | 'notequal' => '<>', |
||
| 67 | 'ne' => '<>', |
||
| 68 | 'greaterthan' => '>', |
||
| 69 | 'gt' => '>', |
||
| 70 | 'lessthan' => '<', |
||
| 71 | 'lt' => '<', |
||
| 72 | 'greaterthanorequal' => '>=', |
||
| 73 | 'ge' => '>=', |
||
| 74 | 'gte' => '>=', |
||
| 75 | 'lessthanorequal' => '<=', |
||
| 76 | 'le' => '<=', |
||
| 77 | 'lte' => '<=', |
||
| 78 | 'between' => 'BETWEEN', |
||
| 79 | 'like' => 'LIKE', |
||
| 80 | 'in' => 'IN', |
||
| 81 | 'notin' => 'NOT IN', |
||
| 82 | 'isnull' => 'IS NULL', |
||
| 83 | 'isnotnull' => 'IS NOT NULL', |
||
| 84 | 'notnull' => 'IS NOT NULL', |
||
| 85 | ]; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var int <p>The count of bind params, using this count and const "PREFIX" (:ph) to generate place holder in |
||
| 89 | * SQL.</p> |
||
| 90 | */ |
||
| 91 | private static $count = 0; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var DB |
||
| 95 | */ |
||
| 96 | protected $db; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array <p>Part of the SQL, mapping the function name and the operator to build SQL Part.</p> |
||
| 100 | * |
||
| 101 | * <br /> |
||
| 102 | * |
||
| 103 | * call the function like this: |
||
| 104 | * <pre> |
||
| 105 | * $user->orderBy('id DESC', 'name ASC')->limit(2, 1); |
||
| 106 | * </pre> |
||
| 107 | * |
||
| 108 | * the result in SQL: |
||
| 109 | * <pre> |
||
| 110 | * ORDER BY id DESC, name ASC LIMIT 2,1 |
||
| 111 | * </pre> |
||
| 112 | */ |
||
| 113 | protected $sqlParts = [ |
||
| 114 | 'select' => 'SELECT', |
||
| 115 | 'from' => 'FROM', |
||
| 116 | 'set' => 'SET', |
||
| 117 | 'where' => 'WHERE', |
||
| 118 | 'group' => 'GROUP BY', |
||
| 119 | 'having' => 'HAVING', |
||
| 120 | 'order' => 'ORDER BY', |
||
| 121 | 'limit' => 'LIMIT', |
||
| 122 | 'top' => 'TOP', |
||
| 123 | ]; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var array <p>The default sql expressions values.</p> |
||
| 127 | */ |
||
| 128 | protected $defaultSqlExpressions = [ |
||
| 129 | 'expressions' => [], |
||
| 130 | 'wrap' => false, |
||
| 131 | 'select' => null, |
||
| 132 | 'insert' => null, |
||
| 133 | 'update' => null, |
||
| 134 | 'set' => null, |
||
| 135 | 'delete' => 'DELETE ', |
||
| 136 | 'join' => null, |
||
| 137 | 'from' => null, |
||
| 138 | 'values' => null, |
||
| 139 | 'where' => null, |
||
| 140 | 'having' => null, |
||
| 141 | 'limit' => null, |
||
| 142 | 'order' => null, |
||
| 143 | 'group' => null, |
||
| 144 | ]; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var array <p>Stored the Expressions of the SQL.</p> |
||
| 148 | */ |
||
| 149 | protected $sqlExpressions = []; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var string <p>The table name in database.</p> |
||
| 153 | */ |
||
| 154 | protected $table; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var string <p>The primary key of this ActiveRecord, just support single primary key.</p> |
||
| 158 | */ |
||
| 159 | protected $primaryKeyName = 'id'; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var array <p>Stored the dirty data of this object, when call "insert" or "update" function, will write this data |
||
| 163 | * into database.</p> |
||
| 164 | */ |
||
| 165 | protected $dirty = []; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var bool |
||
| 169 | */ |
||
| 170 | protected $new_data_are_dirty = true; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var array <p>Stored the params will bind to SQL when call DB->query().</p> |
||
| 174 | */ |
||
| 175 | protected $params = []; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var ActiveRecordExpressions[] <p>Stored the configure of the relation, or target of the relation.</p> |
||
| 179 | */ |
||
| 180 | protected $relations = []; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Magic function to make calls witch in function mapping stored in $operators and $sqlPart. |
||
| 184 | * also can call function of DB object. |
||
| 185 | * |
||
| 186 | * @param string $name <p>The name of the function.</p> |
||
| 187 | * @param array $args <p>The arguments of the function.</p> |
||
| 188 | * |
||
| 189 | * @return $this|mixed <p>Return the result of callback or the current object to make chain method calls.</p> |
||
| 190 | * |
||
| 191 | * @throws ActiveRecordException |
||
| 192 | */ |
||
| 193 | 16 | public function __call(string $name, array $args = []) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Magic function to GET the values of current object. |
||
| 234 | * |
||
| 235 | * @param mixed $var |
||
| 236 | * |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | 23 | public function &__get($var) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Magic function to SET values of the current object. |
||
| 258 | * |
||
| 259 | * @param string $var |
||
| 260 | * @param mixed $val |
||
| 261 | */ |
||
| 262 | 23 | public function __set($var, $val) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Magic function to UNSET values of the current object. |
||
| 293 | * |
||
| 294 | * @param mixed $var |
||
| 295 | */ |
||
| 296 | 1 | public function __unset($var) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get a value from an array (optional using dot-notation). |
||
| 313 | * |
||
| 314 | * @param string $key <p>The key to look for.</p> |
||
| 315 | * @param mixed $fallback <p>Value to fallback to.</p> |
||
| 316 | * @param array $array <p>The array to get from, if it's set to "null" we use the current array from the |
||
| 317 | * class.</p> |
||
| 318 | * |
||
| 319 | * @return mixed |
||
| 320 | */ |
||
| 321 | 23 | public function get($key, $fallback = null, array $array = null) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * helper function to add condition into WHERE. |
||
| 328 | * |
||
| 329 | * @param ActiveRecordExpressions $expression <p>The expression will be concat into WHERE or SET statement.</p> |
||
| 330 | * @param string $operator <p>The operator to concat this Expressions into WHERE or SET |
||
| 331 | * statement.</p> |
||
| 332 | * @param string $name <p>The Expression will contact to.</p> |
||
| 333 | */ |
||
| 334 | 14 | protected function _addCondition(ActiveRecordExpressions $expression, string $operator, string $name = 'where') |
|
| 357 | |||
| 358 | /** |
||
| 359 | * helper function to make wrapper. Stored the expression in to array. |
||
| 360 | * |
||
| 361 | * @param ActiveRecordExpressions $exp <p>The expression will be stored.</p> |
||
| 362 | * @param string $operator <p>The operator to concat this Expressions into WHERE statement.</p> |
||
| 363 | */ |
||
| 364 | 1 | protected function _addExpression(ActiveRecordExpressions $exp, string $operator) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Helper function to build SQL with sql parts. |
||
| 384 | * |
||
| 385 | * @param string[] $sql_array <p>The SQL part will be build.</p> |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | 21 | protected function _buildSql(array $sql_array = []): string |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Helper function to build SQL with sql parts. |
||
| 401 | * |
||
| 402 | * @param string $sql_string_part <p>The SQL part will be build.</p> |
||
| 403 | * @param int $index <p>The index of $n in $sql array.</p> |
||
| 404 | * @param self $active_record <p>The reference to $this.</p> |
||
| 405 | */ |
||
| 406 | 21 | private function _buildSqlCallback(string &$sql_string_part, int $index, self $active_record) |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Helper function to build place holder when make SQL expressions. |
||
| 441 | * |
||
| 442 | * @param mixed $value <p>The value will be bind to SQL, just store it in $this->params.</p> |
||
| 443 | * |
||
| 444 | * @return mixed $value |
||
| 445 | */ |
||
| 446 | 18 | protected function _filterParam($value) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Helper function to add condition into WHERE. |
||
| 462 | * |
||
| 463 | * @param string $field <p>The field name, the source of Expressions</p> |
||
| 464 | * @param string $operator <p>The operator for this condition.</p> |
||
| 465 | * @param mixed $value <p>The target of the Expressions.</p> |
||
| 466 | * @param string $operator_concat <p>The operator to concat this Expressions into WHERE or SET statement.</p> |
||
| 467 | * @param string $name <p>The Expression will contact to.</p> |
||
| 468 | */ |
||
| 469 | 14 | public function addCondition(string $field, string $operator, $value, string $operator_concat = 'AND', string $name = 'where') |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Helper function to copy an existing active record (and insert it into the database). |
||
| 496 | * |
||
| 497 | * @param bool $insert |
||
| 498 | * |
||
| 499 | * @return $this |
||
| 500 | */ |
||
| 501 | 1 | public function copy(bool $insert = true): self |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Function to delete current record in database. |
||
| 516 | * |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | 1 | public function delete(): bool |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Helper function to exec sql. |
||
| 537 | * |
||
| 538 | * @param string $sql <p>The SQL need to be execute.</p> |
||
| 539 | * @param array $param <p>The param will be bind to the sql statement.</p> |
||
| 540 | * |
||
| 541 | * @return bool|int|Result <p> |
||
| 542 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 543 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 544 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 545 | * "true" by e.g. "DROP"-queries<br /> |
||
| 546 | * "false" on error |
||
| 547 | * </p> |
||
| 548 | */ |
||
| 549 | 23 | public function execute(string $sql, array $param = []) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Function to find one record and assign in to current object. |
||
| 560 | * |
||
| 561 | * @param mixed $id <p> |
||
| 562 | * If call this function using this param, we will find the record by using this id. |
||
| 563 | * If not set, just find the first record in database. |
||
| 564 | * </p> |
||
| 565 | * |
||
| 566 | * @return false|$this <p> |
||
| 567 | * If we could find the record, assign in to current object and return it, |
||
| 568 | * otherwise return "false". |
||
| 569 | * </p> |
||
| 570 | */ |
||
| 571 | 11 | public function fetch($id = null) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Function to find all records in database. |
||
| 602 | * |
||
| 603 | * @param array|null $ids <p> |
||
| 604 | * If call this function using this param, we will find the record by using this id's. |
||
| 605 | * If not set, just find all records in database. |
||
| 606 | * </p> |
||
| 607 | * |
||
| 608 | * @return $this[] |
||
| 609 | */ |
||
| 610 | 6 | public function fetchAll(array $ids = null): array |
|
| 633 | |||
| 634 | /** |
||
| 635 | * @param mixed $id |
||
| 636 | * |
||
| 637 | * @return $this |
||
| 638 | * |
||
| 639 | * @throws FetchingException <p>Will be thrown, if we can not find the id.</p> |
||
| 640 | */ |
||
| 641 | 2 | public function fetchById($id): self |
|
| 650 | |||
| 651 | /** |
||
| 652 | * @param mixed $id |
||
| 653 | * |
||
| 654 | * @return $this|null |
||
| 655 | */ |
||
| 656 | 4 | public function fetchByIdIfExists($id) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @param array $ids |
||
| 669 | * |
||
| 670 | * @return $this[] |
||
| 671 | */ |
||
| 672 | 2 | public function fetchByIds(array $ids): array |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @param array $ids |
||
| 688 | * |
||
| 689 | * @return $this[] |
||
| 690 | */ |
||
| 691 | 1 | public function fetchByIdsPrimaryKeyAsArrayIndex(array $ids): array |
|
| 702 | |||
| 703 | /** |
||
| 704 | * @param string $query |
||
| 705 | * |
||
| 706 | * @return $this[]|$this |
||
| 707 | */ |
||
| 708 | 2 | public function fetchByQuery(string $query) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @return $this |
||
| 731 | */ |
||
| 732 | 1 | public static function fetchEmpty(): self |
|
| 738 | |||
| 739 | /** |
||
| 740 | * @param string $query |
||
| 741 | * |
||
| 742 | * @return $this[] |
||
| 743 | */ |
||
| 744 | 1 | public function fetchManyByQuery(string $query): array |
|
| 754 | |||
| 755 | /** |
||
| 756 | * @param string $query |
||
| 757 | * |
||
| 758 | * @return $this|null |
||
| 759 | */ |
||
| 760 | 1 | public function fetchOneByQuery(string $query) |
|
| 776 | |||
| 777 | /** |
||
| 778 | * @return array |
||
| 779 | */ |
||
| 780 | 1 | public function getDirty(): array |
|
| 784 | |||
| 785 | /** |
||
| 786 | * @return array |
||
| 787 | */ |
||
| 788 | public function getParams(): array |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @return mixed|null |
||
| 795 | */ |
||
| 796 | 13 | public function getPrimaryKey() |
|
| 805 | |||
| 806 | /** |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | public function getPrimaryKeyName(): string |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Helper function to get relation of this object. |
||
| 816 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 817 | * |
||
| 818 | * @param string $name <p>The name of the relation (the array key from the definition).</p> |
||
| 819 | * |
||
| 820 | * @return mixed |
||
| 821 | * |
||
| 822 | * @throws ActiveRecordException <p>If the relation can't be found .</p> |
||
| 823 | */ |
||
| 824 | 3 | protected function &getRelation(string $name) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * @return string |
||
| 897 | */ |
||
| 898 | public function getTable(): string |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Helper function for "GROUP BY". |
||
| 905 | * |
||
| 906 | * @param mixed $args |
||
| 907 | * |
||
| 908 | * @return $this |
||
| 909 | */ |
||
| 910 | public function groupBy($args): self |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Function to build insert SQL, and insert current record into database. |
||
| 919 | * |
||
| 920 | * @return bool|int <p> |
||
| 921 | * If insert was successful, it will return the new id, |
||
| 922 | * otherwise it will return false or true (if there are no dirty data). |
||
| 923 | * </p> |
||
| 924 | */ |
||
| 925 | 4 | public function insert() |
|
| 961 | |||
| 962 | /** |
||
| 963 | * @return bool |
||
| 964 | */ |
||
| 965 | public function isNewDataAreDirty(): bool |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Helper function to add condition into JOIN. |
||
| 972 | * |
||
| 973 | * @param string $table <p>The join table name.</p> |
||
| 974 | * @param string $on <p>The condition of ON.</p> |
||
| 975 | * @param string $type <p>The join type, like "LEFT", "INNER", "OUTER".</p> |
||
| 976 | * |
||
| 977 | * @return $this |
||
| 978 | */ |
||
| 979 | 1 | public function join(string $table, string $on, string $type = 'LEFT'): self |
|
| 997 | |||
| 998 | /** |
||
| 999 | * Helper function for "ORDER BY". |
||
| 1000 | * |
||
| 1001 | * @param mixed $args |
||
| 1002 | * |
||
| 1003 | * @return $this |
||
| 1004 | */ |
||
| 1005 | 2 | public function orderBy($args): self |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Helper function to query one record by sql and params. |
||
| 1014 | * |
||
| 1015 | * @param string $sql <p> |
||
| 1016 | * The SQL query to find the record. |
||
| 1017 | * </p> |
||
| 1018 | * @param array $param <p> |
||
| 1019 | * The param will be bind to the $sql query. |
||
| 1020 | * </p> |
||
| 1021 | * @param null|self $obj <p> |
||
| 1022 | * The object, if find record in database, we will assign the attributes into |
||
| 1023 | * this object. |
||
| 1024 | * </p> |
||
| 1025 | * @param bool $single <p> |
||
| 1026 | * If set to true, we will find record and fetch in current object, otherwise |
||
| 1027 | * will find all records. |
||
| 1028 | * </p> |
||
| 1029 | * |
||
| 1030 | * @return bool|$this|$this[] |
||
| 1031 | */ |
||
| 1032 | 17 | public function query(string $sql, array $param = [], self $obj = null, bool $single = false) |
|
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Function to reset the $params and $sqlExpressions. |
||
| 1062 | * |
||
| 1063 | * @return $this |
||
| 1064 | */ |
||
| 1065 | 23 | public function reset(): self |
|
| 1072 | |||
| 1073 | /** |
||
| 1074 | * Reset the dirty data. |
||
| 1075 | * |
||
| 1076 | * @return $this |
||
| 1077 | */ |
||
| 1078 | 6 | public function resetDirty(): self |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * set the DB connection. |
||
| 1087 | * |
||
| 1088 | * @param DB $db |
||
| 1089 | */ |
||
| 1090 | public function setDb(DB $db) |
||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * @param bool $bool |
||
| 1097 | */ |
||
| 1098 | 17 | public function setNewDataAreDirty(bool $bool) |
|
| 1102 | |||
| 1103 | /** |
||
| 1104 | * @param mixed $primaryKey |
||
| 1105 | * @param bool $dirty |
||
| 1106 | * |
||
| 1107 | * @return $this |
||
| 1108 | */ |
||
| 1109 | 1 | public function setPrimaryKey($primaryKey, bool $dirty = true): self |
|
| 1123 | |||
| 1124 | /** |
||
| 1125 | * @param string $primaryKeyName |
||
| 1126 | * |
||
| 1127 | * @return $this |
||
| 1128 | */ |
||
| 1129 | public function setPrimaryKeyName(string $primaryKeyName): self |
||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * @param string $table |
||
| 1138 | */ |
||
| 1139 | public function setTable(string $table) |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Function to build update SQL, and update current record in database, just write the dirty data into database. |
||
| 1146 | * |
||
| 1147 | * @return bool|int <p> |
||
| 1148 | * If update was successful, it will return the affected rows as int, |
||
| 1149 | * otherwise it will return false or true (if there are no dirty data). |
||
| 1150 | * </p> |
||
| 1151 | */ |
||
| 1152 | 2 | public function update() |
|
| 1181 | |||
| 1182 | /** |
||
| 1183 | * Make wrap when build the SQL expressions of WHERE. |
||
| 1184 | * |
||
| 1185 | * @param string $op <p>If given, this param will build one "ActiveRecordExpressionsWrap" and include the stored |
||
| 1186 | * expressions add into WHERE, otherwise it will stored the expressions into an array.</p> |
||
| 1187 | * |
||
| 1188 | * @return $this |
||
| 1189 | */ |
||
| 1190 | 1 | public function wrap($op = null): self |
|
| 1212 | } |
||
| 1213 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..