Complex classes like DAO 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 DAO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class DAO { |
||
| 21 | use DAOUpdatesTrait,DAORelationsTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Database |
||
| 25 | */ |
||
| 26 | public static $db; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Loads member associated with $instance by a ManyToOne type relationship |
||
| 30 | * @param object $instance |
||
| 31 | * @param string $member |
||
| 32 | * @param boolean $useCache |
||
| 33 | */ |
||
| 34 | public static function getManyToOne($instance, $member, $useCache=NULL) { |
||
| 55 | |||
| 56 | private static function _getOneToManyFromArray(&$ret, $array, $fkv, $mappedBy) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Assign / load the child records in the $member member of $instance. |
||
| 73 | * @param object $instance |
||
| 74 | * @param string $member Member on which a oneToMany annotation must be present |
||
| 75 | * @param boolean $useCache |
||
| 76 | * @param array $annot used internally |
||
| 77 | */ |
||
| 78 | public static function getOneToMany($instance, $member, $useCache=NULL, $annot=null) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param object $instance |
||
| 96 | * @param string $member |
||
| 97 | * @param array $array |
||
| 98 | * @param string $mappedBy |
||
| 99 | */ |
||
| 100 | public static function affectsOneToManyFromArray($instance, $member, $array=null, $mappedBy=null) { |
||
| 114 | |||
| 115 | private static function setToMember($member, $instance, $value, $class, $part) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Assigns / loads the child records in the $member member of $instance. |
||
| 128 | * If $ array is null, the records are loaded from the database |
||
| 129 | * @param object $instance |
||
| 130 | * @param string $member Member on which a ManyToMany annotation must be present |
||
| 131 | * @param array $array optional parameter containing the list of possible child records |
||
| 132 | * @param boolean $useCache |
||
| 133 | */ |
||
| 134 | public static function getManyToMany($instance, $member,$array=null,$useCache=NULL){ |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param object $instance |
||
| 153 | * @param array $array |
||
| 154 | * @param boolean $useCache |
||
| 155 | */ |
||
| 156 | public static function affectsManyToManys($instance,$array=NULL,$useCache=NULL){ |
||
| 165 | |||
| 166 | private static function getManyToManyFromArray($instance, $array, $class, $parser) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns an array of $className objects from the database |
||
| 196 | * @param string $className class name of the model to load |
||
| 197 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 198 | * @param boolean|array $included if true, charges associate members with associations, if array, example : ["client.*","commands"] |
||
| 199 | * @param boolean $useCache use the active cache if true |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public static function getAll($className, $condition='', $included=true,$useCache=NULL) { |
||
| 205 | |||
| 206 | private static function _getAll($className, ConditionParser $conditionParser, $included=true,$useCache=NULL) { |
||
| 236 | |||
| 237 | public static function paginate($className,$page=1,$rowsPerPage=20,$condition=null,$included=true){ |
||
| 243 | |||
| 244 | public static function getRownum($className,$ids){ |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param array $row |
||
| 254 | * @param string $className |
||
| 255 | * @param array $invertedJoinColumns |
||
| 256 | * @param array $oneToManyFields |
||
| 257 | * @param array $members |
||
| 258 | * @param array $oneToManyQueries |
||
| 259 | * @param array $manyToOneQueries |
||
| 260 | * @param array $manyToManyParsers |
||
| 261 | * @return object |
||
| 262 | */ |
||
| 263 | private static function loadObjectFromRow($row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields,$members,&$oneToManyQueries,&$manyToOneQueries,&$manyToManyParsers) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
||
| 296 | * @param string $className complete classname of the model to load |
||
| 297 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 298 | * @return int count of objects |
||
| 299 | */ |
||
| 300 | public static function count($className, $condition='') { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns an instance of $className from the database, from $keyvalues values of the primary key |
||
| 309 | * @param String $className complete classname of the model to load |
||
| 310 | * @param Array|string $keyValues primary key values or condition |
||
| 311 | * @param boolean|array $included if true, charges associate members with association |
||
| 312 | * @param boolean $useCache use cache if true |
||
| 313 | * @return object the instance loaded or null if not found |
||
| 314 | */ |
||
| 315 | public static function getOne($className, $keyValues, $included=true,$useCache=NULL) { |
||
| 325 | |||
| 326 | private static function parseKey(&$keyValues,$className){ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Establishes the connection to the database using the past parameters |
||
| 336 | * @param string $dbType |
||
| 337 | * @param string $dbName |
||
| 338 | * @param string $serverName |
||
| 339 | * @param string $port |
||
| 340 | * @param string $user |
||
| 341 | * @param string $password |
||
| 342 | * @param array $options |
||
| 343 | * @param boolean $cache |
||
| 344 | */ |
||
| 345 | public static function connect($dbType,$dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[],$cache=false) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns true if the connection to the database is estabished |
||
| 357 | * @return boolean |
||
| 358 | */ |
||
| 359 | public static function isConnected(){ |
||
| 362 | } |
||
| 363 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.