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 | /** |
||
| 45 | * @var DB static property to connect database. |
||
| 46 | */ |
||
| 47 | public static $db; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array maping the function name and the operator, to build Expressions in WHERE condition. |
||
| 51 | * <pre>user can call it like this: |
||
| 52 | * $user->isnotnull()->eq('id', 1); |
||
| 53 | * will create Expressions can explain to SQL: |
||
| 54 | * WHERE user.id IS NOT NULL AND user.id = :ph1</pre> |
||
| 55 | */ |
||
| 56 | public static $operators = array( |
||
| 57 | 'equal' => '=', |
||
| 58 | 'eq' => '=', |
||
| 59 | 'notequal' => '<>', |
||
| 60 | 'ne' => '<>', |
||
| 61 | 'greaterthan' => '>', |
||
| 62 | 'gt' => '>', |
||
| 63 | 'lessthan' => '<', |
||
| 64 | 'lt' => '<', |
||
| 65 | 'greaterthanorequal' => '>=', |
||
| 66 | 'ge' => '>=', |
||
| 67 | 'gte' => '>=', |
||
| 68 | 'lessthanorequal' => '<=', |
||
| 69 | 'le' => '<=', |
||
| 70 | 'lte' => '<=', |
||
| 71 | 'between' => 'BETWEEN', |
||
| 72 | 'like' => 'LIKE', |
||
| 73 | 'in' => 'IN', |
||
| 74 | 'notin' => 'NOT IN', |
||
| 75 | 'isnull' => 'IS NULL', |
||
| 76 | 'isnotnull' => 'IS NOT NULL', |
||
| 77 | 'notnull' => 'IS NOT NULL', |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array Part of SQL, maping the function name and the operator to build SQL Part. |
||
| 82 | * <pre>call function like this: |
||
| 83 | * $user->order('id desc', 'name asc')->limit(2,1); |
||
| 84 | * can explain to SQL: |
||
| 85 | * ORDER BY id desc, name asc limit 2,1</pre> |
||
| 86 | */ |
||
| 87 | public static $sqlParts = array( |
||
| 88 | 'select' => 'SELECT', |
||
| 89 | 'from' => 'FROM', |
||
| 90 | 'set' => 'SET', |
||
| 91 | 'where' => 'WHERE', |
||
| 92 | 'group' => 'GROUP BY', |
||
| 93 | 'groupby' => 'GROUP BY', |
||
| 94 | 'having' => 'HAVING', |
||
| 95 | 'order' => 'ORDER BY', |
||
| 96 | 'orderby' => 'ORDER BY', |
||
| 97 | 'limit' => 'limit', |
||
| 98 | 'top' => 'TOP', |
||
| 99 | ); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array Static property to stored the default Sql Expressions values. |
||
| 103 | */ |
||
| 104 | public static $defaultSqlExpressions = array( |
||
| 105 | 'expressions' => array(), |
||
| 106 | 'wrap' => false, |
||
| 107 | 'select' => null, |
||
| 108 | 'insert' => null, |
||
| 109 | 'update' => null, |
||
| 110 | 'set' => null, |
||
| 111 | 'delete' => 'DELETE ', |
||
| 112 | 'join' => null, |
||
| 113 | 'from' => null, |
||
| 114 | 'values' => null, |
||
| 115 | 'where' => null, |
||
| 116 | 'having' => null, |
||
| 117 | 'limit' => null, |
||
| 118 | 'order' => null, |
||
| 119 | 'group' => null, |
||
| 120 | ); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array Stored the Expressions of the SQL. |
||
| 124 | */ |
||
| 125 | protected $sqlExpressions = array(); |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string The table name in database. |
||
| 129 | */ |
||
| 130 | public $table; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var string The primary key of this ActiveRecord, just suport single primary key. |
||
| 134 | */ |
||
| 135 | public $primaryKey = 'id'; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @var array Stored the drity data of this object, when call "insert" or "update" function, will write this data |
||
| 139 | * into database. |
||
| 140 | */ |
||
| 141 | public $dirty = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var array Stored the params will bind to SQL when call PDOStatement::execute(), |
||
| 145 | */ |
||
| 146 | public $params = array(); |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var Arrayy[] Stored the configure of the relation, or target of the relation. |
||
| 150 | */ |
||
| 151 | public $relations = array(); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var int The count of bind params, using this count and const "PREFIX" (:ph) to generate place holder in SQL. |
||
| 155 | */ |
||
| 156 | public static $count = 0; |
||
| 157 | |||
| 158 | const BELONGS_TO = 'belongs_to'; |
||
| 159 | const HAS_MANY = 'has_many'; |
||
| 160 | const HAS_ONE = 'has_one'; |
||
| 161 | |||
| 162 | const PREFIX = ':ph'; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Function to reset the $params and $sqlExpressions. |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function reset() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * function to SET or RESET the dirty data. |
||
| 179 | * |
||
| 180 | * @param array $dirty The dirty data will be set, or empty array to reset the dirty data. |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function dirty(array $dirty = array()) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * set the DB connection. |
||
| 193 | * |
||
| 194 | * @param DB $db |
||
| 195 | */ |
||
| 196 | public static function setDb($db) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * function to find one record and assign in to current object. |
||
| 203 | * |
||
| 204 | * @param int $id If call this function using this param, will find record by using this id. If not set, just find |
||
| 205 | * the first record in database. |
||
| 206 | * |
||
| 207 | * @return bool|ActiveRecord if find record, assign in to current object and return it, other wise return "false". |
||
| 208 | */ |
||
| 209 | public function fetch($id = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Function to find all records in database. |
||
| 236 | * |
||
| 237 | * @return array return array of ActiveRecord |
||
| 238 | */ |
||
| 239 | public function fetchAll() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Function to delete current record in database. |
||
| 261 | * |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | public function delete() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Function to build update SQL, and update current record in database, just write the dirty data into database. |
||
| 280 | * |
||
| 281 | * @return bool|ActiveRecord if update success return current object, other wise return false. |
||
| 282 | */ |
||
| 283 | public function update() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Function to build insert SQL, and insert current record into database. |
||
| 311 | * |
||
| 312 | * @return bool|ActiveRecord if insert success return current object, other wise return false. |
||
| 313 | */ |
||
| 314 | public function insert() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Helper function to exec sql. |
||
| 350 | * |
||
| 351 | * @param string $sql The SQL need to be execute. |
||
| 352 | * @param array $param The param will be bind to the sql statement. |
||
| 353 | * |
||
| 354 | * @return bool|int|Result <p> |
||
| 355 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 356 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 357 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 358 | * "true" by e.g. "DROP"-queries<br /> |
||
| 359 | * "false" on error |
||
| 360 | * </p> |
||
| 361 | */ |
||
| 362 | public static function execute($sql, array $param = array()) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Helper function to query one record by sql and params. |
||
| 373 | * |
||
| 374 | * @param string $sql The SQL to find record. |
||
| 375 | * @param array $param The param will be bind to PDOStatement. |
||
| 376 | * @param ActiveRecord $obj The object, if find record in database, will assign the attributes in to this object. |
||
| 377 | * @param bool $single If set to true, will find record and fetch in current object, otherwise will find all |
||
| 378 | * records. |
||
| 379 | * |
||
| 380 | * @return bool|ActiveRecord|array |
||
| 381 | */ |
||
| 382 | public static function _query($sql, array $param = array(), $obj = null, $single = false) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Helper function to get relation of this object. |
||
| 405 | * There was three types of relations: {BELONGS_TO, HAS_ONE, HAS_MANY} |
||
| 406 | * |
||
| 407 | * @param string $name The name of the relation, the array key when defind the relation. |
||
| 408 | * |
||
| 409 | * @return mixed |
||
| 410 | * |
||
| 411 | * @throws \Exception |
||
| 412 | */ |
||
| 413 | protected function &getRelation($name) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Helper function to build SQL with sql parts. |
||
| 485 | * |
||
| 486 | * @param string $n The SQL part will be build. |
||
| 487 | * @param int $i The index of $n in $sql array. |
||
| 488 | * @param ActiveRecord $o The reference to $this |
||
| 489 | */ |
||
| 490 | private function _buildSqlCallback(&$n, $i, $o) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Helper function to build SQL with sql parts. |
||
| 505 | * |
||
| 506 | * @param array $sqls The SQL part will be build. |
||
| 507 | * |
||
| 508 | * @return string |
||
| 509 | */ |
||
| 510 | protected function _buildSql($sqls = array()) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Magic function to make calls witch in function mapping stored in $operators and $sqlPart. |
||
| 522 | * also can call function of PDO object. |
||
| 523 | * |
||
| 524 | * @param string $name function name |
||
| 525 | * @param array $args The arguments of the function. |
||
| 526 | * |
||
| 527 | * @return $this|mixed Return the result of callback or the current object to make chain method calls. |
||
| 528 | * |
||
| 529 | * @throws \Exception |
||
| 530 | */ |
||
| 531 | public function __call($name, $args) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Make wrap when build the SQL expressions of WHERE. |
||
| 560 | * |
||
| 561 | * @param string $op If give this param will build one WrapExpressions include the stored expressions add into WHERE. |
||
| 562 | * otherwise wil stored the expressions into array. |
||
| 563 | * |
||
| 564 | * @return $this |
||
| 565 | */ |
||
| 566 | public function wrap($op = null) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Helper function to build place holder when make SQL expressions. |
||
| 590 | * |
||
| 591 | * @param mixed $value The value will bind to SQL, just store it in $this->params. |
||
| 592 | * |
||
| 593 | * @return mixed $value |
||
| 594 | */ |
||
| 595 | protected function _filterParam($value) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Helper function to add condition into WHERE. |
||
| 611 | * create the SQL Expressions. |
||
| 612 | * |
||
| 613 | * @param string $field The field name, the source of Expressions |
||
| 614 | * @param string $operator |
||
| 615 | * @param mixed $value The target of the Expressions |
||
| 616 | * @param string $op The operator to concat this Expressions into WHERE or SET statement. |
||
| 617 | * @param string $name The Expression will contact to. |
||
| 618 | */ |
||
| 619 | public function addCondition($field, $operator, $value, $op = 'AND', $name = 'where') |
||
| 642 | |||
| 643 | /** |
||
| 644 | * helper function to add condition into JOIN. |
||
| 645 | * create the SQL Expressions. |
||
| 646 | * |
||
| 647 | * @param string $table The join table name |
||
| 648 | * @param string $on The condition of ON |
||
| 649 | * @param string $type The join type, like "LEFT", "INNER", "OUTER" |
||
| 650 | * |
||
| 651 | * @return $this |
||
| 652 | */ |
||
| 653 | public function join($table, $on, $type = 'LEFT') |
||
| 667 | |||
| 668 | /** |
||
| 669 | * helper function to make wrapper. Stored the expression in to array. |
||
| 670 | * |
||
| 671 | * @param Expressions $exp The expression will be stored. |
||
| 672 | * @param string $operator The operator to concat this Expressions into WHERE statment. |
||
| 673 | */ |
||
| 674 | protected function _addExpression($exp, $operator) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * helper function to add condition into WHERE. |
||
| 685 | * |
||
| 686 | * @param Expressions $exp The expression will be concat into WHERE or SET statment. |
||
| 687 | * @param string $operator the operator to concat this Expressions into WHERE or SET statment. |
||
| 688 | * @param string $name The Expression will contact to. |
||
| 689 | */ |
||
| 690 | protected function _addCondition($exp, $operator, $name = 'where') |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Magic function to SET values of the current object. |
||
| 707 | * |
||
| 708 | * @param mixed $var |
||
| 709 | * @param mixed $val |
||
| 710 | */ |
||
| 711 | public function __set($var, $val) |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Magic function to UNSET values of the current object. |
||
| 738 | * |
||
| 739 | * @param mixed $var |
||
| 740 | */ |
||
| 741 | public function __unset($var) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Magic function to GET the values of current object. |
||
| 758 | * |
||
| 759 | * @param $var |
||
| 760 | * |
||
| 761 | * @return mixed |
||
| 762 | */ |
||
| 763 | public function &__get($var) |
||
| 779 | } |
||
| 780 |
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: