Complex classes like Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class Model { |
||
| 7 | /** |
||
| 8 | * Per page limit for pagination |
||
| 9 | * |
||
| 10 | * @var int |
||
| 11 | */ |
||
| 12 | public static $pageLimit = 20; |
||
| 13 | /** |
||
| 14 | * Variable that holds total pages count of last paginate() query |
||
| 15 | * |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | public static $totalPages = 0; |
||
| 19 | /** |
||
| 20 | * Models path |
||
| 21 | * |
||
| 22 | * @var modelPath |
||
| 23 | */ |
||
| 24 | protected static $modelPath; |
||
| 25 | /** |
||
| 26 | * An array that holds object data |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public $data; |
||
| 31 | /** |
||
| 32 | * Flag to define is object is new or loaded from database |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | public $isNew = true; |
||
| 37 | /** |
||
| 38 | * Return type: 'Array' to return results as array, 'Object' as object |
||
| 39 | * 'Json' as json string |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $returnType = 'Object'; |
||
| 44 | /** |
||
| 45 | * An array that holds insert/update/select errors |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | public $errors = null; |
||
| 50 | /** |
||
| 51 | * Primary key for an object. 'id' is a default value. |
||
| 52 | * |
||
| 53 | * @var stating |
||
| 54 | */ |
||
| 55 | protected $primaryKey = 'id'; |
||
| 56 | /** |
||
| 57 | * Table name for an object. Class name will be used by default |
||
| 58 | * |
||
| 59 | * @var stating |
||
| 60 | */ |
||
| 61 | protected $dbTable; |
||
| 62 | protected $dbConn = null; |
||
| 63 | protected $prefix; |
||
| 64 | /** |
||
| 65 | * Working instance of MysqliDb created earlier |
||
| 66 | * |
||
| 67 | * @var Mysql |
||
| 68 | */ |
||
| 69 | private $db; |
||
| 70 | /** |
||
| 71 | * An array that holds has* objects which should be loaded togeather with main |
||
| 72 | * object togeather with main object |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $_with = Array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param array $data Data to preload on object creation |
||
|
|
|||
| 80 | */ |
||
| 81 | public function __construct() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Helper function to create a virtual table class |
||
| 98 | * |
||
| 99 | * @param string tableName Table name |
||
| 100 | * @return dbObject |
||
| 101 | */ |
||
| 102 | public static function table($tableName) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Catches calls to undefined static methods. |
||
| 111 | * |
||
| 112 | * Transparently creating dbObject class to provide smooth API like name::get() name::orderBy()->get() |
||
| 113 | * |
||
| 114 | * @param string $method |
||
| 115 | * @param mixed $arg |
||
| 116 | * |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | public static function __callStatic($method, $arg) { |
||
| 126 | |||
| 127 | public static function autoload($path = null) { |
||
| 134 | |||
| 135 | private static function dbObjectAutoload($classname) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Magic getter function |
||
| 143 | * |
||
| 144 | * @param $name Variable name |
||
| 145 | * |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function __get($name) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Magic setter function |
||
| 185 | * |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function __set($name, $value) { |
||
| 194 | |||
| 195 | public function __isset($name) { |
||
| 202 | |||
| 203 | public function __unset($name) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Save or Update object |
||
| 209 | * |
||
| 210 | * @return mixed insert id or false in case of failure |
||
| 211 | */ |
||
| 212 | public function save($data = null) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return mixed insert id or false in case of failure |
||
| 220 | */ |
||
| 221 | public function insert() { |
||
| 235 | |||
| 236 | private function prepareData() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $data |
||
| 277 | */ |
||
| 278 | private function validate($data) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param array $data Optional update data to apply to the object |
||
| 338 | */ |
||
| 339 | public function update($data = null) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Delete method. Works only if object primaryKey is defined |
||
| 364 | * |
||
| 365 | * @return boolean Indicates success. 0 or 1. |
||
| 366 | */ |
||
| 367 | public function delete() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * 当执行一个自身不存在的方法时,重定向到mysql类中去 |
||
| 377 | * |
||
| 378 | * @param string $method |
||
| 379 | * @param mixed $arg |
||
| 380 | * |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | public function __call($method, $arg) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Converts object data to a JSON string. |
||
| 391 | * |
||
| 392 | * @return string Converted data |
||
| 393 | */ |
||
| 394 | public function __toString() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Converts object data to a JSON string. |
||
| 400 | * |
||
| 401 | * @return string Converted data |
||
| 402 | */ |
||
| 403 | public function toJson() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Converts object data to an associative array. |
||
| 409 | * |
||
| 410 | * @return array Converted data |
||
| 411 | */ |
||
| 412 | public function toArray() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Function queries hasMany relations if needed and also converts hasOne object names |
||
| 424 | * |
||
| 425 | * @param array $data |
||
| 426 | */ |
||
| 427 | private function processAllWith(&$data, $shouldReset = true) { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Fetch all objects |
||
| 465 | * |
||
| 466 | * @access public |
||
| 467 | * @param integer|array $limit Array to define SQL limit in format Array ($count, $offset) |
||
| 468 | * or only $count |
||
| 469 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
| 470 | * |
||
| 471 | * @return array Array of dbObjects |
||
| 472 | */ |
||
| 473 | protected function get($limit = null, $fields = null) { |
||
| 499 | |||
| 500 | private function processHasOneWith() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Function to join object with another object. |
||
| 518 | * |
||
| 519 | * @access public |
||
| 520 | * @param string $objectName Object Name |
||
| 521 | * @param string $key Key for a join from primary object |
||
| 522 | * @param string $joinType SQL join type: LEFT, RIGHT, INNER, OUTER |
||
| 523 | * @param string $primaryKey SQL join On Second primaryKey |
||
| 524 | * |
||
| 525 | * @return dbObject |
||
| 526 | */ |
||
| 527 | private function join($objectName, $key = null, $joinType = 'LEFT', $primaryKey = null) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param array $data |
||
| 546 | */ |
||
| 547 | private function processArrays(&$data) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Function to get a total records count |
||
| 561 | * |
||
| 562 | * @return int |
||
| 563 | */ |
||
| 564 | protected function count() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Helper function to create dbObject with Json return type |
||
| 573 | * |
||
| 574 | * @return dbObject |
||
| 575 | */ |
||
| 576 | private function JsonBuilder() { |
||
| 580 | |||
| 581 | /* |
||
| 582 | * Function building hasOne joins for get/getOne method |
||
| 583 | */ |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Helper function to create dbObject with Array return type |
||
| 587 | * |
||
| 588 | * @return dbObject |
||
| 589 | */ |
||
| 590 | private function ArrayBuilder() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Helper function to create dbObject with Object return type. |
||
| 597 | * Added for consistency. Works same way as new $objname () |
||
| 598 | * |
||
| 599 | * @return dbObject |
||
| 600 | */ |
||
| 601 | private function ObjectBuilder() { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get object by primary key. |
||
| 608 | * |
||
| 609 | * @access public |
||
| 610 | * @param $id Primary Key |
||
| 611 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
| 612 | * |
||
| 613 | * @return dbObject|array |
||
| 614 | */ |
||
| 615 | private function byId($id, $fields = null) { |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Convinient function to fetch one object. Mostly will be togeather with where() |
||
| 622 | * |
||
| 623 | * @access public |
||
| 624 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
| 625 | * |
||
| 626 | * @return dbObject |
||
| 627 | */ |
||
| 628 | protected function getOne($fields = null) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Function to set witch hasOne or hasMany objects should be loaded togeather with a main object |
||
| 650 | * |
||
| 651 | * @access public |
||
| 652 | * @param string $objectName Object Name |
||
| 653 | * |
||
| 654 | * @return dbObject |
||
| 655 | */ |
||
| 656 | private function with($objectName) { |
||
| 664 | |||
| 665 | /* |
||
| 666 | * Enable models autoload from a specified path |
||
| 667 | * |
||
| 668 | * Calling autoload() without path will set path to dbObjectPath/models/ directory |
||
| 669 | * |
||
| 670 | * @param string $path |
||
| 671 | */ |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Pagination wraper to get() |
||
| 675 | * |
||
| 676 | * @access public |
||
| 677 | * @param int $page Page number |
||
| 678 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
| 679 | * @return array |
||
| 680 | */ |
||
| 681 | private function paginate($page, $fields = null) { |
||
| 706 | } |
||
| 707 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.