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 mixed $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 mixed $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) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * helper function to add condition into WHERE. |
||
| 332 | * |
||
| 333 | * @param ActiveRecordExpressions $expression <p>The expression will be concat into WHERE or SET statement.</p> |
||
| 334 | * @param string $operator <p>The operator to concat this Expressions into WHERE or SET |
||
| 335 | * statement.</p> |
||
| 336 | * @param string $name <p>The Expression will contact to.</p> |
||
| 337 | */ |
||
| 338 | 14 | protected function _addCondition(ActiveRecordExpressions $expression, string $operator, string $name = 'where') |
|
| 361 | |||
| 362 | /** |
||
| 363 | * helper function to make wrapper. Stored the expression in to array. |
||
| 364 | * |
||
| 365 | * @param ActiveRecordExpressions $exp <p>The expression will be stored.</p> |
||
| 366 | * @param string $operator <p>The operator to concat this Expressions into WHERE statement.</p> |
||
| 367 | */ |
||
| 368 | 1 | protected function _addExpression(ActiveRecordExpressions $exp, string $operator) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Helper function to build SQL with sql parts. |
||
| 388 | * |
||
| 389 | * @param string[] $sql_array <p>The SQL part will be build.</p> |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 21 | protected function _buildSql(array $sql_array = []): string |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Helper function to build SQL with sql parts. |
||
| 405 | * |
||
| 406 | * @param string $sql_string_part <p>The SQL part will be build.</p> |
||
| 407 | * @param int $index <p>The index of $n in $sql array.</p> |
||
| 408 | * @param self $active_record <p>The reference to $this.</p> |
||
| 409 | */ |
||
| 410 | 21 | private function _buildSqlCallback(string &$sql_string_part, int $index, self $active_record) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Helper function to build place holder when make SQL expressions. |
||
| 445 | * |
||
| 446 | * @param mixed $value <p>The value will be bind to SQL, just store it in $this->params.</p> |
||
| 447 | * |
||
| 448 | * @return mixed $value |
||
| 449 | */ |
||
| 450 | 18 | protected function _filterParam($value) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Helper function to add condition into WHERE. |
||
| 466 | * |
||
| 467 | * @param string $field <p>The field name, the source of Expressions</p> |
||
| 468 | * @param string $operator <p>The operator for this condition.</p> |
||
| 469 | * @param mixed $value <p>The target of the Expressions.</p> |
||
| 470 | * @param string $operator_concat <p>The operator to concat this Expressions into WHERE or SET statement.</p> |
||
| 471 | * @param string $name <p>The Expression will contact to.</p> |
||
| 472 | */ |
||
| 473 | 14 | public function addCondition(string $field, string $operator, $value, string $operator_concat = 'AND', string $name = 'where') |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Helper function to copy an existing active record (and insert it into the database). |
||
| 500 | * |
||
| 501 | * @param bool $insert |
||
| 502 | * |
||
| 503 | * @return $this |
||
| 504 | */ |
||
| 505 | 1 | public function copy(bool $insert = true): self |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Function to delete current record in database. |
||
| 520 | * |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | 1 | public function delete(): bool |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Helper function to exec sql. |
||
| 541 | * |
||
| 542 | * @param string $sql <p>The SQL need to be execute.</p> |
||
| 543 | * @param array $param <p>The param will be bind to the sql statement.</p> |
||
| 544 | * |
||
| 545 | * @return bool|int|Result <p> |
||
| 546 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 547 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 548 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 549 | * "true" by e.g. "DROP"-queries<br /> |
||
| 550 | * "false" on error |
||
| 551 | * </p> |
||
| 552 | */ |
||
| 553 | 23 | public function execute(string $sql, array $param = []) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Function to find one record and assign in to current object. |
||
| 564 | * |
||
| 565 | * @param mixed $id <p> |
||
| 566 | * If call this function using this param, we will find the record by using this id. |
||
| 567 | * If not set, just find the first record in database. |
||
| 568 | * </p> |
||
| 569 | * |
||
| 570 | * @return false|$this <p> |
||
| 571 | * If we could find the record, assign in to current object and return it, |
||
| 572 | * otherwise return "false". |
||
| 573 | * </p> |
||
| 574 | */ |
||
| 575 | 11 | public function fetch($id = null) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Function to find all records in database. |
||
| 606 | * |
||
| 607 | * @param array|null $ids <p> |
||
| 608 | * If call this function using this param, we will find the record by using this id's. |
||
| 609 | * If not set, just find all records in database. |
||
| 610 | * </p> |
||
| 611 | * |
||
| 612 | * @return $this[] |
||
| 613 | */ |
||
| 614 | 6 | public function fetchAll(array $ids = null): array |
|
| 637 | |||
| 638 | /** |
||
| 639 | * @param mixed $id |
||
| 640 | * |
||
| 641 | * @return $this |
||
| 642 | * |
||
| 643 | * @throws FetchingException <p>Will be thrown, if we can not find the id.</p> |
||
| 644 | */ |
||
| 645 | 2 | public function fetchById($id): self |
|
| 654 | |||
| 655 | /** |
||
| 656 | * @param mixed $id |
||
| 657 | * |
||
| 658 | * @return $this|null |
||
| 659 | */ |
||
| 660 | 4 | public function fetchByIdIfExists($id) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * @param array $ids |
||
| 673 | * |
||
| 674 | * @return $this[] |
||
| 675 | */ |
||
| 676 | 2 | public function fetchByIds(array $ids): array |
|
| 689 | |||
| 690 | /** |
||
| 691 | * @param array $ids |
||
| 692 | * |
||
| 693 | * @return $this[] |
||
| 694 | */ |
||
| 695 | 1 | public function fetchByIdsPrimaryKeyAsArrayIndex(array $ids): array |
|
| 706 | |||
| 707 | /** |
||
| 708 | * @param string $query |
||
| 709 | * |
||
| 710 | * @return $this[]|$this |
||
| 711 | */ |
||
| 712 | 2 | public function fetchByQuery(string $query) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * @return $this |
||
| 735 | */ |
||
| 736 | 1 | public static function fetchEmpty(): self |
|
| 742 | |||
| 743 | /** |
||
| 744 | * @param string $query |
||
| 745 | * |
||
| 746 | * @return $this[] |
||
| 747 | */ |
||
| 748 | 1 | public function fetchManyByQuery(string $query): array |
|
| 758 | |||
| 759 | /** |
||
| 760 | * @param string $query |
||
| 761 | * |
||
| 762 | * @return $this|null |
||
| 763 | */ |
||
| 764 | 1 | public function fetchOneByQuery(string $query) |
|
| 780 | |||
| 781 | /** |
||
| 782 | * @return array |
||
| 783 | */ |
||
| 784 | 1 | public function getDirty(): array |
|
| 788 | |||
| 789 | /** |
||
| 790 | * @return array |
||
| 791 | */ |
||
| 792 | public function getParams(): array |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @return mixed|null |
||
| 799 | */ |
||
| 800 | 13 | public function getPrimaryKey() |
|
| 809 | |||
| 810 | /** |
||
| 811 | * @return string |
||
| 812 | */ |
||
| 813 | public function getPrimaryKeyName(): string |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Helper function to get relation of this object. |
||
| 820 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 821 | * |
||
| 822 | * @param string $name <p>The name of the relation (the array key from the definition).</p> |
||
| 823 | * |
||
| 824 | * @return mixed |
||
| 825 | * |
||
| 826 | * @throws ActiveRecordException <p>If the relation can't be found .</p> |
||
| 827 | */ |
||
| 828 | 3 | protected function &getRelation(string $name) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * @return string |
||
| 901 | */ |
||
| 902 | public function getTable(): string |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Helper function for "GROUP BY". |
||
| 909 | * |
||
| 910 | * @param mixed $args |
||
| 911 | * |
||
| 912 | * @return $this |
||
| 913 | */ |
||
| 914 | public function groupBy($args): self |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Function to build insert SQL, and insert current record into database. |
||
| 923 | * |
||
| 924 | * @return bool|int <p> |
||
| 925 | * If insert was successful, it will return the new id, |
||
| 926 | * otherwise it will return false or true (if there are no dirty data). |
||
| 927 | * </p> |
||
| 928 | */ |
||
| 929 | 4 | public function insert() |
|
| 965 | |||
| 966 | /** |
||
| 967 | * @return bool |
||
| 968 | */ |
||
| 969 | public function isNewDataAreDirty(): bool |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Helper function to add condition into JOIN. |
||
| 976 | * |
||
| 977 | * @param string $table <p>The join table name.</p> |
||
| 978 | * @param string $on <p>The condition of ON.</p> |
||
| 979 | * @param string $type <p>The join type, like "LEFT", "INNER", "OUTER".</p> |
||
| 980 | * |
||
| 981 | * @return $this |
||
| 982 | */ |
||
| 983 | 1 | public function join(string $table, string $on, string $type = 'LEFT'): self |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Helper function for "ORDER BY". |
||
| 1004 | * |
||
| 1005 | * @param mixed $args |
||
| 1006 | * |
||
| 1007 | * @return $this |
||
| 1008 | */ |
||
| 1009 | 2 | public function orderBy($args): self |
|
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Helper function to query one record by sql and params. |
||
| 1018 | * |
||
| 1019 | * @param string $sql <p> |
||
| 1020 | * The SQL query to find the record. |
||
| 1021 | * </p> |
||
| 1022 | * @param array $param <p> |
||
| 1023 | * The param will be bind to the $sql query. |
||
| 1024 | * </p> |
||
| 1025 | * @param null|self $obj <p> |
||
| 1026 | * The object, if find record in database, we will assign the attributes into |
||
| 1027 | * this object. |
||
| 1028 | * </p> |
||
| 1029 | * @param bool $single <p> |
||
| 1030 | * If set to true, we will find record and fetch in current object, otherwise |
||
| 1031 | * will find all records. |
||
| 1032 | * </p> |
||
| 1033 | * |
||
| 1034 | * @return bool|$this|$this[] |
||
| 1035 | */ |
||
| 1036 | 17 | public function query(string $sql, array $param = [], self $obj = null, bool $single = false) |
|
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Function to reset the $params and $sqlExpressions. |
||
| 1066 | * |
||
| 1067 | * @return $this |
||
| 1068 | */ |
||
| 1069 | 23 | public function reset(): self |
|
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Reset the dirty data. |
||
| 1079 | * |
||
| 1080 | * @return $this |
||
| 1081 | */ |
||
| 1082 | 6 | public function resetDirty(): self |
|
| 1088 | |||
| 1089 | /** |
||
| 1090 | * set the DB connection. |
||
| 1091 | * |
||
| 1092 | * @param DB $db |
||
| 1093 | */ |
||
| 1094 | public function setDb(DB $db) |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * @param bool $bool |
||
| 1101 | */ |
||
| 1102 | 17 | public function setNewDataAreDirty(bool $bool) |
|
| 1106 | |||
| 1107 | /** |
||
| 1108 | * @param mixed $primaryKey |
||
| 1109 | * @param bool $dirty |
||
| 1110 | * |
||
| 1111 | * @return $this |
||
| 1112 | */ |
||
| 1113 | 1 | public function setPrimaryKey($primaryKey, bool $dirty = true): self |
|
| 1127 | |||
| 1128 | /** |
||
| 1129 | * @param string $primaryKeyName |
||
| 1130 | * |
||
| 1131 | * @return $this |
||
| 1132 | */ |
||
| 1133 | public function setPrimaryKeyName(string $primaryKeyName): self |
||
| 1139 | |||
| 1140 | /** |
||
| 1141 | * @param string $table |
||
| 1142 | */ |
||
| 1143 | public function setTable(string $table) |
||
| 1147 | |||
| 1148 | /** |
||
| 1149 | * Function to build update SQL, and update current record in database, just write the dirty data into database. |
||
| 1150 | * |
||
| 1151 | * @return bool|int <p> |
||
| 1152 | * If update was successful, it will return the affected rows as int, |
||
| 1153 | * otherwise it will return false or true (if there are no dirty data). |
||
| 1154 | * </p> |
||
| 1155 | */ |
||
| 1156 | 2 | public function update() |
|
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Make wrap when build the SQL expressions of WHERE. |
||
| 1188 | * |
||
| 1189 | * @param string $op <p>If given, this param will build one "ActiveRecordExpressionsWrap" and include the stored |
||
| 1190 | * expressions add into WHERE, otherwise it will stored the expressions into an array.</p> |
||
| 1191 | * |
||
| 1192 | * @return $this |
||
| 1193 | */ |
||
| 1194 | 1 | public function wrap($op = null): self |
|
| 1216 | } |
||
| 1217 |
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..