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|array $included if true, loads associate members with associations, if array, example : ["client.*","commands"] |
||
| 33 | * @param boolean $useCache |
||
| 34 | */ |
||
| 35 | public static function getManyToOne($instance, $member, $included=false,$useCache=NULL) { |
||
| 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 boolean $useCache use the active cache if true |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | public static function getAll($className, $condition='', $included=true,$useCache=NULL) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $className |
||
| 212 | * @param ConditionParser $conditionParser |
||
| 213 | * @param boolean|array $included |
||
| 214 | * @param boolean $useCache |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | protected static function _getAll($className, ConditionParser $conditionParser, $included=true,$useCache=NULL) { |
||
| 247 | |||
| 248 | public static function paginate($className,$page=1,$rowsPerPage=20,$condition=null,$included=true){ |
||
| 254 | |||
| 255 | public static function getRownum($className,$ids){ |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param array $row |
||
| 265 | * @param string $className |
||
| 266 | * @param array $invertedJoinColumns |
||
| 267 | * @param array $oneToManyFields |
||
| 268 | * @param array $members |
||
| 269 | * @param array $oneToManyQueries |
||
| 270 | * @param array $manyToOneQueries |
||
| 271 | * @param array $manyToManyParsers |
||
| 272 | * @return object |
||
| 273 | */ |
||
| 274 | private static function loadObjectFromRow($row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields,$members,&$oneToManyQueries,&$manyToOneQueries,&$manyToManyParsers) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the number of objects of $className from the database respecting the condition possibly passed as parameter |
||
| 307 | * @param string $className complete classname of the model to load |
||
| 308 | * @param string $condition Part following the WHERE of an SQL statement |
||
| 309 | * @return int count of objects |
||
| 310 | */ |
||
| 311 | public static function count($className, $condition='') { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns an instance of $className from the database, from $keyvalues values of the primary key |
||
| 320 | * @param String $className complete classname of the model to load |
||
| 321 | * @param Array|string $keyValues primary key values or condition |
||
| 322 | * @param boolean|array $included if true, charges associate members with association |
||
| 323 | * @param boolean $useCache use cache if true |
||
| 324 | * @return object the instance loaded or null if not found |
||
| 325 | */ |
||
| 326 | public static function getOne($className, $keyValues, $included=true,$useCache=NULL) { |
||
| 336 | |||
| 337 | private static function parseKey(&$keyValues,$className){ |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Establishes the connection to the database using the past parameters |
||
| 347 | * @param string $dbType |
||
| 348 | * @param string $dbName |
||
| 349 | * @param string $serverName |
||
| 350 | * @param string $port |
||
| 351 | * @param string $user |
||
| 352 | * @param string $password |
||
| 353 | * @param array $options |
||
| 354 | * @param boolean $cache |
||
| 355 | */ |
||
| 356 | public static function connect($dbType,$dbName, $serverName="127.0.0.1", $port="3306", $user="root", $password="", $options=[],$cache=false) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns true if the connection to the database is estabished |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | public static function isConnected(){ |
||
| 373 | } |
||
| 374 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.