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 |
||
| 6 | class Categories |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var DBAPI |
||
| 10 | */ |
||
| 11 | public $db; |
||
| 12 | public $db_tbl = array(); |
||
| 13 | public $elements = array('templates', 'tmplvars', 'htmlsnippets', 'snippets', 'plugins', 'modules'); |
||
| 14 | |||
| 15 | public function __construct() |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * Get all categories |
||
| 30 | * @return array $categories / array contains all categories |
||
| 31 | */ |
||
| 32 | public function getCategories() |
||
| 33 | { |
||
| 34 | $categories = $this->db->makeArray( |
||
| 35 | $this->db->select( |
||
| 36 | '*', |
||
| 37 | $this->db_tbl['categories'], |
||
| 38 | '1', |
||
| 39 | '`rank`,`category`' |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | return empty($categories) ? array() : $categories; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $search |
||
| 48 | * @param string $where |
||
| 49 | * @return array|bool|object|stdClass |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function getCategory($search, $where = 'category') |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $value |
||
| 66 | * @param string $search |
||
| 67 | * @param string $where |
||
| 68 | * @return bool|int|string |
||
| 69 | */ |
||
| 70 | View Code Duplication | public function getCategoryValue($value, $search, $where = 'category') |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param int $category_id |
||
| 85 | * @param string $element |
||
| 86 | * @return array|bool |
||
| 87 | */ |
||
| 88 | public function getAssignedElements($category_id, $element) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param int $category_id |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getAllAssignedElements($category_id) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param int $category_id |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | public function deleteCategory($category_id) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param int $category_id |
||
| 152 | * @param array $data |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function updateCategory($category_id, $data = array()) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $category_name |
||
| 181 | * @param int $category_rank |
||
| 182 | * @return bool|int|mixed |
||
| 183 | */ |
||
| 184 | public function addCategory($category_name, $category_rank) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $category_name |
||
| 209 | * @return bool|int|string |
||
| 210 | */ |
||
| 211 | public function isCategoryExists($category_name) |
||
| 229 | } |
||
| 230 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.