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 OracleSQLTranslator 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 OracleSQLTranslator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class OracleSQLTranslator extends PHPSQLCreator |
||
41 | { |
||
42 | private $con; # this is the database connection from LimeSurvey |
||
43 | private $preventColumnRefs = array(); |
||
44 | private $allTables = array(); |
||
45 | const ASTERISK_ALIAS = '[#RePl#]'; |
||
46 | |||
47 | public function __construct($con) |
||
48 | { |
||
49 | parent::__construct(); |
||
50 | $this->con = $con; |
||
51 | $this->initGlobalVariables(); |
||
52 | } |
||
53 | |||
54 | private function initGlobalVariables() |
||
55 | { |
||
56 | $this->preventColumnRefs = false; |
||
57 | $this->allTables = array(); |
||
58 | } |
||
59 | |||
60 | public static function dbgprint($txt) |
||
61 | { |
||
62 | if (isset($_ENV['DEBUG'])) { |
||
63 | print $txt; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public static function preprint($s, $return = false) |
||
68 | { |
||
69 | $x = '<pre>'; |
||
70 | $x .= print_r($s, 1); |
||
71 | $x .= '</pre>'; |
||
72 | if ($return) { |
||
73 | return $x; |
||
74 | } |
||
75 | self::dbgprint($x."<br/>\n"); |
||
76 | } |
||
77 | |||
78 | protected function processAlias($parsed) |
||
79 | { |
||
80 | if ($parsed === false) { |
||
81 | return ''; |
||
82 | } |
||
83 | # we don't need an AS between expression and alias |
||
84 | $sql = ' '.$parsed['name']; |
||
85 | |||
86 | return $sql; |
||
87 | } |
||
88 | |||
89 | protected function processDELETE($parsed) |
||
90 | { |
||
91 | if (count($parsed['TABLES']) > 1) { |
||
92 | die('cannot translate delete statement into Oracle dialect, multiple tables are not allowed.'); |
||
93 | } |
||
94 | |||
95 | return 'DELETE'; |
||
96 | } |
||
97 | |||
98 | public static function getColumnNameFor($column) |
||
99 | { |
||
100 | if (strtolower($column) === 'uid') { |
||
101 | $column = 'uid_'; |
||
102 | } |
||
103 | // TODO: add more here, if necessary |
||
104 | return $column; |
||
105 | } |
||
106 | |||
107 | public static function getShortTableNameFor($table) |
||
108 | { |
||
109 | if (strtolower($table) === 'surveys_languagesettings') { |
||
110 | $table = 'surveys_lngsettings'; |
||
111 | } |
||
112 | // TODO: add more here, if necessary |
||
113 | return $table; |
||
114 | } |
||
115 | |||
116 | protected function processTable($parsed, $index) |
||
117 | { |
||
118 | if ($parsed['expr_type'] !== 'table') { |
||
119 | return ''; |
||
120 | } |
||
121 | |||
122 | $sql = $this->getShortTableNameFor($parsed['table']); |
||
123 | $alias = $this->processAlias($parsed['alias']); |
||
124 | $sql .= $alias; |
||
125 | |||
126 | if ($index !== 0) { |
||
127 | $sql = $this->processJoin($parsed['join_type']).' '.$sql; |
||
128 | $sql .= $this->processRefType($parsed['ref_type']); |
||
129 | $sql .= $this->processRefClause($parsed['ref_clause']); |
||
130 | } |
||
131 | |||
132 | # store the table and its alias for later use |
||
133 | $last = array_pop($this->allTables); |
||
134 | $last['tables'][] = array('table' => $this->getShortTableNameFor($parsed['table']), 'alias' => trim($alias)); |
||
135 | $this->allTables[] = $last; |
||
136 | |||
137 | return $sql; |
||
138 | } |
||
139 | |||
140 | protected function processFROM($parsed) |
||
141 | { |
||
142 | $this->allTables[] = array('tables' => array(), 'alias' => ''); |
||
143 | |||
144 | return parent::processFROM($parsed); |
||
145 | } |
||
146 | |||
147 | protected function processTableExpression($parsed, $index) |
||
148 | { |
||
149 | if ($parsed['expr_type'] !== 'table_expression') { |
||
150 | return ''; |
||
151 | } |
||
152 | $sql = substr($this->processFROM($parsed['sub_tree']), 5); // remove FROM keyword |
||
153 | $sql = '('.$sql.')'; |
||
154 | |||
155 | $alias .= $this->processAlias($parsed['alias']); |
||
156 | $sql .= $alias; |
||
157 | |||
158 | # store the tables-expression-alias for later use |
||
159 | $last = array_pop($this->allTables); |
||
160 | $last['alias'] = trim($alias); |
||
161 | $this->allTables[] = $last; |
||
162 | |||
163 | if ($index !== 0) { |
||
164 | $sql = $this->processJoin($parsed['join_type']).' '.$sql; |
||
165 | $sql .= $this->processRefType($parsed['ref_type']); |
||
166 | $sql .= $this->processRefClause($parsed['ref_clause']); |
||
167 | } |
||
168 | |||
169 | return $sql; |
||
170 | } |
||
171 | |||
172 | View Code Duplication | private function getTableNameFromExpression($expr) |
|
173 | { |
||
174 | $pos = strpos($expr, '.'); |
||
175 | if ($pos === false) { |
||
176 | $pos = -1; |
||
177 | } |
||
178 | |||
179 | return trim(substr($expr, 0, $pos + 1), '.'); |
||
180 | } |
||
181 | |||
182 | View Code Duplication | private function getColumnNameFromExpression($expr) |
|
183 | { |
||
184 | $pos = strpos($expr, '.'); |
||
185 | if ($pos === false) { |
||
186 | $pos = -1; |
||
187 | } |
||
188 | |||
189 | return substr($expr, $pos + 1); |
||
190 | } |
||
191 | |||
192 | private function isCLOBColumnInDB($table, $column) |
||
193 | { |
||
194 | $res = $this->con->GetOne( |
||
195 | "SELECT count(*) FROM user_lobs WHERE table_name='".strtoupper($table)."' AND column_name='" |
||
196 | .strtoupper($column)."'"); |
||
197 | |||
198 | return ($res >= 1); |
||
199 | } |
||
200 | |||
201 | protected function isCLOBColumn($table, $column) |
||
202 | { |
||
203 | $tables = end($this->allTables); |
||
204 | |||
205 | if ($table === '') { |
||
206 | foreach ($tables['tables'] as $k => $v) { |
||
207 | if ($this->isCLOBColumn($v['table'], $column)) { |
||
208 | return true; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return false; |
||
213 | } |
||
214 | |||
215 | # check the aliases, $table cannot be empty |
||
216 | foreach ($tables['tables'] as $k => $v) { |
||
217 | if ((strtolower($v['alias']) === strtolower($table)) |
||
218 | || (strtolower($tables['alias']) === strtolower($table))) { |
||
219 | if ($this->isCLOBColumnInDB($v['table'], $column)) { |
||
220 | return true; |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | # it must be a valid table name |
||
226 | return $this->isCLOBColumnInDB($table, $column); |
||
227 | } |
||
228 | |||
229 | protected function processOrderByExpression($parsed) |
||
247 | |||
248 | protected function processColRef($parsed) |
||
249 | { |
||
250 | if ($parsed['expr_type'] !== 'colref') { |
||
251 | return ''; |
||
252 | } |
||
253 | |||
254 | $table = $this->getTableNameFromExpression($parsed['base_expr']); |
||
255 | $col = $this->getColumnNameFromexpression($parsed['base_expr']); |
||
256 | |||
257 | # we have to change the column name, if the column is uid |
||
258 | # we have to change the tablereference, if the tablename is too long |
||
259 | $col = $this->getColumnNameFor($col); |
||
260 | $table = $this->getShortTableNameFor($table); |
||
261 | |||
262 | # if we have * as colref, we cannot use other columns |
||
263 | # we have to add alias.* if we know all table aliases |
||
264 | if (($table === '') && ($col === '*')) { |
||
265 | array_pop($this->preventColumnRefs); |
||
266 | $this->preventColumnRefs[] = true; |
||
267 | |||
268 | return ASTERISK_ALIAS; # this is the position, we have to replace later |
||
269 | } |
||
270 | |||
271 | $alias = ''; |
||
272 | if (isset($parsed['alias'])) { |
||
273 | $alias = $this->processAlias($parsed['alias']); |
||
274 | } |
||
275 | |||
276 | return (($table !== '') ? ($table.'.'.$col) : $col).$alias; |
||
277 | } |
||
278 | |||
279 | protected function processFunctionOnSelect($parsed) |
||
280 | { |
||
281 | $old = end($this->preventColumnRefs); |
||
282 | $sql = $this->processFunction($parsed); |
||
283 | |||
284 | if ($old !== end($this->preventColumnRefs)) { |
||
285 | # prevents wrong handling of count(*) |
||
286 | array_pop($this->preventColumnRefs); |
||
287 | $this->preventColumnRefs[] = $old; |
||
288 | $sql = str_replace(ASTERISK_ALIAS, '*', $sql); |
||
289 | } |
||
290 | |||
291 | return $sql; |
||
292 | } |
||
293 | |||
294 | protected function processSELECT($parsed) |
||
316 | |||
317 | private function correctColRefStatement($sql) |
||
318 | { |
||
319 | $alias = ''; |
||
320 | $tables = end($this->allTables); |
||
321 | |||
322 | # should we correct the selection list? |
||
323 | if (array_pop($this->preventColumnRefs)) { |
||
324 | |||
325 | # do we have a table-expression alias? |
||
326 | if ($tables['alias'] !== '') { |
||
327 | $alias = $tables['alias'].'.*'; |
||
328 | } else { |
||
329 | foreach ($tables['tables'] as $k => $v) { |
||
330 | $alias .= ($v['alias'] === '' ? $v['table'] : $v['alias']).'.*,'; |
||
331 | } |
||
332 | $alias = substr($alias, 0, -1); |
||
333 | } |
||
339 | |||
340 | protected function processSelectStatement($parsed) |
||
366 | |||
367 | public function create($parsed) |
||
383 | |||
384 | public function process($sql) |
||
397 | } |
||
398 | |||
403 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.