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) { |
||
389 | |||
390 | /** |
||
391 | * Converts object data to a JSON string. |
||
392 | * |
||
393 | * @return string Converted data |
||
394 | */ |
||
395 | public function __toString() { |
||
398 | |||
399 | /** |
||
400 | * Converts object data to a JSON string. |
||
401 | * |
||
402 | * @return string Converted data |
||
403 | */ |
||
404 | public function toJson() { |
||
407 | |||
408 | /** |
||
409 | * Converts object data to an associative array. |
||
410 | * |
||
411 | * @return array Converted data |
||
412 | */ |
||
413 | public function toArray() { |
||
422 | |||
423 | /** |
||
424 | * Function queries hasMany relations if needed and also converts hasOne object names |
||
425 | * |
||
426 | * @param array $data |
||
427 | */ |
||
428 | private function processAllWith(&$data, $shouldReset = true) { |
||
463 | |||
464 | /** |
||
465 | * Fetch all objects |
||
466 | * |
||
467 | * @access public |
||
468 | * @param integer|array $limit Array to define SQL limit in format Array ($count, $offset) |
||
469 | * or only $count |
||
470 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
471 | * |
||
472 | * @return array Array of dbObjects |
||
473 | */ |
||
474 | protected function get($limit = null, $fields = null) { |
||
500 | |||
501 | private function processHasOneWith() { |
||
516 | |||
517 | /** |
||
518 | * Function to join object with another object. |
||
519 | * |
||
520 | * @access public |
||
521 | * @param string $objectName Object Name |
||
522 | * @param string $key Key for a join from primary object |
||
523 | * @param string $joinType SQL join type: LEFT, RIGHT, INNER, OUTER |
||
524 | * @param string $primaryKey SQL join On Second primaryKey |
||
525 | * |
||
526 | * @return dbObject |
||
527 | */ |
||
528 | private function join($objectName, $key = null, $joinType = 'LEFT', $primaryKey = null) { |
||
544 | |||
545 | /** |
||
546 | * @param array $data |
||
547 | */ |
||
548 | private function processArrays(&$data) { |
||
559 | |||
560 | /** |
||
561 | * Function to get a total records count |
||
562 | * |
||
563 | * @return int |
||
564 | */ |
||
565 | protected function count() { |
||
571 | |||
572 | /** |
||
573 | * Helper function to create dbObject with Json return type |
||
574 | * |
||
575 | * @return dbObject |
||
576 | */ |
||
577 | private function JsonBuilder() { |
||
581 | |||
582 | /* |
||
583 | * Function building hasOne joins for get/getOne method |
||
584 | */ |
||
585 | |||
586 | /** |
||
587 | * Helper function to create dbObject with Array return type |
||
588 | * |
||
589 | * @return dbObject |
||
590 | */ |
||
591 | private function ArrayBuilder() { |
||
595 | |||
596 | /** |
||
597 | * Helper function to create dbObject with Object return type. |
||
598 | * Added for consistency. Works same way as new $objname () |
||
599 | * |
||
600 | * @return dbObject |
||
601 | */ |
||
602 | private function ObjectBuilder() { |
||
606 | |||
607 | /** |
||
608 | * Get object by primary key. |
||
609 | * |
||
610 | * @access public |
||
611 | * @param $id Primary Key |
||
612 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
613 | * |
||
614 | * @return dbObject|array |
||
615 | */ |
||
616 | private function byId($id, $fields = null) { |
||
620 | |||
621 | /** |
||
622 | * Convinient function to fetch one object. Mostly will be togeather with where() |
||
623 | * |
||
624 | * @access public |
||
625 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
626 | * |
||
627 | * @return dbObject |
||
628 | */ |
||
629 | protected function getOne($fields = null) { |
||
648 | |||
649 | /** |
||
650 | * Function to set witch hasOne or hasMany objects should be loaded togeather with a main object |
||
651 | * |
||
652 | * @access public |
||
653 | * @param string $objectName Object Name |
||
654 | * |
||
655 | * @return dbObject |
||
656 | */ |
||
657 | private function with($objectName) { |
||
665 | |||
666 | /* |
||
667 | * Enable models autoload from a specified path |
||
668 | * |
||
669 | * Calling autoload() without path will set path to dbObjectPath/models/ directory |
||
670 | * |
||
671 | * @param string $path |
||
672 | */ |
||
673 | |||
674 | /** |
||
675 | * Pagination wraper to get() |
||
676 | * |
||
677 | * @access public |
||
678 | * @param int $page Page number |
||
679 | * @param array|string $fields Array or coma separated list of fields to fetch |
||
680 | * @return array |
||
681 | */ |
||
682 | private function paginate($page, $fields = null) { |
||
707 | |||
708 | public function clear() { |
||
712 | } |
||
713 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.