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 |
||
15 | class Select extends Query |
||
16 | { |
||
17 | const MODE_ONE = 1; |
||
18 | const MODE_ALL = 2; |
||
19 | |||
20 | use ExtendedSelectionTrait; |
||
21 | |||
22 | protected $join = []; |
||
23 | protected $orderBy = []; |
||
24 | protected $statement; |
||
25 | protected $mode = 2; |
||
26 | protected $page; |
||
27 | protected $calcFoundRows = false; |
||
28 | |||
29 | /** |
||
30 | * Change the mode to returns just the first row. |
||
31 | * |
||
32 | * @return self |
||
33 | */ |
||
34 | public function one() |
||
40 | |||
41 | /** |
||
42 | * Change the mode to returns all rows. |
||
43 | * |
||
44 | * @return self |
||
45 | */ |
||
46 | public function all() |
||
52 | |||
53 | /** |
||
54 | * Adds a LIMIT clause. |
||
55 | * |
||
56 | * @param int $limit |
||
57 | * @param bool $calcFoundRows |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | public function limit($limit, $calcFoundRows = false) |
||
68 | |||
69 | /** |
||
70 | * Paginate the results |
||
71 | * |
||
72 | * @param int|string $page |
||
73 | * @param int|null $limit |
||
74 | * |
||
75 | * @return self |
||
76 | */ |
||
77 | public function page($page, $limit = null) { |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * |
||
94 | * @return Row|RowCollection|null |
||
95 | */ |
||
96 | public function run() |
||
139 | |||
140 | /** |
||
141 | * Create a row and insert the joined rows if exist. |
||
142 | * |
||
143 | * @param array $data |
||
144 | * |
||
145 | * @return Row |
||
146 | */ |
||
147 | public function createRow(array $data) |
||
171 | |||
172 | /** |
||
173 | * Adds an ORDER BY clause. |
||
174 | * |
||
175 | * @param string $orderBy |
||
176 | * @param string|null $direction |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | public function orderBy($orderBy, $direction = null) |
||
190 | |||
191 | /** |
||
192 | * Adds a LEFT JOIN clause. |
||
193 | * |
||
194 | * @param string $table |
||
195 | * @param string $on |
||
196 | * @param array|null $marks |
||
197 | * |
||
198 | * @return self |
||
199 | */ |
||
200 | public function leftJoin($table, $on = null, $marks = null) |
||
204 | |||
205 | /** |
||
206 | * Adds a JOIN clause. |
||
207 | * |
||
208 | * @param string $join |
||
209 | * @param string $table |
||
210 | * @param string $on |
||
211 | * @param array|null $marks |
||
212 | * |
||
213 | * @return self |
||
214 | */ |
||
215 | public function join($join, $table, $on = null, $marks = null) |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | View Code Duplication | public function __invoke() |
|
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function __toString() |
||
308 | |||
309 | /** |
||
310 | * Generates the fields/tables part of a SELECT query. |
||
311 | * |
||
312 | * @param Table $table |
||
313 | * @param bool $rename |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | protected static function buildFields(Table $table, $rename = false) |
||
328 | } |
||
329 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.