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 FtsTrait 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 FtsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait FtsTrait |
||
13 | { |
||
14 | /** |
||
15 | * Creates a new FTS configuration. |
||
16 | * |
||
17 | * @param string $cfgname The name of the FTS configuration to create |
||
18 | * @param string $parser The parser to be used in new FTS configuration |
||
19 | * @param string $template The existing FTS configuration to be used as template for the new one |
||
20 | * @param string $comment If omitted, defaults to nothing |
||
21 | * |
||
22 | * @return bool|int 0 success |
||
23 | * |
||
24 | * @internal param string $locale Locale of the FTS configuration |
||
25 | * @internal param string $withmap Should we copy whole map of existing FTS configuration to the new one |
||
26 | * @internal param string $makeDefault Should this configuration be the default for locale given |
||
27 | */ |
||
28 | public function createFtsConfiguration($cfgname, $parser = '', $template = '', $comment = '') |
||
77 | |||
78 | // Language functions |
||
79 | |||
80 | /** |
||
81 | * Returns available FTS configurations. |
||
82 | * |
||
83 | * @param bool $all if false, returns schema qualified FTS confs |
||
84 | * |
||
85 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
86 | */ |
||
87 | View Code Duplication | public function getFtsConfigurations($all = true) |
|
110 | |||
111 | // Aggregate functions |
||
112 | |||
113 | /** |
||
114 | * Returns the map of FTS configuration given |
||
115 | * (list of mappings (tokens) and their processing dictionaries). |
||
116 | * |
||
117 | * @param string $ftscfg Name of the FTS configuration |
||
118 | * |
||
119 | * @return \PHPPgAdmin\ADORecordSet recordset |
||
120 | */ |
||
121 | View Code Duplication | public function getFtsConfigurationMap($ftscfg) |
|
153 | |||
154 | /** |
||
155 | * Returns FTS parsers available. |
||
156 | * |
||
157 | * @param bool $all if false, return only Parsers from the current schema |
||
158 | * |
||
159 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
160 | */ |
||
161 | View Code Duplication | public function getFtsParsers($all = true) |
|
182 | |||
183 | /** |
||
184 | * Returns FTS dictionaries available. |
||
185 | * |
||
186 | * @param bool $all if false, return only Dics from the current schema |
||
187 | * |
||
188 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
189 | */ |
||
190 | View Code Duplication | public function getFtsDictionaries($all = true) |
|
210 | |||
211 | /** |
||
212 | * Returns all FTS dictionary templates available. |
||
213 | * |
||
214 | * @return \PHPPgAdmin\ADORecordSet all FTS dictionary templates available |
||
215 | */ |
||
216 | public function getFtsDictionaryTemplates() |
||
238 | |||
239 | /** |
||
240 | * Drops FTS coniguration. |
||
241 | * |
||
242 | * @param string $ftscfg The configuration's name |
||
243 | * @param bool $cascade true to Cascade to dependenced objects |
||
244 | * |
||
245 | * @return int 0 if operation was successful |
||
246 | */ |
||
247 | public function dropFtsConfiguration($ftscfg, $cascade) |
||
260 | |||
261 | /** |
||
262 | * Drops FTS dictionary. |
||
263 | * |
||
264 | * @param string $ftsdict The dico's name |
||
265 | * @param bool $cascade Cascade to dependenced objects |
||
266 | * |
||
267 | * @return int 0 if operation was successful |
||
268 | * |
||
269 | * @todo Support of dictionary templates dropping |
||
270 | */ |
||
271 | public function dropFtsDictionary($ftsdict, $cascade) |
||
285 | |||
286 | /** |
||
287 | * Alters FTS configuration. |
||
288 | * |
||
289 | * @param string $cfgname The conf's name |
||
290 | * @param string $comment A comment on for the conf |
||
291 | * @param string $name The new conf name |
||
292 | * |
||
293 | * @return bool|int 0 on success |
||
294 | */ |
||
295 | View Code Duplication | public function updateFtsConfiguration($cfgname, $comment, $name) |
|
330 | |||
331 | /** |
||
332 | * Creates a new FTS dictionary or FTS dictionary template. |
||
333 | * |
||
334 | * @param string $dictname The name of the FTS dictionary to create |
||
335 | * @param bool $isTemplate Flag whether we create usual dictionary or dictionary template |
||
336 | * @param string $template The existing FTS dictionary to be used as template for the new one |
||
337 | * @param string $lexize The name of the function, which does transformation of input word |
||
338 | * @param string $init The name of the function, which initializes dictionary |
||
339 | * @param string $option Usually, it stores various options required for the dictionary |
||
340 | * @param string $comment If omitted, defaults to nothing |
||
341 | * |
||
342 | * @return bool|int 0 success |
||
343 | */ |
||
344 | public function createFtsDictionary( |
||
420 | |||
421 | // Role, User/Group functions |
||
422 | |||
423 | /** |
||
424 | * Alters FTS dictionary or dictionary template. |
||
425 | * |
||
426 | * @param string $dictname The dico's name |
||
427 | * @param string $comment The comment |
||
428 | * @param string $name The new dico's name |
||
429 | * |
||
430 | * @return bool|int 0 on success |
||
431 | */ |
||
432 | View Code Duplication | public function updateFtsDictionary($dictname, $comment, $name) |
|
466 | |||
467 | /** |
||
468 | * Return all information relating to a FTS dictionary. |
||
469 | * |
||
470 | * @param string $ftsdict The name of the FTS dictionary |
||
471 | * |
||
472 | * @return \PHPPgAdmin\ADORecordSet recordset of FTS dictionary information |
||
473 | */ |
||
474 | public function getFtsDictionaryByName($ftsdict) |
||
498 | |||
499 | /** |
||
500 | * Creates/updates/deletes FTS mapping. |
||
501 | * |
||
502 | * @param string $ftscfg The name of the FTS dictionary |
||
503 | * @param array $mapping Array of tokens' names |
||
504 | * @param string $action What to do with the mapping: add, alter or drop |
||
505 | * @param string $dictname Dictionary that will process tokens given or null in case of drop action |
||
506 | * |
||
507 | * @return int 0 if operation was successful |
||
508 | * |
||
509 | * @internal param string $cfgname The name of the FTS configuration to alter |
||
510 | */ |
||
511 | public function changeFtsMapping($ftscfg, $mapping, $action, $dictname = null) |
||
546 | |||
547 | /** |
||
548 | * Return all information related to a given FTS configuration's mapping. |
||
549 | * |
||
550 | * @param string $ftscfg The name of the FTS configuration |
||
551 | * @param string $mapping The name of the mapping |
||
552 | * |
||
553 | * @return \PHPPgAdmin\ADORecordSet FTS configuration information |
||
554 | */ |
||
555 | public function getFtsMappingByName($ftscfg, $mapping) |
||
586 | |||
587 | /** |
||
588 | * Return list of FTS mappings possible for given parser |
||
589 | * (specified by given configuration since configuration can only have 1 parser). |
||
590 | * |
||
591 | * @param string $ftscfg The config's name that use the parser |
||
592 | * |
||
593 | * @return int 0 if operation was successful |
||
594 | */ |
||
595 | public function getFtsMappings($ftscfg) |
||
605 | |||
606 | /** |
||
607 | * Return all information related to a FTS configuration. |
||
608 | * |
||
609 | * @param string $ftscfg The name of the FTS configuration |
||
610 | * |
||
611 | * @return \PHPPgAdmin\ADORecordSet FTS configuration information |
||
612 | */ |
||
613 | public function getFtsConfigurationByName($ftscfg) |
||
634 | |||
635 | abstract public function fieldClean(&$str); |
||
636 | |||
637 | abstract public function beginTransaction(); |
||
638 | |||
639 | abstract public function rollbackTransaction(); |
||
640 | |||
641 | abstract public function endTransaction(); |
||
642 | |||
643 | abstract public function execute($sql); |
||
644 | |||
645 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
646 | |||
647 | abstract public function selectSet($sql); |
||
648 | |||
649 | abstract public function clean(&$str); |
||
650 | |||
651 | abstract public function arrayClean(&$arr); |
||
652 | } |
||
653 |
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: