Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like OrmUtils 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 OrmUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class OrmUtils{ |
||
| 15 | private static $modelsMetadatas; |
||
| 16 | |||
| 17 | public static function getModelMetadata($className){ |
||
| 23 | |||
| 24 | View Code Duplication | public static function isSerializable($class,$member){ |
|
| 31 | |||
| 32 | View Code Duplication | public static function isNullable($class,$member){ |
|
| 39 | |||
| 40 | View Code Duplication | public static function getFieldName($class,$member){ |
|
| 48 | |||
| 49 | public static function getTableName($class){ |
||
| 52 | |||
| 53 | public static function getKeyFieldsAndValues($instance){ |
||
| 57 | |||
| 58 | public static function getKeyFields($instance){ |
||
| 61 | |||
| 62 | public function getMembers($className){ |
||
| 68 | |||
| 69 | public static function getMembersAndValues($instance,$members=NULL){ |
||
| 85 | |||
| 86 | public static function isNotNullOrNullAccepted($v,$className,$member){ |
||
| 90 | |||
| 91 | public static function getFirstKey($class){ |
||
| 95 | |||
| 96 | public static function getFirstKeyValue($instance){ |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param object $instance |
||
| 103 | * @return mixed[] |
||
| 104 | */ |
||
| 105 | public static function getManyToOneMembersAndValues($instance){ |
||
| 126 | |||
| 127 | public static function getMembersWithAnnotation($class,$annotation){ |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param object $instance |
||
| 135 | * @param string $memberKey |
||
| 136 | * @param array $array |
||
| 137 | * @return boolean |
||
| 138 | */ |
||
| 139 | public static function exists($instance,$memberKey,$array){ |
||
| 151 | |||
| 152 | public static function getJoinColumnName($class,$member){ |
||
| 161 | |||
| 162 | public static function getAnnotationInfo($class,$keyAnnotation){ |
||
| 167 | |||
| 168 | public static function getAnnotationInfoMember($class,$keyAnnotation,$member){ |
||
| 177 | |||
| 178 | public static function getSerializableFields($class){ |
||
| 183 | |||
| 184 | public static function getFieldsInRelations($class){ |
||
| 194 | |||
| 195 | public static function getDefaultFk($classname){ |
||
| 198 | } |
||
| 199 |