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 |
||
| 18 | class OrmUtils { |
||
| 19 | |||
| 20 | use OrmUtilsFieldsTrait,OrmUtilsRelationsTrait; |
||
| 21 | |||
| 22 | private static $modelsMetadatas; |
||
| 23 | |||
| 24 | public static function getModelMetadata($className) { |
||
| 30 | |||
| 31 | public static function isSerializable($class, $member) { |
||
| 34 | |||
| 35 | public static function isNullable($class, $member) { |
||
| 38 | |||
| 39 | protected static function _is($class,$member,$like){ |
||
| 46 | |||
| 47 | public static function getFieldName($class, $member) { |
||
| 54 | |||
| 55 | public static function getTableName($class) { |
||
| 59 | |||
| 60 | public static function getKeyFieldsAndValues($instance) { |
||
| 64 | |||
| 65 | public static function getMembers($className) { |
||
| 71 | |||
| 72 | public static function getMembersAndValues($instance, $members=NULL) { |
||
| 88 | |||
| 89 | public static function isNotNullOrNullAccepted($v, $className, $member) { |
||
| 93 | |||
| 94 | public static function getFirstKeyValue($instance) { |
||
| 98 | |||
| 99 | public static function getKeyValues($instance) { |
||
| 103 | |||
| 104 | public static function getMembersWithAnnotation($class, $annotation) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * |
||
| 112 | * @param object $instance |
||
| 113 | * @param string $memberKey |
||
| 114 | * @param array $array |
||
| 115 | * @return boolean |
||
| 116 | */ |
||
| 117 | public static function exists($instance, $memberKey, $array) { |
||
| 129 | |||
| 130 | public static function getAnnotationInfo($class, $keyAnnotation) { |
||
| 135 | |||
| 136 | public static function getAnnotationInfoMember($class, $keyAnnotation, $member) { |
||
| 151 | |||
| 152 | public static function setFieldToMemberNames(&$fields,$relFields){ |
||
| 159 | |||
| 160 | public static function objectAsJSON($instance){ |
||
| 165 | } |
||
| 166 |