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 |
||
17 | class DbSearch_PostgreSQL implements DbSearch |
||
18 | { |
||
19 | /** |
||
20 | * This instance of the search |
||
21 | * @var DbSearch_PostgreSQL |
||
22 | */ |
||
23 | private static $_search = null; |
||
24 | |||
25 | /** |
||
26 | * This function will tell you whether this database type supports this search type. |
||
27 | * |
||
28 | * @param string $search_type |
||
29 | */ |
||
30 | public function search_support($search_type) |
||
36 | |||
37 | /** |
||
38 | * Compute and execute the correct query for search. |
||
39 | * Returns the result of executing the query. |
||
40 | * |
||
41 | * @param string $identifier |
||
42 | * @param string $db_string |
||
43 | * @param mixed[] $db_values default array() |
||
44 | * @param resource|null $connection |
||
45 | */ |
||
46 | public function search_query($identifier, $db_string, $db_values = array(), $connection = null) |
||
47 | { |
||
48 | $db = database(); |
||
49 | |||
50 | $replacements = array( |
||
51 | 'create_tmp_log_search_topics' => array( |
||
52 | '~mediumint\(\d\)~i' => 'int', |
||
53 | '~unsigned~i' => '', |
||
54 | '~ENGINE=MEMORY~i' => '', |
||
55 | ), |
||
56 | 'create_tmp_log_search_messages' => array( |
||
57 | '~mediumint\(\d\)~i' => 'int', |
||
58 | '~unsigned~i' => '', |
||
59 | '~ENGINE=MEMORY~i' => '', |
||
60 | ), |
||
61 | 'drop_tmp_log_search_topics' => array( |
||
62 | '~IF\sEXISTS~i' => '', |
||
63 | ), |
||
64 | 'drop_tmp_log_search_messages' => array( |
||
65 | '~IF\sEXISTS~i' => '', |
||
66 | ), |
||
67 | 'insert_into_log_messages_fulltext' => array( |
||
68 | '~LIKE~i' => 'iLIKE', |
||
69 | '~NOT\sLIKE~i' => '~NOT iLIKE', |
||
70 | '~NOT\sRLIKE~i' => '!~*', |
||
71 | '~RLIKE~i' => '~*', |
||
72 | ), |
||
73 | 'insert_log_search_results_subject' => array( |
||
74 | '~LIKE~i' => 'iLIKE', |
||
75 | '~NOT\sLIKE~i' => 'NOT iLIKE', |
||
76 | '~NOT\sRLIKE~i' => '!~*', |
||
77 | '~RLIKE~i' => '~*', |
||
78 | ), |
||
79 | ); |
||
80 | |||
81 | if (isset($replacements[$identifier])) |
||
82 | $db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
||
83 | elseif (preg_match('~^\s*INSERT\sIGNORE~i', $db_string) != 0) |
||
84 | { |
||
85 | $db_string = preg_replace('~^\s*INSERT\sIGNORE~i', 'INSERT', $db_string); |
||
86 | // Don't error on multi-insert. |
||
87 | $db->skip_next_error(); |
||
88 | } |
||
89 | |||
90 | // @deprecated since 1.1 - temporary measure until a proper skip_error is implemented |
||
91 | if (!empty($db_values['db_error_skip'])) |
||
92 | { |
||
93 | $db->skip_next_error(); |
||
94 | } |
||
95 | |||
96 | $return = $db->query('', $db_string, |
||
97 | $db_values, $connection |
||
98 | ); |
||
99 | |||
100 | return $return; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns some basic info about the {db_prefix}messages table |
||
105 | * Used in ManageSearch.controller.php in the page to select the index method |
||
106 | */ |
||
107 | public function membersTableInfo() |
||
163 | |||
164 | /** |
||
165 | * Make a custom word index. |
||
166 | * |
||
167 | * @param string $size |
||
168 | */ |
||
169 | public function create_word_search($size) |
||
187 | |||
188 | /** |
||
189 | * Static method that allows to retrieve or create an instance of this class. |
||
190 | */ |
||
191 | public static function db_search() |
||
199 | } |