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 |
||
21 | class CaseExpression extends Component |
||
22 | { |
||
23 | /** |
||
24 | * The value to be compared. |
||
25 | * |
||
26 | * @var Expression |
||
27 | */ |
||
28 | public $value; |
||
29 | |||
30 | /** |
||
31 | * The conditions in WHEN clauses. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | public $conditions; |
||
36 | |||
37 | /** |
||
38 | * The results matching with the WHEN clauses. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | public $results; |
||
43 | |||
44 | /** |
||
45 | * The values to be compared against. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public $compare_values; |
||
50 | |||
51 | /** |
||
52 | * The result in ELSE section of expr. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $else_result; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | */ |
||
61 | 16 | public function __construct() |
|
64 | |||
65 | /** |
||
66 | * @param Parser $parser the parser that serves as context |
||
67 | * @param TokensList $list the list of tokens that are being parsed |
||
68 | * |
||
69 | * @return Expression |
||
|
|||
70 | */ |
||
71 | 16 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
|
203 | |||
204 | /** |
||
205 | * @param Expression $component the component to be built |
||
206 | * @param array $options parameters for building |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 7 | public static function build($component, array $options = array()) |
|
242 | } |
||
243 |
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.