Total Complexity | 44 |
Total Lines | 400 |
Duplicated Lines | 13.75 % |
Changes | 0 |
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 Postgres83 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.
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 Postgres83, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Postgres83 extends Postgres84 |
||
12 | { |
||
13 | public $major_version = 8.3; |
||
14 | |||
15 | // List of all legal privileges that can be applied to different types |
||
16 | // of objects. |
||
17 | public $privlist = [ |
||
18 | 'table' => ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'REFERENCES', 'TRIGGER', 'ALL PRIVILEGES'], |
||
19 | 'view' => ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'REFERENCES', 'TRIGGER', 'ALL PRIVILEGES'], |
||
20 | 'sequence' => ['SELECT', 'UPDATE', 'ALL PRIVILEGES'], |
||
21 | 'database' => ['CREATE', 'TEMPORARY', 'CONNECT', 'ALL PRIVILEGES'], |
||
22 | 'function' => ['EXECUTE', 'ALL PRIVILEGES'], |
||
23 | 'language' => ['USAGE', 'ALL PRIVILEGES'], |
||
24 | 'schema' => ['CREATE', 'USAGE', 'ALL PRIVILEGES'], |
||
25 | 'tablespace' => ['CREATE', 'ALL PRIVILEGES'], |
||
26 | ]; |
||
27 | // List of characters in acl lists and the privileges they |
||
28 | // refer to. |
||
29 | public $privmap = [ |
||
30 | 'r' => 'SELECT', |
||
31 | 'w' => 'UPDATE', |
||
32 | 'a' => 'INSERT', |
||
33 | 'd' => 'DELETE', |
||
34 | 'R' => 'RULE', |
||
35 | 'x' => 'REFERENCES', |
||
36 | 't' => 'TRIGGER', |
||
37 | 'X' => 'EXECUTE', |
||
38 | 'U' => 'USAGE', |
||
39 | 'C' => 'CREATE', |
||
40 | 'T' => 'TEMPORARY', |
||
41 | 'c' => 'CONNECT', |
||
42 | ]; |
||
43 | |||
44 | // Databse functions |
||
45 | |||
46 | /** |
||
47 | * Return all database available on the server |
||
48 | * |
||
49 | * @param $currentdatabase database name that should be on top of the resultset |
||
50 | * |
||
51 | * @return A list of databases, sorted alphabetically |
||
52 | */ |
||
53 | View Code Duplication | public function getDatabases($currentdatabase = null) |
|
|
|||
54 | { |
||
55 | $conf = $this->conf; |
||
56 | $server_info = $this->server_info; |
||
57 | |||
58 | if (isset($conf['owned_only']) && $conf['owned_only'] && !$this->isSuperUser()) { |
||
59 | $username = $server_info['username']; |
||
60 | $this->clean($username); |
||
61 | $clause = " AND pr.rolname='{$username}'"; |
||
62 | } else { |
||
63 | $clause = ''; |
||
64 | } |
||
65 | |||
66 | if ($currentdatabase != null) { |
||
67 | $this->clean($currentdatabase); |
||
68 | $orderby = "ORDER BY pdb.datname = '{$currentdatabase}' DESC, pdb.datname"; |
||
69 | } else { |
||
70 | $orderby = 'ORDER BY pdb.datname'; |
||
71 | } |
||
72 | |||
73 | if (!$conf['show_system']) { |
||
74 | $where = ' AND NOT pdb.datistemplate'; |
||
75 | } else { |
||
76 | $where = ' AND pdb.datallowconn'; |
||
77 | } |
||
78 | |||
79 | $sql = " |
||
80 | SELECT pdb.datname AS datname, pr.rolname AS datowner, pg_encoding_to_char(encoding) AS datencoding, |
||
81 | (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pdb.oid=pd.objoid AND pd.classoid='pg_database'::regclass) AS datcomment, |
||
82 | (SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=pdb.dattablespace) AS tablespace, |
||
83 | pg_catalog.pg_database_size(pdb.oid) as dbsize |
||
84 | FROM pg_catalog.pg_database pdb LEFT JOIN pg_catalog.pg_roles pr ON (pdb.datdba = pr.oid) |
||
85 | WHERE true |
||
86 | {$where} |
||
87 | {$clause} |
||
88 | {$orderby}"; |
||
89 | |||
90 | return $this->selectSet($sql); |
||
91 | } |
||
92 | |||
93 | // Administration functions |
||
94 | |||
95 | /** |
||
96 | * Returns all available autovacuum per table information. |
||
97 | * |
||
98 | * @param string $table |
||
99 | * @return \PHPPgAdmin\Database\A recordset |
||
100 | */ |
||
101 | public function getTableAutovacuum($table = '') |
||
102 | { |
||
103 | $sql = ''; |
||
104 | |||
105 | if ($table !== '') { |
||
106 | $this->clean($table); |
||
107 | $c_schema = $this->_schema; |
||
108 | $this->clean($c_schema); |
||
109 | |||
110 | $sql = " |
||
111 | SELECT vacrelid, nspname, relname, |
||
112 | CASE enabled |
||
113 | WHEN 't' THEN 'on' |
||
114 | ELSE 'off' |
||
115 | END AS autovacuum_enabled, vac_base_thresh AS autovacuum_vacuum_threshold, |
||
116 | vac_scale_factor AS autovacuum_vacuum_scale_factor, anl_base_thresh AS autovacuum_analyze_threshold, |
||
117 | anl_scale_factor AS autovacuum_analyze_scale_factor, vac_cost_delay AS autovacuum_vacuum_cost_delay, |
||
118 | vac_cost_limit AS autovacuum_vacuum_cost_limit |
||
119 | FROM pg_autovacuum AS a |
||
120 | join pg_class AS c on (c.oid=a.vacrelid) |
||
121 | join pg_namespace AS n on (n.oid=c.relnamespace) |
||
122 | WHERE c.relname = '{$table}' AND n.nspname = '{$c_schema}' |
||
123 | ORDER BY nspname, relname |
||
124 | "; |
||
125 | } else { |
||
126 | $sql = " |
||
127 | SELECT vacrelid, nspname, relname, |
||
128 | CASE enabled |
||
129 | WHEN 't' THEN 'on' |
||
130 | ELSE 'off' |
||
131 | END AS autovacuum_enabled, vac_base_thresh AS autovacuum_vacuum_threshold, |
||
132 | vac_scale_factor AS autovacuum_vacuum_scale_factor, anl_base_thresh AS autovacuum_analyze_threshold, |
||
133 | anl_scale_factor AS autovacuum_analyze_scale_factor, vac_cost_delay AS autovacuum_vacuum_cost_delay, |
||
134 | vac_cost_limit AS autovacuum_vacuum_cost_limit |
||
135 | FROM pg_autovacuum AS a |
||
136 | join pg_class AS c on (c.oid=a.vacrelid) |
||
137 | join pg_namespace AS n on (n.oid=c.relnamespace) |
||
138 | ORDER BY nspname, relname |
||
139 | "; |
||
140 | } |
||
141 | |||
142 | return $this->selectSet($sql); |
||
143 | } |
||
144 | |||
145 | public function saveAutovacuum( |
||
256 | } |
||
257 | |||
258 | public function dropAutovacuum($table) |
||
273 | } |
||
274 | |||
275 | // Sequence functions |
||
276 | |||
277 | /** |
||
278 | * Alter a sequence's properties |
||
279 | * |
||
280 | * @param $seqrs The sequence RecordSet returned by getSequence() |
||
281 | * @param $increment The sequence incremental value |
||
282 | * @param $minvalue The sequence minimum value |
||
283 | * @param $maxvalue The sequence maximum value |
||
284 | * @param $restartvalue The sequence current value |
||
285 | * @param $cachevalue The sequence cache value |
||
286 | * @param $cycledvalue Sequence can cycle ? |
||
287 | * @param $startvalue The sequence start value when issueing a restart (ignored) |
||
288 | * @return int|\PHPPgAdmin\Database\A 0 success |
||
289 | */ |
||
290 | public function alterSequenceProps( |
||
291 | $seqrs, |
||
292 | $increment, |
||
293 | $minvalue, |
||
294 | $maxvalue, |
||
295 | $restartvalue, |
||
296 | $cachevalue, |
||
297 | $cycledvalue, |
||
298 | $startvalue |
||
299 | ) { |
||
300 | $sql = ''; |
||
301 | /* vars are cleaned in _alterSequence */ |
||
302 | if (!empty($increment) && ($increment != $seqrs->fields['increment_by'])) { |
||
303 | $sql .= " INCREMENT {$increment}"; |
||
304 | } |
||
305 | |||
306 | if (!empty($minvalue) && ($minvalue != $seqrs->fields['min_value'])) { |
||
307 | $sql .= " MINVALUE {$minvalue}"; |
||
308 | } |
||
309 | |||
310 | if (!empty($maxvalue) && ($maxvalue != $seqrs->fields['max_value'])) { |
||
311 | $sql .= " MAXVALUE {$maxvalue}"; |
||
312 | } |
||
313 | |||
314 | if (!empty($restartvalue) && ($restartvalue != $seqrs->fields['last_value'])) { |
||
315 | $sql .= " RESTART {$restartvalue}"; |
||
316 | } |
||
317 | |||
318 | if (!empty($cachevalue) && ($cachevalue != $seqrs->fields['cache_value'])) { |
||
319 | $sql .= " CACHE {$cachevalue}"; |
||
320 | } |
||
321 | |||
322 | // toggle cycle yes/no |
||
323 | if (!is_null($cycledvalue)) { |
||
324 | $sql .= (!$cycledvalue ? ' NO ' : '') . ' CYCLE'; |
||
325 | } |
||
326 | |||
327 | if ($sql != '') { |
||
328 | $f_schema = $this->_schema; |
||
329 | $this->fieldClean($f_schema); |
||
330 | $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" {$sql}"; |
||
331 | |||
332 | return $this->execute($sql); |
||
333 | } |
||
334 | |||
335 | return 0; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Alter a sequence's owner |
||
340 | * |
||
341 | * @param $seqrs The sequence RecordSet returned by getSequence() |
||
342 | * @param $owner |
||
343 | * @return int|\PHPPgAdmin\Database\A 0 success |
||
344 | * @internal param \PHPPgAdmin\Database\The $name new owner for the sequence |
||
345 | */ |
||
346 | View Code Duplication | public function alterSequenceOwner($seqrs, $owner) |
|
347 | { |
||
348 | // If owner has been changed, then do the alteration. We are |
||
349 | // careful to avoid this generally as changing owner is a |
||
350 | // superuser only function. |
||
351 | /* vars are cleaned in _alterSequence */ |
||
352 | if (!empty($owner) && ($seqrs->fields['seqowner'] != $owner)) { |
||
353 | $f_schema = $this->_schema; |
||
354 | $this->fieldClean($f_schema); |
||
355 | $sql = "ALTER TABLE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" OWNER TO \"{$owner}\""; |
||
356 | |||
357 | return $this->execute($sql); |
||
358 | } |
||
359 | |||
360 | return 0; |
||
361 | } |
||
362 | |||
363 | // Function functions |
||
364 | |||
365 | /** |
||
366 | * Returns all details for a particular function |
||
367 | * |
||
368 | * @param $function_oid |
||
369 | * @return \PHPPgAdmin\Database\Function info |
||
370 | * @internal param \PHPPgAdmin\Database\The $func name of the function to retrieve |
||
371 | */ |
||
372 | public function getFunction($function_oid) |
||
373 | { |
||
374 | $this->clean($function_oid); |
||
375 | |||
376 | $sql = " |
||
377 | SELECT |
||
378 | pc.oid AS prooid, proname, pg_catalog.pg_get_userbyid(proowner) AS proowner, |
||
379 | nspname as proschema, lanname as prolanguage, procost, prorows, |
||
380 | pg_catalog.format_type(prorettype, NULL) as proresult, prosrc, |
||
381 | probin, proretset, proisstrict, provolatile, prosecdef, |
||
382 | pg_catalog.oidvectortypes(pc.proargtypes) AS proarguments, |
||
383 | proargnames AS proargnames, |
||
384 | pg_catalog.obj_description(pc.oid, 'pg_proc') AS procomment, |
||
385 | proconfig |
||
386 | FROM |
||
387 | pg_catalog.pg_proc pc, pg_catalog.pg_language pl, |
||
388 | pg_catalog.pg_namespace pn |
||
389 | WHERE |
||
390 | pc.oid = '{$function_oid}'::oid AND pc.prolang = pl.oid |
||
391 | AND pc.pronamespace = pn.oid |
||
392 | "; |
||
393 | |||
394 | return $this->selectSet($sql); |
||
395 | } |
||
396 | |||
397 | // Capabilities |
||
398 | public function hasQueryKill() |
||
401 | } |
||
402 | |||
403 | public function hasDatabaseCollation() |
||
406 | } |
||
407 | |||
408 | public function hasAlterSequenceStart() |
||
411 | } |
||
412 | } |
||
413 |
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.