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 |
||
27 | class CaseExpression extends Component |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * The value to be compared |
||
32 | * |
||
33 | * @var Expression |
||
34 | */ |
||
35 | public $value; |
||
36 | |||
37 | /** |
||
38 | * The conditions in WHEN clauses |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | public $conditions; |
||
43 | |||
44 | /** |
||
45 | * The results matching with the WHEN clauses |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public $results; |
||
50 | |||
51 | /** |
||
52 | * The values to be compared against |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $compare_values; |
||
57 | |||
58 | /** |
||
59 | * The result in ELSE section of expr |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | public $else_result; |
||
64 | |||
65 | /** |
||
66 | * Constructor. |
||
67 | * |
||
68 | */ |
||
69 | public function __construct() |
||
72 | |||
73 | /** |
||
74 | * |
||
75 | * @param Parser $parser The parser that serves as context. |
||
76 | * @param TokensList $list The list of tokens that are being parsed. |
||
77 | * |
||
78 | * @return Expression |
||
|
|||
79 | */ |
||
80 | public static function parse(Parser $parser, TokensList $list, array $options = array()) |
||
81 | { |
||
82 | $ret = new CaseExpression(); |
||
83 | |||
84 | /** |
||
85 | * State of parser |
||
86 | * |
||
87 | * @var int $parser |
||
88 | */ |
||
89 | $state = 0; |
||
90 | |||
91 | /** |
||
92 | * Syntax type (type 0 or type 1) |
||
93 | * |
||
94 | * @var int $type |
||
95 | */ |
||
96 | $type = 0; |
||
97 | |||
98 | ++$list->idx; // Skip 'CASE' |
||
99 | |||
100 | for (; $list->idx < $list->count; ++$list->idx) { |
||
101 | |||
102 | /** |
||
103 | * Token parsed at this moment. |
||
104 | * |
||
105 | * @var Token $token |
||
106 | */ |
||
107 | $token = $list->tokens[$list->idx]; |
||
108 | |||
109 | // End of statement. |
||
110 | if ($token->type === Token::TYPE_DELIMITER) { |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | // Skipping whitespaces and comments. |
||
115 | if (($token->type === Token::TYPE_WHITESPACE) |
||
116 | || ($token->type === Token::TYPE_COMMENT) |
||
117 | ) { |
||
118 | continue; |
||
119 | } |
||
120 | |||
121 | if ($state === 0) { |
||
122 | if ($token->type === Token::TYPE_KEYWORD |
||
123 | && $token->value === 'WHEN' |
||
124 | ) { |
||
125 | ++$list->idx; // Skip 'WHEN' |
||
126 | $new_condition = Condition::parse($parser, $list); |
||
127 | $type = 1; |
||
128 | $state = 1; |
||
129 | $ret->conditions[] = $new_condition; |
||
130 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
131 | && $token->value === 'ELSE' |
||
132 | ) { |
||
133 | ++$list->idx; // Skip 'ELSE' |
||
134 | $ret->else_result = Expression::parse($parser, $list); |
||
135 | $state = 0; // last clause of CASE expression |
||
136 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
137 | && ($token->value === 'END' |
||
138 | || $token->value === 'end') |
||
139 | ) { |
||
140 | $state = 3; // end of CASE expression |
||
141 | ++$list->idx; |
||
142 | break; |
||
143 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
144 | $parser->error(__('Unexpected keyword.'), $token); |
||
145 | break; |
||
146 | } else { |
||
147 | $ret->value = Expression::parse($parser, $list); |
||
148 | $type = 0; |
||
149 | $state = 1; |
||
150 | } |
||
151 | } elseif ($state === 1) { |
||
152 | if ($type === 0) { |
||
153 | if ($token->type === Token::TYPE_KEYWORD |
||
154 | && $token->value === 'WHEN' |
||
155 | ) { |
||
156 | ++$list->idx; // Skip 'WHEN' |
||
157 | $new_value = Expression::parse($parser, $list); |
||
158 | $state = 2; |
||
159 | $ret->compare_values[] = $new_value; |
||
160 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
161 | && $token->value === 'ELSE' |
||
162 | ) { |
||
163 | ++$list->idx; // Skip 'ELSE' |
||
164 | $ret->else_result = Expression::parse($parser, $list); |
||
165 | $state = 0; // last clause of CASE expression |
||
166 | } elseif ($token->type === Token::TYPE_KEYWORD |
||
167 | && ($token->value === 'END' |
||
168 | || $token->value === 'end') |
||
169 | ) { |
||
170 | $state = 3; // end of CASE expression |
||
171 | ++$list->idx; |
||
172 | break; |
||
173 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
174 | $parser->error(__('Unexpected keyword.'), $token); |
||
175 | break; |
||
176 | } else { |
||
177 | $parser->error(__('Unexpected token.'), $token); |
||
178 | break; |
||
179 | } |
||
180 | View Code Duplication | } else { |
|
181 | if ($token->type === Token::TYPE_KEYWORD |
||
182 | && $token->value === 'THEN' |
||
183 | ) { |
||
184 | ++$list->idx; // Skip 'THEN' |
||
185 | $new_result = Expression::parse($parser, $list); |
||
186 | $state = 0; |
||
187 | $ret->results[] = $new_result; |
||
188 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
189 | $parser->error(__('Unexpected keyword.'), $token); |
||
190 | break; |
||
191 | } else { |
||
192 | $parser->error(__('Unexpected token.'), $token); |
||
193 | break; |
||
194 | } |
||
195 | } |
||
196 | } elseif ($state === 2) { |
||
197 | View Code Duplication | if ($type === 0) { |
|
198 | if ($token->type === Token::TYPE_KEYWORD |
||
199 | && $token->value === 'THEN' |
||
200 | ) { |
||
201 | ++$list->idx; // Skip 'THEN' |
||
202 | $new_result = Expression::parse($parser, $list); |
||
203 | $ret->results[] = $new_result; |
||
204 | $state = 1; |
||
205 | } elseif ($token->type === Token::TYPE_KEYWORD) { |
||
206 | $parser->error(__('Unexpected keyword.'), $token); |
||
207 | break; |
||
208 | } else { |
||
209 | $parser->error(__('Unexpected token.'), $token); |
||
210 | break; |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | if ($state !== 3) { |
||
217 | $parser->error( |
||
218 | __('Unexpected end of CASE expression'), |
||
219 | $list->tokens[$list->idx - 1] |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | --$list->idx; |
||
224 | return $ret; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param Expression $component The component to be built. |
||
229 | * @param array $options Parameters for building. |
||
230 | * |
||
231 | * @return string |
||
232 | */ |
||
233 | public static function build($component, array $options = array()) |
||
265 | } |
||
266 |
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.