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 IndexTrait 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 IndexTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait IndexTrait |
||
13 | { |
||
14 | /** |
||
15 | * Test if a table has been clustered on an index. |
||
16 | * |
||
17 | * @param string $table The table to test |
||
18 | * |
||
19 | * @return bool true if the table has been already clustered |
||
20 | */ |
||
21 | View Code Duplication | public function alreadyClustered($table) |
|
39 | |||
40 | /** |
||
41 | * Creates an index. |
||
42 | * |
||
43 | * @param string $name The index name (can be blank) |
||
44 | * @param string $table The table on which to add the index |
||
45 | * @param array|string $columns An array of columns that form the index or a string expression for a functional index |
||
46 | * @param string $type The index type |
||
47 | * @param bool $unique True if unique, false otherwise |
||
48 | * @param string $where Index predicate ('' for none) |
||
49 | * @param string $tablespace The tablespaces ('' means none/default) |
||
50 | * @param bool $concurrently true to create index concurrently |
||
51 | * |
||
52 | * @return array status (0 if operation was successful) and sql sentence |
||
53 | */ |
||
54 | public function createIndex($name, $table, $columns, $type, $unique, $where, $tablespace, $concurrently) |
||
95 | |||
96 | /** |
||
97 | * Removes an index from the database. |
||
98 | * |
||
99 | * @param string $index The index to drop |
||
100 | * @param bool $cascade True to cascade drop, false to restrict |
||
101 | * |
||
102 | * @return array<integer,mixed|string> 0 if operation was successful |
||
103 | */ |
||
104 | View Code Duplication | public function dropIndex($index, $cascade) |
|
119 | |||
120 | /** |
||
121 | * Rebuild indexes. |
||
122 | * |
||
123 | * @param string $type 'DATABASE' or 'TABLE' or 'INDEX' |
||
124 | * @param string $name The name of the specific database, table, or index to be reindexed |
||
125 | * @param bool $force If true, recreates indexes forcedly in PostgreSQL 7.0-7.1, forces rebuild of system indexes in |
||
126 | * 7.2-7.3, ignored in >=7.4 |
||
127 | * |
||
128 | * @return int 0 if operation was successful |
||
129 | */ |
||
130 | public function reindex($type, $name, $force = false) |
||
157 | |||
158 | /** |
||
159 | * Clusters an index. |
||
160 | * |
||
161 | * @param string $table The table the index is on |
||
162 | * @param string $index The name of the index |
||
163 | * |
||
164 | * @return array<integer,mixed|string> 0 if operation was successful |
||
165 | */ |
||
166 | View Code Duplication | public function clusterIndex($table = '', $index = '') |
|
189 | |||
190 | /** |
||
191 | * Returns a list of all constraints on a table, |
||
192 | * including constraint name, definition, related col and referenced namespace, |
||
193 | * table and col if needed. |
||
194 | * |
||
195 | * @param string $table the table where we are looking for fk |
||
196 | * |
||
197 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
198 | */ |
||
199 | View Code Duplication | public function getConstraintsWithFields($table) |
|
254 | |||
255 | /** |
||
256 | * Adds a primary key constraint to a table. |
||
257 | * |
||
258 | * @param string $table The table to which to add the primery key |
||
259 | * @param array $fields (array) An array of fields over which to add the primary key |
||
260 | * @param string $name (optional) The name to give the key, otherwise default name is assigned |
||
261 | * @param string $tablespace (optional) The tablespace for the schema, '' indicates default |
||
262 | * |
||
263 | * @return int 0 if operation was successful |
||
264 | */ |
||
265 | View Code Duplication | public function addPrimaryKey($table, $fields, $name = '', $tablespace = '') |
|
291 | |||
292 | /** |
||
293 | * Adds a unique constraint to a table. |
||
294 | * |
||
295 | * @param string $table The table to which to add the unique key |
||
296 | * @param array|mixed $fields (array) An array of fields over which to add the unique key |
||
297 | * @param string $name (optional) The name to give the key, otherwise default name is assigned |
||
298 | * @param string $tablespace (optional) The tablespace for the schema, '' indicates default |
||
299 | * |
||
300 | * @return int 0 if operation was successful |
||
301 | */ |
||
302 | View Code Duplication | public function addUniqueKey($table, $fields, $name = '', $tablespace = '') |
|
328 | |||
329 | // Function functions |
||
330 | |||
331 | /** |
||
332 | * Adds a check constraint to a table. |
||
333 | * |
||
334 | * @param string $table The table to which to add the check |
||
335 | * @param string $definition The definition of the check |
||
336 | * @param string $name (optional) The name to give the check, otherwise default name is assigned |
||
337 | * |
||
338 | * @return int 0 if operation was successful |
||
339 | */ |
||
340 | View Code Duplication | public function addCheckConstraint($table, $definition, $name = '') |
|
357 | |||
358 | /** |
||
359 | * Drops a check constraint from a table. |
||
360 | * |
||
361 | * @param string $table The table from which to drop the check |
||
362 | * @param string $name The name of the check to be dropped |
||
363 | * |
||
364 | * @return bool|int 0 success |
||
365 | */ |
||
366 | public function dropCheckConstraint($table, $name) |
||
419 | |||
420 | /** |
||
421 | * Adds a foreign key constraint to a table. |
||
422 | * |
||
423 | * @param string $table The table on which to add an FK |
||
424 | * @param string $targschema The schema that houses the target table to which to add the foreign key |
||
425 | * @param string $targtable The table to which to add the foreign key |
||
426 | * @param array $sfields (array) An array of source fields over which to add the foreign key |
||
427 | * @param array $tfields (array) An array of target fields over which to add the foreign key |
||
428 | * @param string $upd_action The action for updates (eg. RESTRICT) |
||
429 | * @param string $del_action The action for deletes (eg. RESTRICT) |
||
430 | * @param string $match The match type (eg. MATCH FULL) |
||
431 | * @param string $deferrable The deferrability (eg. NOT DEFERRABLE) |
||
432 | * @param string $initially The initially parameter for the FK (eg. INITIALLY IMMEDIATE) |
||
433 | * @param string $name [optional] The name to give the key, otherwise default name is assigned |
||
434 | * |
||
435 | * @return int 0 if operation was successful |
||
436 | * |
||
437 | * @internal param \PHPPgAdmin\Database\The $target table that contains the target columns |
||
438 | * @internal param \PHPPgAdmin\Database\The $intially initial deferrability (eg. INITIALLY IMMEDIATE) |
||
439 | */ |
||
440 | public function addForeignKey( |
||
497 | |||
498 | /** |
||
499 | * Removes a constraint from a relation. |
||
500 | * |
||
501 | * @param string $constraint The constraint to drop |
||
502 | * @param string $relation The relation from which to drop |
||
503 | * @param string $type The type of constraint (c, f, u or p) |
||
504 | * @param bool $cascade True to cascade drop, false to restrict |
||
505 | * |
||
506 | * @return int 0 if operation was successful |
||
507 | */ |
||
508 | public function dropConstraint($constraint, $relation, $type, $cascade) |
||
522 | |||
523 | /** |
||
524 | * A function for getting all columns linked by foreign keys given a group of tables. |
||
525 | * |
||
526 | * @param array $tables multi dimensional assoc array that holds schema and table name |
||
527 | * |
||
528 | * @return int|\PHPPgAdmin\ADORecordSet recordset of linked tables and columns or -1 if $tables isn't an array |
||
529 | */ |
||
530 | public function getLinkingKeys($tables) |
||
614 | |||
615 | /** |
||
616 | * Finds the foreign keys that refer to the specified table. |
||
617 | * |
||
618 | * @param string $table The table to find referrers for |
||
619 | * |
||
620 | * @return int|\PHPPgAdmin\ADORecordSet A recordset or -1 in case of error |
||
621 | */ |
||
622 | View Code Duplication | public function getReferrers($table) |
|
656 | |||
657 | abstract public function fieldClean(&$str); |
||
658 | |||
659 | abstract public function beginTransaction(); |
||
660 | |||
661 | abstract public function rollbackTransaction(); |
||
662 | |||
663 | abstract public function endTransaction(); |
||
664 | |||
665 | abstract public function execute($sql); |
||
666 | |||
667 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
668 | |||
669 | abstract public function selectSet($sql); |
||
670 | |||
671 | abstract public function clean(&$str); |
||
672 | |||
673 | abstract public function hasTablespaces(); |
||
674 | |||
675 | abstract public function arrayClean(&$arr); |
||
676 | |||
677 | abstract public function fieldArrayClean(&$arr); |
||
678 | } |
||
679 |
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.