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 Select 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 Select, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class Select implements StatementInterface, NodeInterface |
||
55 | { |
||
56 | private $distinct; |
||
57 | |||
58 | /** |
||
59 | * Returns true if the SELECT is a SELECT DISTINCT. |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function getDistinct() |
||
67 | |||
68 | /** |
||
69 | * Sets to true if the SELECT is a SELECT DISTINCT. |
||
70 | * |
||
71 | * @param bool $distinct |
||
72 | */ |
||
73 | public function setDistinct($distinct) |
||
77 | |||
78 | private $columns; |
||
79 | |||
80 | /** |
||
81 | * Returns the list of columns for this SQL select. |
||
82 | * |
||
83 | * @return NodeInterface[] |
||
84 | */ |
||
85 | public function getColumns() |
||
89 | |||
90 | /** |
||
91 | * Sets the list of columns for this SQL select. |
||
92 | * |
||
93 | * @param NodeInterface[] $columns |
||
94 | */ |
||
95 | public function setColumns($columns) |
||
99 | |||
100 | private $from; |
||
101 | |||
102 | /** |
||
103 | * Returns the list of tables for this SQL select. |
||
104 | * |
||
105 | * @return NodeInterface[] |
||
106 | */ |
||
107 | public function getFrom() |
||
111 | |||
112 | /** |
||
113 | * Sets the list of tables for this SQL select. |
||
114 | * |
||
115 | * @param NodeInterface[] $from |
||
116 | */ |
||
117 | public function setFrom($from) |
||
121 | |||
122 | private $where; |
||
123 | |||
124 | /** |
||
125 | * Returns the list of where statements. |
||
126 | * |
||
127 | * @return NodeInterface[] |
||
128 | */ |
||
129 | public function getWhere() |
||
133 | |||
134 | /** |
||
135 | * Sets the list of where statements. |
||
136 | * |
||
137 | * @param NodeInterface[]|NodeInterface $where |
||
138 | */ |
||
139 | public function setWhere($where) |
||
143 | |||
144 | private $group; |
||
145 | |||
146 | /** |
||
147 | * Returns the list of group statements. |
||
148 | * |
||
149 | * @return NodeInterface[] |
||
150 | */ |
||
151 | public function getGroup() |
||
155 | |||
156 | /** |
||
157 | * Sets the list of group statements. |
||
158 | * |
||
159 | * @param NodeInterface[]|NodeInterface $group |
||
160 | */ |
||
161 | public function setGroup($group) |
||
165 | |||
166 | private $having; |
||
167 | |||
168 | /** |
||
169 | * Returns the list of having statements. |
||
170 | * |
||
171 | * @return NodeInterface[]|NodeInterface |
||
172 | */ |
||
173 | public function getHaving() |
||
177 | |||
178 | /** |
||
179 | * Sets the list of having statements. |
||
180 | * |
||
181 | * @param NodeInterface[]|NodeInterface $having |
||
182 | */ |
||
183 | public function setHaving($having) |
||
187 | |||
188 | private $order; |
||
189 | |||
190 | /** |
||
191 | * Returns the list of order statements. |
||
192 | * |
||
193 | * @return NodeInterface[]|NodeInterface |
||
194 | */ |
||
195 | public function getOrder() |
||
199 | |||
200 | /** |
||
201 | * Sets the list of order statements. |
||
202 | * |
||
203 | * @param NodeInterface[]|NodeInterface $order |
||
204 | */ |
||
205 | public function setOrder($order) |
||
209 | |||
210 | private $options; |
||
211 | |||
212 | /** |
||
213 | * Returns the list of options to be applied just after the "SELECT" keyword. |
||
214 | * |
||
215 | * @return string[] |
||
216 | */ |
||
217 | public function getOptions() |
||
221 | |||
222 | /** |
||
223 | * Sets the list of options to be applied just after the "SELECT" keyword. |
||
224 | * |
||
225 | * @param string[] $options |
||
226 | */ |
||
227 | public function setOptions($options) |
||
231 | |||
232 | private $limit; |
||
233 | |||
234 | /** |
||
235 | * @return NodeInterface |
||
236 | */ |
||
237 | public function getLimit() { |
||
240 | |||
241 | /** |
||
242 | * @param NodeInterface $limit |
||
243 | */ |
||
244 | public function setLimit($limit) { |
||
247 | |||
248 | private $offset; |
||
249 | |||
250 | /** |
||
251 | * @return NodeInterface |
||
252 | */ |
||
253 | public function getOffset() |
||
257 | |||
258 | /** |
||
259 | * @param NodeInterface $offset |
||
260 | */ |
||
261 | public function setOffset($offset) |
||
265 | |||
266 | /** |
||
267 | * @param MoufManager $moufManager |
||
268 | * |
||
269 | * @return MoufInstanceDescriptor |
||
270 | */ |
||
271 | View Code Duplication | public function toInstanceDescriptor(MoufManager $moufManager) |
|
287 | |||
288 | /** |
||
289 | * Configure the $instanceDescriptor describing this object (it must already exist as a Mouf instance). |
||
290 | * |
||
291 | * @param MoufManager $moufManager |
||
292 | * |
||
293 | * @return MoufInstanceDescriptor |
||
294 | */ |
||
295 | View Code Duplication | public function overwriteInstanceDescriptor($name, MoufManager $moufManager) |
|
312 | |||
313 | /** |
||
314 | * Renders the object as a SQL string. |
||
315 | * |
||
316 | * @param array $parameters |
||
317 | * @param Connection $dbConnection |
||
318 | * @param int|number $indent |
||
319 | * @param int $conditionsMode |
||
320 | * @return string |
||
321 | */ |
||
322 | public function toSql(array $parameters = array(), Connection $dbConnection = null, $indent = 0, $conditionsMode = self::CONDITION_APPLY) |
||
402 | |||
403 | /** |
||
404 | * Walks the tree of nodes, calling the visitor passed in parameter. |
||
405 | * |
||
406 | * @param VisitorInterface $visitor |
||
407 | */ |
||
408 | public function walk(VisitorInterface $visitor) { |
||
424 | |||
425 | private function walkChildren(&$children, VisitorInterface $visitor) { |
||
448 | } |
||
449 |
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.