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:
1 | <?php |
||
13 | class FromStatement extends Statement |
||
14 | { |
||
15 | const FROM = 0; |
||
16 | const INSERT = 1; |
||
17 | const UPDATE = 2; |
||
18 | const DELETE = 3; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $type; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $tables = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $joins = []; |
||
34 | |||
35 | /** |
||
36 | * @param int $type type of table statement |
||
37 | */ |
||
38 | public function __construct($type = self::FROM) |
||
42 | |||
43 | /** |
||
44 | * Adds one or more tables to this statement. |
||
45 | * Supported input styles: |
||
46 | * - addTable('Table,Table2') |
||
47 | * - addTable(['Table','Table2']). |
||
48 | * |
||
49 | * @param string|array $tables |
||
50 | * |
||
51 | * @return self |
||
52 | */ |
||
53 | View Code Duplication | public function addTable($tables) |
|
65 | |||
66 | /** |
||
67 | * Adds a join condition to this statement |
||
68 | * Supported input styles: |
||
69 | * - addJoin('Table,Table2') |
||
70 | * - addJoin(['Table','Table2']). |
||
71 | * |
||
72 | * @param string|array $tables table names |
||
73 | * @param string $on ON condition |
||
74 | * @param string $using USING columns |
||
75 | * @param string $type join type, i.e. OUTER JOIN, CROSS JOIN |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | public function addJoin($tables, $on = null, $using = null, $type = 'JOIN') |
||
104 | |||
105 | /** |
||
106 | * Gets the table(s) associated with this statement. |
||
107 | * |
||
108 | * @return array |
||
109 | */ |
||
110 | public function getTables() |
||
114 | |||
115 | /** |
||
116 | * Gets the join(s) associated with this statement. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function getJoins() |
||
124 | |||
125 | public function build() |
||
157 | |||
158 | /** |
||
159 | * Builds a list of joins. |
||
160 | * |
||
161 | * @param array $joins |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | private function buildJoins(array $joins) |
||
197 | } |
||
198 |