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 |
||
30 | class LegacyInspector extends Client |
||
31 | { |
||
32 | use InspectorTrait; |
||
33 | |||
34 | /** |
||
35 | * getClientType |
||
36 | * |
||
37 | * @see ClientInterface |
||
38 | */ |
||
39 | public function getClientType() |
||
43 | |||
44 | /** |
||
45 | * getClientIdentifier |
||
46 | * |
||
47 | * @see ClientInterface |
||
48 | */ |
||
49 | public function getClientIdentifier() |
||
53 | |||
54 | /** |
||
55 | * getSchemas |
||
56 | * |
||
57 | * Return a list of available schemas in the current database. |
||
58 | * |
||
59 | * @return \PommProject\Foundation\ConvertedResultIterator |
||
60 | * @deprecated |
||
61 | */ |
||
62 | public function getSchemas() |
||
85 | |||
86 | /** |
||
87 | * getTableOid |
||
88 | * |
||
89 | * Return the table oid from PostgreSQL catalog. If no table is found, null |
||
90 | * is returned. |
||
91 | * |
||
92 | * @param string $schema |
||
93 | * @param string $table |
||
94 | * @return int|null |
||
95 | * @deprecated |
||
96 | */ |
||
97 | View Code Duplication | public function getTableOid($schema, $table) |
|
117 | |||
118 | /** |
||
119 | * getTableFieldInformation |
||
120 | * |
||
121 | * Get table's field information. If no fields are found, null is |
||
122 | * returned. |
||
123 | * |
||
124 | * @param int $oid |
||
125 | * @return \PommProject\Foundation\ConvertedResultIterator|null |
||
126 | * @deprecated |
||
127 | */ |
||
128 | View Code Duplication | public function getTableFieldInformation($oid) |
|
162 | |||
163 | /** |
||
164 | * getSchemaOid |
||
165 | * |
||
166 | * Return the given schema oid, null if the schema is not found. |
||
167 | * |
||
168 | * @param string $schema |
||
169 | * @param Where $where optional where clause. |
||
170 | * @return int|null |
||
171 | * @deprecated |
||
172 | */ |
||
173 | View Code Duplication | public function getSchemaOid($schema, Where $where = null) |
|
192 | |||
193 | /** |
||
194 | * getPrimaryKey |
||
195 | * |
||
196 | * Get relation's primary key if any. |
||
197 | * |
||
198 | * @param int $table_oid |
||
199 | * @return array|null |
||
200 | * @deprecated |
||
201 | */ |
||
202 | public function getPrimaryKey($table_oid) |
||
203 | { |
||
204 | $sql = <<<SQL |
||
205 | with |
||
206 | pk_field as ( |
||
207 | select |
||
208 | att.attname as field |
||
209 | from |
||
210 | pg_catalog.pg_attribute att |
||
211 | join pg_catalog.pg_index ind on |
||
212 | att.attrelid = ind.indrelid and att.attnum = any(ind.indkey) |
||
213 | where |
||
214 | {condition} |
||
215 | order by att.attnum asc |
||
216 | ) |
||
217 | select array_agg(field) as fields from pk_field |
||
218 | SQL; |
||
219 | $condition = |
||
220 | Where::create('ind.indrelid = $*', [$table_oid]) |
||
221 | ->andWhere('ind.indisprimary') |
||
222 | ; |
||
223 | |||
224 | $pk = $this |
||
225 | ->executeSql($sql, $condition) |
||
226 | ->current() |
||
227 | ; |
||
228 | |||
229 | return ($pk['fields'] === null || $pk['fields'][0] === null) ? [] : array_reverse($pk['fields']); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * getSchemaRelations |
||
234 | * |
||
235 | * Return information on relations in a given schema. An additional Where |
||
236 | * condition can be passed to filter against other criteria. |
||
237 | * |
||
238 | * @param int $schema_oid |
||
239 | * @param Where $where |
||
240 | * @return \PommProject\Foundation\ConvertedResultIterator |
||
241 | * @deprecated |
||
242 | */ |
||
243 | View Code Duplication | public function getSchemaRelations($schema_oid, Where $where = null) |
|
272 | |||
273 | /** |
||
274 | * getTableComment |
||
275 | * |
||
276 | * Return the comment on a table if set. Null otherwise. |
||
277 | * |
||
278 | * @param int $table_oid |
||
279 | * @return string|null |
||
280 | * @deprecated |
||
281 | */ |
||
282 | View Code Duplication | public function getTableComment($table_oid) |
|
293 | |||
294 | /** |
||
295 | * getTypeInformation |
||
296 | * |
||
297 | * Return the Oid of the given type name. |
||
298 | * It Additionally returns the type category. |
||
299 | * |
||
300 | * @param string $type_name |
||
301 | * @param string $type_schema |
||
302 | * @return array|null |
||
303 | * @deprecated |
||
304 | */ |
||
305 | public function getTypeInformation($type_name, $type_schema = null) |
||
329 | |||
330 | /** |
||
331 | * getTypeCategory |
||
332 | * |
||
333 | * Get type category. |
||
334 | * |
||
335 | * @param int $oid |
||
336 | * @return array|null |
||
337 | * @deprecated |
||
338 | */ |
||
339 | View Code Duplication | public function getTypeCategory($oid) |
|
358 | |||
359 | /** |
||
360 | * getTypeEnumValues |
||
361 | * |
||
362 | * Return all possible values from an enumerated type in its natural order. |
||
363 | * |
||
364 | * @param int $oid |
||
365 | * @return array|null |
||
366 | * @deprecated |
||
367 | */ |
||
368 | public function getTypeEnumValues($oid) |
||
390 | |||
391 | /** |
||
392 | * getCompositeInformation |
||
393 | * |
||
394 | * Return the structure of a composite row. |
||
395 | * |
||
396 | * @param int $oid |
||
397 | * @return \PommProject\Foundation\ConvertedResultIterator |
||
398 | * @deprecated |
||
399 | */ |
||
400 | public function getCompositeInformation($oid) |
||
417 | |||
418 | /** |
||
419 | * getVersion |
||
420 | * |
||
421 | * Return server version. |
||
422 | * |
||
423 | * @throws FoundationException if invalid string. |
||
424 | * @return string |
||
425 | * @deprecated |
||
426 | */ |
||
427 | public function getVersion() |
||
436 | } |
||
437 |
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.