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 CaseExpression 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 CaseExpression, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class CaseExpression extends Component |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * The value to be compared |
||
31 | * |
||
32 | * @var Expression |
||
33 | */ |
||
34 | public $value; |
||
35 | |||
36 | /** |
||
37 | * The conditions in WHEN clauses |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | public $conditions; |
||
42 | |||
43 | /** |
||
44 | * The results matching with the WHEN clauses |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | public $results; |
||
49 | |||
50 | /** |
||
51 | * The values to be compared against |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | public $compare_values; |
||
56 | |||
57 | /** |
||
58 | * The result in ELSE section of expr |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | public $else_result; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | * |
||
67 | */ |
||
68 | public function __construct() |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * @param Parser $parser The parser that serves as context. |
||
75 | * @param TokensList $list The list of tokens that are being parsed. |
||
76 | * |
||
77 | * @return Expression |
||
|
|||
78 | */ |
||
79 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
||
225 | |||
226 | /** |
||
227 | * @param Expression $component The component to be built. |
||
228 | * @param array $options Parameters for building. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public static function build($component, array $options = array()) |
||
264 | } |
||
265 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.