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 |
||
22 | trait SqlDialectTrait |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Quotes a database identifier (a column name, table name, etc..) to |
||
27 | * be used safely in queries without the risk of using reserved words |
||
28 | * |
||
29 | * @param string $identifier The identifier to quote. |
||
30 | * @return string |
||
31 | */ |
||
32 | public function quoteIdentifier($identifier) |
||
33 | { |
||
34 | $identifier = trim($identifier); |
||
35 | |||
36 | if ($identifier === '*') { |
||
37 | return '*'; |
||
38 | } |
||
39 | |||
40 | if ($identifier === '') { |
||
41 | return ''; |
||
42 | } |
||
43 | |||
44 | // string |
||
45 | if (preg_match('/^[\w-]+$/', $identifier)) { |
||
46 | return $this->_startQuote . $identifier . $this->_endQuote; |
||
47 | } |
||
48 | |||
49 | if (preg_match('/^[\w-]+\.[^ \*]*$/', $identifier)) { |
||
50 | // string.string |
||
51 | $items = explode('.', $identifier); |
||
52 | return $this->_startQuote . implode($this->_endQuote . '.' . $this->_startQuote, $items) . $this->_endQuote; |
||
53 | } |
||
54 | |||
55 | if (preg_match('/^[\w-]+\.\*$/', $identifier)) { |
||
56 | // string.* |
||
57 | return $this->_startQuote . str_replace('.*', $this->_endQuote . '.*', $identifier); |
||
58 | } |
||
59 | |||
60 | View Code Duplication | if (preg_match('/^([\w-]+)\((.*)\)$/', $identifier, $matches)) { |
|
61 | // Functions |
||
62 | return $matches[1] . '(' . $this->quoteIdentifier($matches[2]) . ')'; |
||
63 | } |
||
64 | |||
65 | // Alias.field AS thing |
||
66 | View Code Duplication | if (preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+AS\s*([\w-]+)$/i', $identifier, $matches)) { |
|
67 | return $this->quoteIdentifier($matches[1]) . ' AS ' . $this->quoteIdentifier($matches[3]); |
||
68 | } |
||
69 | |||
70 | if (preg_match('/^[\w-_\s]*[\w-_]+/', $identifier)) { |
||
71 | return $this->_startQuote . $identifier . $this->_endQuote; |
||
72 | } |
||
73 | |||
74 | return $identifier; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Returns a callable function that will be used to transform a passed Query object. |
||
79 | * This function, in turn, will return an instance of a Query object that has been |
||
80 | * transformed to accommodate any specificities of the SQL dialect in use. |
||
81 | * |
||
82 | * @param string $type the type of query to be transformed |
||
83 | * (select, insert, update, delete) |
||
84 | * @return callable |
||
85 | */ |
||
86 | public function queryTranslator($type) |
||
87 | { |
||
88 | return function ($query) use ($type) { |
||
89 | if ($this->autoQuoting()) { |
||
90 | $query = (new IdentifierQuoter($this))->quote($query); |
||
91 | } |
||
92 | |||
93 | $query = $this->{'_' . $type . 'QueryTranslator'}($query); |
||
94 | $translators = $this->_expressionTranslators(); |
||
95 | if (!$translators) { |
||
96 | return $query; |
||
97 | } |
||
98 | |||
99 | $query->traverseExpressions(function ($expression) use ($translators, $query) { |
||
100 | foreach ($translators as $class => $method) { |
||
101 | if ($expression instanceof $class) { |
||
102 | $this->{$method}($expression, $query); |
||
103 | } |
||
104 | } |
||
105 | }); |
||
106 | return $query; |
||
107 | }; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns an associative array of methods that will transform Expression |
||
112 | * objects to conform with the specific SQL dialect. Keys are class names |
||
113 | * and values a method in this class. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | protected function _expressionTranslators() |
||
121 | |||
122 | /** |
||
123 | * Apply translation steps to select queries. |
||
124 | * |
||
125 | * @param \Cake\Database\Query $query The query to translate |
||
126 | * @return \Cake\Database\Query The modified query |
||
127 | */ |
||
128 | protected function _selectQueryTranslator($query) |
||
132 | |||
133 | /** |
||
134 | * Returns the passed query after rewriting the DISTINCT clause, so that drivers |
||
135 | * that do not support the "ON" part can provide the actual way it should be done |
||
136 | * |
||
137 | * @param \Cake\Database\Query $query The query to be transformed |
||
138 | * @return \Cake\Database\Query |
||
139 | */ |
||
140 | protected function _transformDistinct($query) |
||
148 | |||
149 | /** |
||
150 | * Apply translation steps to delete queries. |
||
151 | * |
||
152 | * Chops out aliases on delete query conditions as most database dialects do not |
||
153 | * support aliases in delete queries. This also removes aliases |
||
154 | * in table names as they frequently don't work either. |
||
155 | * |
||
156 | * We are intentionally not supporting deletes with joins as they have even poorer support. |
||
157 | * |
||
158 | * @param \Cake\Database\Query $query The query to translate |
||
159 | * @return \Cake\Database\Query The modified query |
||
160 | */ |
||
161 | protected function _deleteQueryTranslator($query) |
||
198 | |||
199 | /** |
||
200 | * Apply translation steps to update queries. |
||
201 | * |
||
202 | * @param \Cake\Database\Query $query The query to translate |
||
203 | * @return \Cake\Database\Query The modified query |
||
204 | */ |
||
205 | protected function _updateQueryTranslator($query) |
||
209 | |||
210 | /** |
||
211 | * Apply translation steps to insert queries. |
||
212 | * |
||
213 | * @param \Cake\Database\Query $query The query to translate |
||
214 | * @return \Cake\Database\Query The modified query |
||
215 | */ |
||
216 | protected function _insertQueryTranslator($query) |
||
220 | |||
221 | /** |
||
222 | * Returns a SQL snippet for creating a new transaction savepoint |
||
223 | * |
||
224 | * @param string $name save point name |
||
225 | * @return string |
||
226 | */ |
||
227 | public function savePointSQL($name) |
||
231 | |||
232 | /** |
||
233 | * Returns a SQL snippet for releasing a previously created save point |
||
234 | * |
||
235 | * @param string $name save point name |
||
236 | * @return string |
||
237 | */ |
||
238 | public function releaseSavePointSQL($name) |
||
242 | |||
243 | /** |
||
244 | * Returns a SQL snippet for rollbacking a previously created save point |
||
245 | * |
||
246 | * @param string $name save point name |
||
247 | * @return string |
||
248 | */ |
||
249 | public function rollbackSavePointSQL($name) |
||
253 | } |
||
254 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: