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 |
||
| 15 | class OrmUtils { |
||
| 16 | private static $modelsMetadatas; |
||
| 17 | |||
| 18 | public static function getModelMetadata($className) { |
||
| 24 | |||
| 25 | View Code Duplication | public static function isSerializable($class, $member) { |
|
| 32 | |||
| 33 | View Code Duplication | public static function isNullable($class, $member) { |
|
| 40 | |||
| 41 | View Code Duplication | public static function getFieldName($class, $member) { |
|
| 49 | |||
| 50 | public static function getTableName($class) { |
||
| 53 | |||
| 54 | public static function getKeyFieldsAndValues($instance) { |
||
| 58 | |||
| 59 | public static function getKeyFields($instance) { |
||
| 65 | |||
| 66 | public static function getMembers($className) { |
||
| 72 | |||
| 73 | public static function getFieldTypes($className) { |
||
| 79 | |||
| 80 | public static function getMembersAndValues($instance, $members=NULL) { |
||
| 96 | |||
| 97 | public static function isNotNullOrNullAccepted($v, $className, $member) { |
||
| 101 | |||
| 102 | public static function getFirstKey($class) { |
||
| 106 | |||
| 107 | public static function getFirstKeyValue($instance) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * |
||
| 114 | * @param object $instance |
||
| 115 | * @return mixed[] |
||
| 116 | */ |
||
| 117 | public static function getManyToOneMembersAndValues($instance) { |
||
| 138 | |||
| 139 | public static function getMembersWithAnnotation($class, $annotation) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * |
||
| 147 | * @param object $instance |
||
| 148 | * @param string $memberKey |
||
| 149 | * @param array $array |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | public static function exists($instance, $memberKey, $array) { |
||
| 164 | |||
| 165 | public static function getJoinColumnName($class, $member) { |
||
| 174 | |||
| 175 | public static function getAnnotationInfo($class, $keyAnnotation) { |
||
| 180 | |||
| 181 | public static function getAnnotationInfoMember($class, $keyAnnotation, $member) { |
||
| 196 | |||
| 197 | public static function getSerializableFields($class) { |
||
| 202 | |||
| 203 | public static function getFieldsInRelations($class) { |
||
| 216 | |||
| 217 | public static function getDefaultFk($classname) { |
||
| 220 | } |
||
| 221 |