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 |
||
| 39 | abstract class ActiveRecord extends Arrayy |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var DB static property to connect database. |
||
| 43 | */ |
||
| 44 | protected static $db; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array maping the function name and the operator, to build Expressions in WHERE condition. |
||
| 48 | * |
||
| 49 | * user can call it like this: |
||
| 50 | * <pre> |
||
| 51 | * $user->isnotnull()->eq('id', 1); |
||
| 52 | * </pre> |
||
| 53 | * |
||
| 54 | * will create Expressions can explain to SQL: |
||
| 55 | * <pre> |
||
| 56 | * WHERE user.id IS NOT NULL AND user.id = :ph1 |
||
| 57 | * </pre> |
||
| 58 | */ |
||
| 59 | protected static $operators = array( |
||
| 60 | 'equal' => '=', |
||
| 61 | 'eq' => '=', |
||
| 62 | 'notequal' => '<>', |
||
| 63 | 'ne' => '<>', |
||
| 64 | 'greaterthan' => '>', |
||
| 65 | 'gt' => '>', |
||
| 66 | 'lessthan' => '<', |
||
| 67 | 'lt' => '<', |
||
| 68 | 'greaterthanorequal' => '>=', |
||
| 69 | 'ge' => '>=', |
||
| 70 | 'gte' => '>=', |
||
| 71 | 'lessthanorequal' => '<=', |
||
| 72 | 'le' => '<=', |
||
| 73 | 'lte' => '<=', |
||
| 74 | 'between' => 'BETWEEN', |
||
| 75 | 'like' => 'LIKE', |
||
| 76 | 'in' => 'IN', |
||
| 77 | 'notin' => 'NOT IN', |
||
| 78 | 'isnull' => 'IS NULL', |
||
| 79 | 'isnotnull' => 'IS NOT NULL', |
||
| 80 | 'notnull' => 'IS NOT NULL', |
||
| 81 | ); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array Part of SQL, maping the function name and the operator to build SQL Part. |
||
| 85 | * <pre>call function like this: |
||
| 86 | * $user->order('id DESC', 'name ASC')->limit(2, 1); |
||
| 87 | * can explain to SQL: |
||
| 88 | * ORDER BY id DESC, name ASC LIMIT 2,1</pre> |
||
| 89 | */ |
||
| 90 | protected $sqlParts = array( |
||
| 91 | 'select' => 'SELECT', |
||
| 92 | 'from' => 'FROM', |
||
| 93 | 'set' => 'SET', |
||
| 94 | 'where' => 'WHERE', |
||
| 95 | 'group' => 'GROUP BY', |
||
| 96 | 'having' => 'HAVING', |
||
| 97 | 'order' => 'ORDER BY', |
||
| 98 | 'limit' => 'LIMIT', |
||
| 99 | 'top' => 'TOP', |
||
| 100 | ); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var array Static property to stored the default Sql Expressions values. |
||
| 104 | */ |
||
| 105 | protected $defaultSqlExpressions = array( |
||
| 106 | 'expressions' => array(), |
||
| 107 | 'wrap' => false, |
||
| 108 | 'select' => null, |
||
| 109 | 'insert' => null, |
||
| 110 | 'update' => null, |
||
| 111 | 'set' => null, |
||
| 112 | 'delete' => 'DELETE ', |
||
| 113 | 'join' => null, |
||
| 114 | 'from' => null, |
||
| 115 | 'values' => null, |
||
| 116 | 'where' => null, |
||
| 117 | 'having' => null, |
||
| 118 | 'limit' => null, |
||
| 119 | 'order' => null, |
||
| 120 | 'group' => null, |
||
| 121 | ); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var array Stored the Expressions of the SQL. |
||
| 125 | */ |
||
| 126 | protected $sqlExpressions = array(); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string The table name in database. |
||
| 130 | */ |
||
| 131 | protected $table; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string The primary key of this ActiveRecord, just suport single primary key. |
||
| 135 | */ |
||
| 136 | protected $primaryKeyName = 'id'; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var array Stored the drity data of this object, when call "insert" or "update" function, will write this data |
||
| 140 | * into database. |
||
| 141 | */ |
||
| 142 | protected $dirty = array(); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | protected static $new_data_are_dirty = true; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var array Stored the params will bind to SQL when call DB->query(), |
||
| 151 | */ |
||
| 152 | protected $params = array(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var ActiveRecordExpressions[] Stored the configure of the relation, or target of the relation. |
||
| 156 | */ |
||
| 157 | protected $relations = array(); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var int The count of bind params, using this count and const "PREFIX" (:ph) to generate place holder in SQL. |
||
| 161 | */ |
||
| 162 | private static $count = 0; |
||
| 163 | |||
| 164 | const BELONGS_TO = 'belongs_to'; |
||
| 165 | const HAS_MANY = 'has_many'; |
||
| 166 | const HAS_ONE = 'has_one'; |
||
| 167 | |||
| 168 | const PREFIX = ':active_record'; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getPrimaryKeyName() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return mixed|null |
||
| 180 | */ |
||
| 181 | 4 | public function getPrimaryKey() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getTable() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Function to reset the $params and $sqlExpressions. |
||
| 201 | * |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | 12 | public function reset() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Reset the dirty data. |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 4 | public function resetDirty() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * set the DB connection. |
||
| 226 | * |
||
| 227 | * @param DB $db |
||
| 228 | */ |
||
| 229 | public static function setDb($db) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * function to find one record and assign in to current object. |
||
| 236 | * |
||
| 237 | * @param int $id If call this function using this param, will find record by using this id. If not set, just find |
||
| 238 | * the first record in database. |
||
| 239 | * |
||
| 240 | * @return bool|ActiveRecord if find record, assign in to current object and return it, otherwise return "false". |
||
| 241 | */ |
||
| 242 | 7 | public function fetch($id = null) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Function to find all records in database. |
||
| 269 | * |
||
| 270 | * @return array return array of ActiveRecord |
||
| 271 | */ |
||
| 272 | 3 | public function fetchAll() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Function to delete current record in database. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 1 | public function delete() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $primaryKeyName |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setPrimaryKeyName($primaryKeyName) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $table |
||
| 325 | */ |
||
| 326 | public function setTable($table) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Function to build update SQL, and update current record in database, just write the dirty data into database. |
||
| 333 | * |
||
| 334 | * @return bool|int <p> |
||
| 335 | * If update was successful, it will return the affected rows as int, |
||
| 336 | * otherwise it will return false or true (if there are no dirty data). |
||
| 337 | * </p> |
||
| 338 | */ |
||
| 339 | 2 | public function update() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Function to build insert SQL, and insert current record into database. |
||
| 371 | * |
||
| 372 | * @return bool|int <p> |
||
| 373 | * If insert was successful, it will return the new id, |
||
| 374 | * otherwise it will return false or true (if there are no dirty data). |
||
| 375 | * </p> |
||
| 376 | */ |
||
| 377 | 2 | public function insert() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Helper function to exec sql. |
||
| 416 | * |
||
| 417 | * @param string $sql The SQL need to be execute. |
||
| 418 | * @param array $param The param will be bind to the sql statement. |
||
| 419 | * |
||
| 420 | * @return bool|int|Result <p> |
||
| 421 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 422 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 423 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 424 | * "true" by e.g. "DROP"-queries<br /> |
||
| 425 | * "false" on error |
||
| 426 | * </p> |
||
| 427 | */ |
||
| 428 | 13 | public static function execute($sql, array $param = array()) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Helper function to query one record by sql and params. |
||
| 439 | * |
||
| 440 | * @param string $sql <p> |
||
| 441 | * The SQL query to find the record. |
||
| 442 | * </p> |
||
| 443 | * @param array $param <p> |
||
| 444 | * The param will be bind to the $sql query. |
||
| 445 | * </p> |
||
| 446 | * @param ActiveRecord|null $obj <p> |
||
| 447 | * The object, if find record in database, we will assign the attributes into |
||
| 448 | * this object. |
||
| 449 | * </p> |
||
| 450 | * @param bool $single <p> |
||
| 451 | * If set to true, we will find record and fetch in current object, otherwise |
||
| 452 | * will find all records. |
||
| 453 | * </p> |
||
| 454 | * |
||
| 455 | * @return bool|ActiveRecord|array |
||
| 456 | */ |
||
| 457 | 8 | public static function query($sql, array $param = array(), $obj = null, $single = false) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Helper function to get relation of this object. |
||
| 487 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 488 | * |
||
| 489 | * @param string $name The name of the relation, the array key when defind the relation. |
||
| 490 | * |
||
| 491 | * @return mixed |
||
| 492 | * |
||
| 493 | * @throws ActiveRecordException <p>If the relation can't be found .</p> |
||
| 494 | */ |
||
| 495 | 3 | protected function &getRelation($name) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Helper function to build SQL with sql parts. |
||
| 567 | * |
||
| 568 | * @param string $n The SQL part will be build. |
||
| 569 | * @param int $i The index of $n in $sql array. |
||
| 570 | * @param ActiveRecord $o The reference to $this |
||
| 571 | */ |
||
| 572 | 12 | private function _buildSqlCallback(&$n, $i, $o) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Helper function to build SQL with sql parts. |
||
| 607 | * |
||
| 608 | * @param array $sqls The SQL part will be build. |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | 12 | protected function _buildSql($sqls = array()) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Magic function to make calls witch in function mapping stored in $operators and $sqlPart. |
||
| 624 | * also can call function of DB object. |
||
| 625 | * |
||
| 626 | * @param string $name function name |
||
| 627 | * @param array $args The arguments of the function. |
||
| 628 | * |
||
| 629 | * @return $this|mixed Return the result of callback or the current object to make chain method calls. |
||
| 630 | * |
||
| 631 | * @throws ActiveRecordException |
||
| 632 | */ |
||
| 633 | 9 | public function __call($name, $args) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Make wrap when build the SQL expressions of WHERE. |
||
| 674 | * |
||
| 675 | * @param string $op If give this param will build one WrapExpressions include the stored expressions add into WHERE. |
||
| 676 | * otherwise wil stored the expressions into array. |
||
| 677 | * |
||
| 678 | * @return $this |
||
| 679 | */ |
||
| 680 | 1 | public function wrap($op = null) |
|
| 701 | |||
| 702 | /** |
||
| 703 | * Helper function to build place holder when make SQL expressions. |
||
| 704 | * |
||
| 705 | * @param mixed $value The value will bind to SQL, just store it in $this->params. |
||
| 706 | * |
||
| 707 | * @return mixed $value |
||
| 708 | */ |
||
| 709 | 9 | protected function _filterParam($value) |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Helper function to add condition into WHERE. |
||
| 725 | * create the SQL Expressions. |
||
| 726 | * |
||
| 727 | * @param string $field The field name, the source of Expressions |
||
| 728 | * @param string $operator |
||
| 729 | * @param mixed $value The target of the Expressions |
||
| 730 | * @param string $op The operator to concat this Expressions into WHERE or SET statement. |
||
| 731 | * @param string $name The Expression will contact to. |
||
| 732 | */ |
||
| 733 | 7 | public function addCondition($field, $operator, $value, $op = 'AND', $name = 'where') |
|
| 756 | |||
| 757 | /** |
||
| 758 | * helper function to add condition into JOIN. |
||
| 759 | * create the SQL Expressions. |
||
| 760 | * |
||
| 761 | * @param string $table The join table name |
||
| 762 | * @param string $on The condition of ON |
||
| 763 | * @param string $type The join type, like "LEFT", "INNER", "OUTER" |
||
| 764 | * |
||
| 765 | * @return $this |
||
| 766 | */ |
||
| 767 | 1 | public function join($table, $on, $type = 'LEFT') |
|
| 781 | |||
| 782 | /** |
||
| 783 | * helper function to make wrapper. Stored the expression in to array. |
||
| 784 | * |
||
| 785 | * @param ActiveRecordExpressions $exp The expression will be stored. |
||
| 786 | * @param string $operator The operator to concat this Expressions into WHERE statment. |
||
| 787 | */ |
||
| 788 | 1 | protected function _addExpression($exp, $operator) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * helper function to add condition into WHERE. |
||
| 799 | * |
||
| 800 | * @param ActiveRecordExpressions $exp The expression will be concat into WHERE or SET statment. |
||
| 801 | * @param string $operator the operator to concat this Expressions into WHERE or SET statment. |
||
| 802 | * @param string $name The Expression will contact to. |
||
| 803 | */ |
||
| 804 | 7 | protected function _addCondition($exp, $operator, $name = 'where') |
|
| 818 | |||
| 819 | /** |
||
| 820 | * @return array |
||
| 821 | */ |
||
| 822 | 1 | public function getDirty() |
|
| 826 | |||
| 827 | /** |
||
| 828 | * @return bool |
||
| 829 | */ |
||
| 830 | public static function isNewDataAreDirty() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param bool $bool |
||
| 837 | */ |
||
| 838 | 8 | public static function setNewDataAreDirty($bool) |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Magic function to SET values of the current object. |
||
| 845 | * |
||
| 846 | * @param mixed $var |
||
| 847 | * @param mixed $val |
||
| 848 | */ |
||
| 849 | 12 | public function __set($var, $val) |
|
| 877 | |||
| 878 | /** |
||
| 879 | * Magic function to UNSET values of the current object. |
||
| 880 | * |
||
| 881 | * @param mixed $var |
||
| 882 | */ |
||
| 883 | 1 | public function __unset($var) |
|
| 897 | |||
| 898 | /** |
||
| 899 | * Helper function for "GROUP BY". |
||
| 900 | * |
||
| 901 | * @param array $args |
||
| 902 | * @param null $dummy <p>only needed for API compatibility with Arrayy</p> |
||
| 903 | * |
||
| 904 | * @return $this |
||
| 905 | */ |
||
| 906 | public function group($args, $dummy = null) |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Helper function for "ORDER BY". |
||
| 915 | * |
||
| 916 | * @param $args ... |
||
| 917 | * |
||
| 918 | * @return $this |
||
| 919 | */ |
||
| 920 | 2 | public function order($args) |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Magic function to GET the values of current object. |
||
| 929 | * |
||
| 930 | * @param $var |
||
| 931 | * |
||
| 932 | * @return mixed |
||
| 933 | */ |
||
| 934 | 12 | public function &__get($var) |
|
| 950 | } |
||
| 951 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: