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 |
||
| 15 | trait DatabaseTrait |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var \CI_DB |
||
| 19 | */ |
||
| 20 | protected $db; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var \CI_DB_result |
||
| 24 | */ |
||
| 25 | protected $query; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Parses the table name from Describe class. |
||
| 29 | * |
||
| 30 | * @param string $table |
||
| 31 | * @param boolean $isForeignKey |
||
| 32 | * @return string |
||
| 33 | */ |
||
| 34 | 36 | protected function getTableName($table, $isForeignKey = false) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Sets the database class. |
||
| 63 | * |
||
| 64 | * @param \CI_DB|null $database |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | 39 | public function setDatabase($database = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Sets the query result. |
||
| 88 | * |
||
| 89 | * @param \CI_DB_result $query |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | 3 | public function setQuery($query) |
|
| 98 | } |
||
| 99 |
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.