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 |
||
| 18 | class DAO { |
||
| 19 | use DAOUpdatesTrait; |
||
| 20 | public static $db; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Loads member associated with $instance by a ManyToOne type relationship |
||
| 24 | * @param object $instance |
||
| 25 | * @param mixed $value |
||
|
|
|||
| 26 | * @param array $annotationArray |
||
| 27 | * @param boolean $useCache |
||
| 28 | */ |
||
| 29 | public static function getManyToOne($instance, $member, $useCache=NULL) { |
||
| 50 | |||
| 51 | private static function _getOneToManyFromArray(&$ret, $array, $fkv, $mappedBy) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Assign / load the child records in the $member member of $instance. |
||
| 65 | * @param object $instance |
||
| 66 | * @param string $member Member on which a oneToMany annotation must be present |
||
| 67 | * @param boolean $useCache |
||
| 68 | * @param array $annot used internally |
||
| 69 | */ |
||
| 70 | public static function getOneToMany($instance, $member, $useCache=NULL, $annot=null) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param object $instance |
||
| 88 | * @param string $member |
||
| 89 | * @param array $array |
||
| 90 | * @param string $mappedBy |
||
| 91 | */ |
||
| 92 | public static function affectsOneToManyFromArray($instance, $member, $array=null, $mappedBy=null) { |
||
| 106 | |||
| 107 | private static function setToMember($member, $instance, $value, $class, $part) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Assigns / loads the child records in the $member member of $instance. |
||
| 120 | * If $ array is null, the records are loaded from the database |
||
| 121 | * @param object $instance |
||
| 122 | * @param string $member Member on which a ManyToMany annotation must be present |
||
| 123 | * @param array $array optional parameter containing the list of possible child records |
||
| 124 | * @param boolean $useCache |
||
| 125 | */ |
||
| 126 | public static function getManyToMany($instance, $member,$array=null,$useCache=NULL){ |
||
| 142 | |||
| 143 | public static function affectsManyToManys($instance,$array=NULL,$useCache=NULL){ |
||
| 152 | |||
| 153 | private static function getManyToManyFromArray(&$ret, $instance, $array, $class, $parser) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns an array of $className objects from the database |
||
| 180 | * @param string $className class name of the model to load |
||
| 181 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 182 | * @param boolean $loadManyToOne if true, charges associate members with manyToOne association |
||
| 183 | * @param boolean $loadOneToMany if true, charges associate members with oneToMany association |
||
| 184 | * @param boolean $useCache use the active cache if true |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public static function getAll($className, $condition='', $loadManyToOne=true, $loadOneToMany=false,$useCache=NULL) { |
||
| 224 | |||
| 225 | private static function _affectsObjectsFromArray($queries,$objects,$affectsCallback,$useCache=NULL){ |
||
| 235 | |||
| 236 | private static function affectsManyToOneFromArray($object,$member,$manyToOneObjects,$fkField){ |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array $row |
||
| 246 | * @param string $className |
||
| 247 | * @param array $invertedJoinColumns |
||
| 248 | * @param array $oneToManyFields |
||
| 249 | * @param array $members |
||
| 250 | * @param array $oneToManyQueries |
||
| 251 | * @param array $manyToOneQueries |
||
| 252 | * @param boolean $useCache |
||
| 253 | * @return object |
||
| 254 | */ |
||
| 255 | private static function loadObjectFromRow($row, $className, $invertedJoinColumns, $oneToManyFields, $members,&$oneToManyQueries,&$manyToOneQueries) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Prepares members associated with $instance with a oneToMany type relationship |
||
| 282 | * @param $ret array of sql conditions |
||
| 283 | * @param object $instance |
||
| 284 | * @param string $member Member on which a OneToMany annotation must be present |
||
| 285 | * @param array $annot used internally |
||
| 286 | */ |
||
| 287 | private static function prepareOneToMany(&$ret,$instance, $member, $annot=null) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Prepares members associated with $instance with a manyToOne type relationship |
||
| 306 | * @param $ret array of sql conditions |
||
| 307 | * @param mixed $value |
||
| 308 | * @param string $fkField |
||
| 309 | * @param array $annotationArray |
||
| 310 | */ |
||
| 311 | private static function prepareManyToOne(&$ret, $value, $fkField,$annotationArray) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
||
| 323 | * @param string $className complete classname of the model to load |
||
| 324 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 325 | * @return int count of objects |
||
| 326 | */ |
||
| 327 | public static function count($className, $condition='') { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns an instance of $className from the database, from $keyvalues values of the primary key |
||
| 336 | * @param String $className complete classname of the model to load |
||
| 337 | * @param Array|string $keyValues primary key values or condition |
||
| 338 | * @param $loadManyToOne if true, charges associate members with manyToOne association |
||
| 339 | * @param $loadOneToMany if true, charges associate members with oneToMany association |
||
| 340 | * @param boolean $useCache use cache if true |
||
| 341 | * @return object the instance loaded or null if not found |
||
| 342 | */ |
||
| 343 | public static function getOne($className, $keyValues, $loadManyToOne=true, $loadOneToMany=false, $useCache=NULL) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Establishes the connection to the database using the past parameters |
||
| 359 | * @param string $dbType |
||
| 360 | * @param string $dbName |
||
| 361 | * @param string $serverName |
||
| 362 | * @param string $port |
||
| 363 | * @param string $user |
||
| 364 | * @param string $password |
||
| 365 | * @param array $options |
||
| 366 | * @param boolean $cache |
||
| 367 | */ |
||
| 368 | public static function connect($dbType,$dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[],$cache=false) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Returns true if the connection to the database is estabished |
||
| 380 | * @return boolean |
||
| 381 | */ |
||
| 382 | public static function isConnected(){ |
||
| 385 | } |
||
| 386 |
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.