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 Reflexion 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 Reflexion, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Reflexion { |
||
| 15 | |||
| 16 | public static function getProperties($instance) { |
||
| 24 | |||
| 25 | public static function getMethods($instance, $filter=null) { |
||
| 30 | |||
| 31 | public static function getKeyFields($instance) { |
||
| 34 | |||
| 35 | public static function getMemberValue($instance, $member) { |
||
| 40 | |||
| 41 | public static function setMemberValue($instance, $member,$value) { |
||
| 50 | |||
| 51 | public static function getProperty($instance, $member) { |
||
| 58 | |||
| 59 | public static function getPropertiesAndValues($instance, $props=NULL) { |
||
| 76 | |||
| 77 | public static function getAnnotationClass($class, $annotation) { |
||
| 81 | |||
| 82 | public static function getAnnotationMember($class, $member, $annotation) { |
||
| 88 | |||
| 89 | public static function getAnnotationsMethod($class, $method, $annotation) { |
||
| 95 | |||
| 96 | View Code Duplication | public static function getMembersAnnotationWithAnnotation($class, $annotation) { |
|
| 106 | |||
| 107 | public static function getMembersWithAnnotation($class, $annotation) { |
||
| 117 | |||
| 118 | View Code Duplication | public static function getMembersNameWithAnnotation($class, $annotation) { |
|
| 128 | |||
| 129 | public static function isNullable($class, $member) { |
||
| 136 | |||
| 137 | public static function getDbType($class, $member) { |
||
| 144 | |||
| 145 | public static function isSerializable($class, $member) { |
||
| 151 | |||
| 152 | public static function getFieldName($class, $member) { |
||
| 160 | |||
| 161 | public static function getTableName($class) { |
||
| 173 | |||
| 174 | public static function getMethodParameters(\ReflectionMethod $method) { |
||
| 181 | } |
||
| 182 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.