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 |
||
| 21 | class DAO { |
||
| 22 | use DAOUpdatesTrait,DAORelationsTrait,DAOUQueries; |
||
| 23 | |||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Database |
||
| 27 | */ |
||
| 28 | public static $db; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Loads member associated with $instance by a ManyToOne relationship |
||
| 32 | * @param object $instance |
||
| 33 | * @param string $member |
||
| 34 | * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
||
| 35 | * @param boolean $useCache |
||
| 36 | */ |
||
| 37 | public static function getManyToOne($instance, $member, $included=false,$useCache=NULL) { |
||
| 38 | $fieldAnnot=OrmUtils::getMemberJoinColumns($instance, $member); |
||
| 39 | if($fieldAnnot!==null){ |
||
| 40 | $annotationArray=$fieldAnnot[1]; |
||
| 41 | $member=$annotationArray["member"]; |
||
| 42 | $value=Reflexion::getMemberValue($instance, $member); |
||
| 43 | $key=OrmUtils::getFirstKey($annotationArray["className"]); |
||
| 44 | $kv=array ($key => $value ); |
||
| 45 | $obj=self::getOne($annotationArray["className"], $kv, $included, $useCache); |
||
|
|
|||
| 46 | if ($obj !== null) { |
||
| 47 | Logger::info("DAO", "Loading the member " . $member . " for the object " . \get_class($instance),"getManyToOne"); |
||
| 48 | $accesseur="set" . ucfirst($member); |
||
| 49 | if (method_exists($instance, $accesseur)) { |
||
| 50 | $instance->$accesseur($obj); |
||
| 51 | $instance->_rest[$member]=$obj->_rest; |
||
| 52 | return $obj; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | private static function _getOneToManyFromArray(&$ret, $array, $fkv, $mappedBy) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Assign / load the child records in the $member member of $instance. |
||
| 75 | * @param object $instance |
||
| 76 | * @param string $member Member on which a oneToMany annotation must be present |
||
| 77 | * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
||
| 78 | * @param boolean $useCache |
||
| 79 | * @param array $annot used internally |
||
| 80 | */ |
||
| 81 | public static function getOneToMany($instance, $member, $included=true,$useCache=NULL, $annot=null) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param object $instance |
||
| 99 | * @param string $member |
||
| 100 | * @param array $array |
||
| 101 | * @param string $mappedBy |
||
| 102 | */ |
||
| 103 | public static function affectsOneToManyFromArray($instance, $member, $array=null, $mappedBy=null) { |
||
| 117 | |||
| 118 | private static function setToMember($member, $instance, $value, $class, $part) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Assigns / loads the child records in the $member member of $instance. |
||
| 131 | * If $ array is null, the records are loaded from the database |
||
| 132 | * @param object $instance |
||
| 133 | * @param string $member Member on which a ManyToMany annotation must be present |
||
| 134 | * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
||
| 135 | * @param array $array optional parameter containing the list of possible child records |
||
| 136 | * @param boolean $useCache |
||
| 137 | */ |
||
| 138 | public static function getManyToMany($instance, $member,$included=false,$array=null,$useCache=NULL){ |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param object $instance |
||
| 157 | * @param array $array |
||
| 158 | * @param boolean $useCache |
||
| 159 | */ |
||
| 160 | public static function affectsManyToManys($instance,$array=NULL,$useCache=NULL){ |
||
| 169 | |||
| 170 | private static function getManyToManyFromArray($instance, $array, $class, $parser) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns an array of $className objects from the database |
||
| 200 | * @param string $className class name of the model to load |
||
| 201 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 202 | * @param boolean|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
||
| 203 | * @param array|null $parameters |
||
| 204 | * @param boolean $useCache use the active cache if true |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public static function getAll($className, $condition='', $included=true,$parameters=null,$useCache=NULL) { |
||
| 210 | |||
| 211 | protected static function _getOne($className,ConditionParser $conditionParser,$included,$useCache){ |
||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $className |
||
| 224 | * @param ConditionParser $conditionParser |
||
| 225 | * @param boolean|array $included |
||
| 226 | * @param boolean $useCache |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | protected static function _getAll($className, ConditionParser $conditionParser, $included=true,$useCache=NULL) { |
||
| 259 | |||
| 260 | public static function paginate($className,$page=1,$rowsPerPage=20,$condition=null,$included=true){ |
||
| 266 | |||
| 267 | public static function getRownum($className,$ids){ |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $row |
||
| 277 | * @param string $className |
||
| 278 | * @param array $invertedJoinColumns |
||
| 279 | * @param array $oneToManyFields |
||
| 280 | * @param array $members |
||
| 281 | * @param array $oneToManyQueries |
||
| 282 | * @param array $manyToOneQueries |
||
| 283 | * @param array $manyToManyParsers |
||
| 284 | * @return object |
||
| 285 | */ |
||
| 286 | private static function loadObjectFromRow($row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields,$members,&$oneToManyQueries,&$manyToOneQueries,&$manyToManyParsers) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
||
| 319 | * @param string $className complete classname of the model to load |
||
| 320 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 321 | * @param array|null $parameters The query parameters |
||
| 322 | * @return int count of objects |
||
| 323 | */ |
||
| 324 | public static function count($className, $condition='',$parameters=null) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Returns an instance of $className from the database, from $keyvalues values of the primary key |
||
| 333 | * @param String $className complete classname of the model to load |
||
| 334 | * @param Array|string $keyValues primary key values or condition |
||
| 335 | * @param boolean|array $included if true, charges associate members with association |
||
| 336 | * @param array|null $parameters the request parameters |
||
| 337 | * @param boolean $useCache use cache if true |
||
| 338 | * @return object the instance loaded or null if not found |
||
| 339 | */ |
||
| 340 | public static function getOne($className, $keyValues, $included=true,$parameters=null,$useCache=NULL) { |
||
| 350 | |||
| 351 | private static function parseKey(&$keyValues,$className){ |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Establishes the connection to the database using the past parameters |
||
| 361 | * @param string $dbType |
||
| 362 | * @param string $dbName |
||
| 363 | * @param string $serverName |
||
| 364 | * @param string $port |
||
| 365 | * @param string $user |
||
| 366 | * @param string $password |
||
| 367 | * @param array $options |
||
| 368 | * @param boolean $cache |
||
| 369 | */ |
||
| 370 | public static function connect($dbType,$dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[],$cache=false) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns true if the connection to the database is established |
||
| 382 | * @return boolean |
||
| 383 | */ |
||
| 384 | public static function isConnected(){ |
||
| 387 | } |
||
| 388 |
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.