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 PDOStatement::execute(), |
||
| 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 | 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 | public function reset() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Reset the dirty data. |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | 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, other wise return "false". |
||
| 241 | */ |
||
| 242 | 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 | public function fetchAll() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Function to delete current record in database. |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | 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|ActiveRecord if update success return current object, other wise return false. |
||
| 335 | */ |
||
| 336 | public function update() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Function to build insert SQL, and insert current record into database. |
||
| 365 | * |
||
| 366 | * @return bool|ActiveRecord if insert success return current object, other wise return false. |
||
| 367 | */ |
||
| 368 | public function insert() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Helper function to exec sql. |
||
| 404 | * |
||
| 405 | * @param string $sql The SQL need to be execute. |
||
| 406 | * @param array $param The param will be bind to the sql statement. |
||
| 407 | * |
||
| 408 | * @return bool|int|Result <p> |
||
| 409 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 410 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 411 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 412 | * "true" by e.g. "DROP"-queries<br /> |
||
| 413 | * "false" on error |
||
| 414 | * </p> |
||
| 415 | */ |
||
| 416 | public static function execute($sql, array $param = array()) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Helper function to query one record by sql and params. |
||
| 427 | * |
||
| 428 | * @param string $sql The SQL to find record. |
||
| 429 | * @param array $param The param will be bind to PDOStatement. |
||
| 430 | * @param ActiveRecord $obj The object, if find record in database, will assign the attributes in to this object. |
||
| 431 | * @param bool $single If set to true, will find record and fetch in current object, otherwise will find all |
||
| 432 | * records. |
||
| 433 | * |
||
| 434 | * @return bool|ActiveRecord|array |
||
| 435 | */ |
||
| 436 | public static function _query($sql, array $param = array(), $obj = null, $single = false) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Helper function to get relation of this object. |
||
| 466 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 467 | * |
||
| 468 | * @param string $name The name of the relation, the array key when defind the relation. |
||
| 469 | * |
||
| 470 | * @return mixed |
||
| 471 | * |
||
| 472 | * @throws ActiveRecordException <p>If the relation can't be found .</p> |
||
| 473 | */ |
||
| 474 | protected function &getRelation($name) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Helper function to build SQL with sql parts. |
||
| 546 | * |
||
| 547 | * @param string $n The SQL part will be build. |
||
| 548 | * @param int $i The index of $n in $sql array. |
||
| 549 | * @param ActiveRecord $o The reference to $this |
||
| 550 | */ |
||
| 551 | private function _buildSqlCallback(&$n, $i, $o) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Helper function to build SQL with sql parts. |
||
| 586 | * |
||
| 587 | * @param array $sqls The SQL part will be build. |
||
| 588 | * |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | protected function _buildSql($sqls = array()) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Magic function to make calls witch in function mapping stored in $operators and $sqlPart. |
||
| 603 | * also can call function of PDO object. |
||
| 604 | * |
||
| 605 | * @param string $name function name |
||
| 606 | * @param array $args The arguments of the function. |
||
| 607 | * |
||
| 608 | * @return $this|mixed Return the result of callback or the current object to make chain method calls. |
||
| 609 | * |
||
| 610 | * @throws ActiveRecordException |
||
| 611 | */ |
||
| 612 | public function __call($name, $args) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Make wrap when build the SQL expressions of WHERE. |
||
| 653 | * |
||
| 654 | * @param string $op If give this param will build one WrapExpressions include the stored expressions add into WHERE. |
||
| 655 | * otherwise wil stored the expressions into array. |
||
| 656 | * |
||
| 657 | * @return $this |
||
| 658 | */ |
||
| 659 | public function wrap($op = null) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Helper function to build place holder when make SQL expressions. |
||
| 683 | * |
||
| 684 | * @param mixed $value The value will bind to SQL, just store it in $this->params. |
||
| 685 | * |
||
| 686 | * @return mixed $value |
||
| 687 | */ |
||
| 688 | protected function _filterParam($value) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Helper function to add condition into WHERE. |
||
| 704 | * create the SQL Expressions. |
||
| 705 | * |
||
| 706 | * @param string $field The field name, the source of Expressions |
||
| 707 | * @param string $operator |
||
| 708 | * @param mixed $value The target of the Expressions |
||
| 709 | * @param string $op The operator to concat this Expressions into WHERE or SET statement. |
||
| 710 | * @param string $name The Expression will contact to. |
||
| 711 | */ |
||
| 712 | public function addCondition($field, $operator, $value, $op = 'AND', $name = 'where') |
||
| 735 | |||
| 736 | /** |
||
| 737 | * helper function to add condition into JOIN. |
||
| 738 | * create the SQL Expressions. |
||
| 739 | * |
||
| 740 | * @param string $table The join table name |
||
| 741 | * @param string $on The condition of ON |
||
| 742 | * @param string $type The join type, like "LEFT", "INNER", "OUTER" |
||
| 743 | * |
||
| 744 | * @return $this |
||
| 745 | */ |
||
| 746 | public function join($table, $on, $type = 'LEFT') |
||
| 760 | |||
| 761 | /** |
||
| 762 | * helper function to make wrapper. Stored the expression in to array. |
||
| 763 | * |
||
| 764 | * @param ActiveRecordExpressions $exp The expression will be stored. |
||
| 765 | * @param string $operator The operator to concat this Expressions into WHERE statment. |
||
| 766 | */ |
||
| 767 | protected function _addExpression($exp, $operator) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * helper function to add condition into WHERE. |
||
| 778 | * |
||
| 779 | * @param ActiveRecordExpressions $exp The expression will be concat into WHERE or SET statment. |
||
| 780 | * @param string $operator the operator to concat this Expressions into WHERE or SET statment. |
||
| 781 | * @param string $name The Expression will contact to. |
||
| 782 | */ |
||
| 783 | protected function _addCondition($exp, $operator, $name = 'where') |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @return array |
||
| 800 | */ |
||
| 801 | public function getDirty() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @return bool |
||
| 808 | */ |
||
| 809 | public static function isNewDataAreDirty() |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @param bool $bool |
||
| 816 | */ |
||
| 817 | public static function setNewDataAreDirty($bool) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Magic function to SET values of the current object. |
||
| 824 | * |
||
| 825 | * @param mixed $var |
||
| 826 | * @param mixed $val |
||
| 827 | */ |
||
| 828 | public function __set($var, $val) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Magic function to UNSET values of the current object. |
||
| 859 | * |
||
| 860 | * @param mixed $var |
||
| 861 | */ |
||
| 862 | public function __unset($var) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Helper function for "GROUP BY". |
||
| 879 | * |
||
| 880 | * @param array $args |
||
| 881 | * @param null $dummy <p>only needed for API compatibility with Arrayy</p> |
||
| 882 | * |
||
| 883 | * @return $this |
||
| 884 | */ |
||
| 885 | public function group($args, $dummy = null) |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Helper function for "ORDER BY". |
||
| 894 | * |
||
| 895 | * @param $args ... |
||
| 896 | * |
||
| 897 | * @return $this |
||
| 898 | */ |
||
| 899 | public function order($args) |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Magic function to GET the values of current object. |
||
| 908 | * |
||
| 909 | * @param $var |
||
| 910 | * |
||
| 911 | * @return mixed |
||
| 912 | */ |
||
| 913 | public function &__get($var) |
||
| 929 | } |
||
| 930 |
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: