Complex classes like DAORelationsTrait 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 DAORelationsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait DAORelationsTrait { |
||
| 9 | |||
| 10 | private static function _affectsRelationObjects($manyToOneQueries,$oneToManyQueries,$manyToManyParsers,$objects,$included,$useCache){ |
||
| 11 | if(\sizeof($manyToOneQueries)>0){ |
||
| 12 | self::_affectsObjectsFromArray($manyToOneQueries, $objects,$included, function($object,$member,$manyToOneObjects,$fkField){ |
||
| 13 | self::affectsManyToOneFromArray($object,$member,$manyToOneObjects,$fkField); |
||
| 14 | }); |
||
| 15 | } |
||
| 16 | |||
| 17 | if(\sizeof($oneToManyQueries)>0){ |
||
| 18 | self::_affectsObjectsFromArray($oneToManyQueries, $objects,$included, function($object,$member,$relationObjects,$fkField){ |
||
| 19 | self::affectsOneToManyFromArray($object,$member,$relationObjects,$fkField); |
||
| 20 | }); |
||
| 21 | } |
||
| 22 | |||
| 23 | if(\sizeof($manyToManyParsers)>0){ |
||
| 24 | self::_affectsManyToManyObjectsFromArray($manyToManyParsers, $objects,$included,$useCache); |
||
| 25 | } |
||
| 26 | } |
||
| 27 | |||
| 28 | private static function affectsManyToOneFromArray($object,$member,$manyToOneObjects,$fkField){ |
||
| 29 | $class=\get_class($object); |
||
| 30 | if(isset($object->$fkField)){ |
||
| 31 | $value=$manyToOneObjects[$object->$fkField]; |
||
| 32 | self::setToMember($member, $object, $value, $class, "getManyToOne"); |
||
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | private static function _affectsObjectsFromArray($queries,$objects,$included,$affectsCallback,$useCache=NULL){ |
||
| 37 | $includedNext=false; |
||
| 38 | foreach ($queries as $key=>$conditions){ |
||
| 39 | list($class,$member,$fkField)=\explode("|", $key); |
||
| 40 | if(is_array($included)){ |
||
| 41 | $includedNext=self::_getIncludedNext($included, $member); |
||
| 42 | } |
||
| 43 | $condition=\implode(" OR ", $conditions); |
||
| 44 | $relationObjects=self::getAll($class,$condition,$includedNext,$useCache); |
||
| 45 | foreach ($objects as $object){ |
||
| 46 | $affectsCallback($object, $member,$relationObjects,$fkField); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | private static function _affectsManyToManyObjectsFromArray($parsers,$objects,$included,$useCache=NULL){ |
||
| 52 | $includedNext=false; |
||
| 53 | foreach ($parsers as $key=>$parser){ |
||
| 54 | list($class,$member,$inversedBy)=\explode("|", $key); |
||
|
|
|||
| 55 | if(is_array($included)){ |
||
| 56 | $includedNext=self::_getIncludedNext($included, $member); |
||
| 57 | } |
||
| 58 | $condition=$parser->generate(); |
||
| 59 | $relationObjects=self::getAll($class,$condition,$includedNext,$useCache); |
||
| 60 | foreach ($objects as $object){ |
||
| 61 | $ret=self::getManyToManyFromArrayIds($object, $relationObjects, $member); |
||
| 62 | self::setToMember($member, $object, $ret, $class, "getManyToMany"); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | private static function _getIncludedNext($included,$member){ |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | private static function getManyToManyFromArrayIds($object, $relationObjects, $member){ |
||
| 74 | $iMember="_".$member; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Prepares members associated with $instance with a ManyToMany type relationship |
||
| 89 | * @param $ret array of sql conditions |
||
| 90 | * @param object $instance |
||
| 91 | * @param string $member Member on which a ManyToMany annotation must be present |
||
| 92 | * @param array $annot used internally |
||
| 93 | */ |
||
| 94 | private static function prepareManyToMany(&$ret,$instance, $member, $annot=null) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Prepares members associated with $instance with a oneToMany type relationship |
||
| 118 | * @param $ret array of sql conditions |
||
| 119 | * @param object $instance |
||
| 120 | * @param string $member Member on which a OneToMany annotation must be present |
||
| 121 | * @param array $annot used internally |
||
| 122 | */ |
||
| 123 | private static function prepareOneToMany(&$ret,$instance, $member, $annot=null) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Prepares members associated with $instance with a manyToOne type relationship |
||
| 142 | * @param $ret array of sql conditions |
||
| 143 | * @param mixed $value |
||
| 144 | * @param string $fkField |
||
| 145 | * @param array $annotationArray |
||
| 146 | */ |
||
| 147 | private static function prepareManyToOne(&$ret, $value, $fkField,$annotationArray) { |
||
| 156 | |||
| 157 | private static function getIncludedForStep($included){ |
||
| 176 | |||
| 177 | private static function parseEncludeMember(&$ret,$includedMember){ |
||
| 201 | |||
| 202 | private static function getInvertedJoinColumns($included,&$invertedJoinColumns){ |
||
| 210 | |||
| 211 | private static function getToManyFields($included,&$toManyFields){ |
||
| 218 | |||
| 219 | private static function _initRelationFields($included,$metaDatas,&$invertedJoinColumns,&$oneToManyFields,&$manyToManyFields){ |
||
| 241 | } |
||
| 242 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.