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 |
||
| 19 | class DAO { |
||
| 20 | use DAOUpdatesTrait,DAORelationsTrait; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Database |
||
| 24 | */ |
||
| 25 | public static $db; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Loads member associated with $instance by a ManyToOne type relationship |
||
| 29 | * @param object $instance |
||
| 30 | * @param string $member |
||
| 31 | * @param boolean $useCache |
||
| 32 | */ |
||
| 33 | public static function getManyToOne($instance, $member, $useCache=NULL) { |
||
| 34 | $fieldAnnot=OrmUtils::getMemberJoinColumns($instance, $member); |
||
| 35 | if($fieldAnnot!==null){ |
||
| 36 | $field=$fieldAnnot[0]; |
||
| 37 | $value=Reflexion::getMemberValue($instance, $field); |
||
| 38 | $annotationArray=$fieldAnnot[1]; |
||
| 39 | $member=$annotationArray["member"]; |
||
| 40 | $key=OrmUtils::getFirstKey($annotationArray["className"]); |
||
| 41 | $kv=array ($key => $value ); |
||
| 42 | $obj=self::getOne($annotationArray["className"], $kv, false, $useCache); |
||
| 43 | if ($obj !== null) { |
||
| 44 | Logger::log("getManyToOne", "Chargement de " . $member . " pour l'objet " . \get_class($instance)); |
||
| 45 | $accesseur="set" . ucfirst($member); |
||
| 46 | if (method_exists($instance, $accesseur)) { |
||
| 47 | $instance->$accesseur($obj); |
||
| 48 | $instance->_rest[$member]=$obj->_rest; |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | private static function _getOneToManyFromArray(&$ret, $array, $fkv, $mappedBy) { |
||
| 56 | $elementAccessor="get" . ucfirst($mappedBy); |
||
| 57 | foreach ( $array as $element ) { |
||
| 58 | $elementRef=$element->$elementAccessor(); |
||
| 59 | if (!is_null($elementRef)) { |
||
| 60 | if(is_object($elementRef)) |
||
| 61 | $idElementRef=OrmUtils::getFirstKeyValue($elementRef); |
||
| 62 | else |
||
| 63 | $idElementRef=$elementRef; |
||
| 64 | if ($idElementRef == $fkv) |
||
| 65 | $ret[]=$element; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Assign / load the child records in the $member member of $instance. |
||
| 72 | * @param object $instance |
||
| 73 | * @param string $member Member on which a oneToMany annotation must be present |
||
| 74 | * @param boolean $useCache |
||
| 75 | * @param array $annot used internally |
||
| 76 | */ |
||
| 77 | public static function getOneToMany($instance, $member, $useCache=NULL, $annot=null) { |
||
| 78 | $ret=array (); |
||
| 79 | $class=get_class($instance); |
||
| 80 | if (!isset($annot)) |
||
| 81 | $annot=OrmUtils::getAnnotationInfoMember($class, "#oneToMany", $member); |
||
| 82 | if ($annot !== false) { |
||
| 83 | $fkAnnot=OrmUtils::getAnnotationInfoMember($annot["className"], "#joinColumn", $annot["mappedBy"]); |
||
| 84 | if ($fkAnnot !== false) { |
||
| 85 | $fkv=OrmUtils::getFirstKeyValue($instance); |
||
| 86 | $ret=self::getAll($annot["className"], $fkAnnot["name"] . "='" . $fkv . "'", true, $useCache); |
||
| 87 | self::setToMember($member, $instance, $ret, $class, "getOneToMany"); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | return $ret; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param object $instance |
||
| 95 | * @param string $member |
||
| 96 | * @param array $array |
||
| 97 | * @param string $mappedBy |
||
| 98 | */ |
||
| 99 | public static function affectsOneToManyFromArray($instance, $member, $array=null, $mappedBy=null) { |
||
| 113 | |||
| 114 | private static function setToMember($member, $instance, $value, $class, $part) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Assigns / loads the child records in the $member member of $instance. |
||
| 127 | * If $ array is null, the records are loaded from the database |
||
| 128 | * @param object $instance |
||
| 129 | * @param string $member Member on which a ManyToMany annotation must be present |
||
| 130 | * @param array $array optional parameter containing the list of possible child records |
||
| 131 | * @param boolean $useCache |
||
| 132 | */ |
||
| 133 | public static function getManyToMany($instance, $member,$array=null,$useCache=NULL){ |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param object $instance |
||
| 152 | * @param array $array |
||
| 153 | * @param boolean $useCache |
||
| 154 | */ |
||
| 155 | public static function affectsManyToManys($instance,$array=NULL,$useCache=NULL){ |
||
| 164 | |||
| 165 | private static function getManyToManyFromArray($instance, $array, $class, $parser) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns an array of $className objects from the database |
||
| 195 | * @param string $className class name of the model to load |
||
| 196 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 197 | * @param boolean $loadManyToOne if true, charges associate members with manyToOne association |
||
|
|
|||
| 198 | * @param boolean $loadOneToMany if true, charges associate members with oneToMany association |
||
| 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) { |
||
| 251 | |||
| 252 | public static function paginate($className,$page=1,$rowsPerPage=20,$condition=null){ |
||
| 258 | |||
| 259 | public static function getRownum($className,$ids){ |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $row |
||
| 269 | * @param string $className |
||
| 270 | * @param array $invertedJoinColumns |
||
| 271 | * @param array $oneToManyFields |
||
| 272 | * @param array $members |
||
| 273 | * @param array $oneToManyQueries |
||
| 274 | * @param array $manyToOneQueries |
||
| 275 | * @return object |
||
| 276 | */ |
||
| 277 | private static function loadObjectFromRow($row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields,$members,&$oneToManyQueries,&$manyToOneQueries,&$manyToManyParsers) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
||
| 310 | * @param string $className complete classname of the model to load |
||
| 311 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 312 | * @return int count of objects |
||
| 313 | */ |
||
| 314 | public static function count($className, $condition='') { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns an instance of $className from the database, from $keyvalues values of the primary key |
||
| 323 | * @param String $className complete classname of the model to load |
||
| 324 | * @param Array|string $keyValues primary key values or condition |
||
| 325 | * @param boolean $included if true, charges associate members with association |
||
| 326 | * @param boolean $useCache use cache if true |
||
| 327 | * @return object the instance loaded or null if not found |
||
| 328 | */ |
||
| 329 | public static function getOne($className, $keyValues, $included=true,$useCache=NULL) { |
||
| 341 | |||
| 342 | private static function parseKey(&$keyValues,$className){ |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Establishes the connection to the database using the past parameters |
||
| 352 | * @param string $dbType |
||
| 353 | * @param string $dbName |
||
| 354 | * @param string $serverName |
||
| 355 | * @param string $port |
||
| 356 | * @param string $user |
||
| 357 | * @param string $password |
||
| 358 | * @param array $options |
||
| 359 | * @param boolean $cache |
||
| 360 | */ |
||
| 361 | public static function connect($dbType,$dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[],$cache=false) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns true if the connection to the database is estabished |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | public static function isConnected(){ |
||
| 378 | } |
||
| 379 |
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.