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 |
||
| 12 | abstract class CustomColumnType |
||
|
|
|||
| 13 | { |
||
| 14 | const ALL_CUSTOMS_ID = "cops:custom"; |
||
| 15 | |||
| 16 | const CUSTOM_TYPE_TEXT = "text"; // type 1 + 2 |
||
| 17 | const CUSTOM_TYPE_COMMENT = "comments"; // type 3 |
||
| 18 | const CUSTOM_TYPE_SERIES = "series"; // type 4 |
||
| 19 | const CUSTOM_TYPE_ENUM = "enumeration"; // type 5 |
||
| 20 | const CUSTOM_TYPE_DATE = "datetime"; // type 6 |
||
| 21 | const CUSTOM_TYPE_FLOAT = "float"; // type 7 |
||
| 22 | const CUSTOM_TYPE_INT = "int"; // type 8 |
||
| 23 | const CUSTOM_TYPE_RATING = "rating"; // type 9 |
||
| 24 | const CUSTOM_TYPE_BOOL = "bool"; // type 10 |
||
| 25 | const CUSTOM_TYPE_COMPOSITE = "composite"; // type 11 + 12 |
||
| 26 | |||
| 27 | /** @var array[integer]CustomColumnType */ |
||
| 28 | private static $customColumnCacheID = array(); |
||
| 29 | |||
| 30 | /** @var array[string]CustomColumnType */ |
||
| 31 | private static $customColumnCacheLookup = array(); |
||
| 32 | |||
| 33 | /** @var integer the id of this column */ |
||
| 34 | public $customId; |
||
| 35 | /** @var string name/title of this column */ |
||
| 36 | public $columnTitle; |
||
| 37 | /** @var string the datatype of this column (one of the CUSTOM_TYPE_* constant values) */ |
||
| 38 | public $datatype; |
||
| 39 | /** @var null|Entry[] */ |
||
| 40 | private $customValues = NULL; |
||
| 41 | |||
| 42 | 14 | protected function __construct($pcustomId, $pdatatype) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * The URI to show all book swith a specific value in this column |
||
| 52 | * |
||
| 53 | * @param string|integer $id the id of the value to show |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 13 | public function getUri($id) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * The URI to show all the values of this column |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | 25 | public function getUriAllCustoms() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * The EntryID to show all book swith a specific value in this column |
||
| 73 | * |
||
| 74 | * @param string|integer $id the id of the value to show |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 17 | public function getEntryId($id) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * The EntryID to show all the values of this column |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | 37 | public function getAllCustomsId() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * The title of this column |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 37 | public function getTitle() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * The description of this column as it is definied in the database |
||
| 104 | * |
||
| 105 | * @return string|null |
||
| 106 | */ |
||
| 107 | 20 | public function getDatabaseDescription() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Get the Entry for this column |
||
| 120 | * This is used in the initializeContent method to display e.g. the index page |
||
| 121 | * |
||
| 122 | * @return Entry |
||
| 123 | */ |
||
| 124 | 14 | public function getCount() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get the amount of distinct values for this column |
||
| 139 | * |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | 16 | protected function getDistinctValueCount() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Encode a value of this column ready to be displayed in an HTML document |
||
| 149 | * |
||
| 150 | * @param integer|string $value |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 7 | public function encodeHTMLValue($value) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get the datatype of a CustomColumn by its customID |
||
| 160 | * |
||
| 161 | * @param integer $customId |
||
| 162 | * @return string|null |
||
| 163 | */ |
||
| 164 | 17 | View Code Duplication | private static function getDatatypeByCustomID($customId) |
| 173 | |||
| 174 | /** |
||
| 175 | * Create a CustomColumnType by CustomID |
||
| 176 | * |
||
| 177 | * @param integer $customId the id of the custom column |
||
| 178 | * @return CustomColumnType|null |
||
| 179 | * @throws Exception If the $customId is not found or the datatype is unknown |
||
| 180 | */ |
||
| 181 | 42 | public static function createByCustomID($customId) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Create a CustomColumnType by its lookup name |
||
| 217 | * |
||
| 218 | * @param string $lookup the lookup-name of the custom column |
||
| 219 | * @return CustomColumnType|null |
||
| 220 | */ |
||
| 221 | 33 | public static function createByLookup($lookup) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Return an entry array for all possible (in the DB used) values of this column |
||
| 237 | * These are the values used in the getUriAllCustoms() page |
||
| 238 | * |
||
| 239 | * @return Entry[] |
||
| 240 | */ |
||
| 241 | 36 | public function getAllCustomValues() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get the title of a CustomColumn by its customID |
||
| 252 | * |
||
| 253 | * @param integer $customId |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 14 | View Code Duplication | protected static function getTitleByCustomID($customId) |
| 265 | |||
| 266 | /** |
||
| 267 | * Get the query to find all books with a specific value of this column |
||
| 268 | * the returning array has two values: |
||
| 269 | * - first the query (string) |
||
| 270 | * - second an array of all PreparedStatement parameters |
||
| 271 | * |
||
| 272 | * @param string|integer $id the id of the searched value |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | abstract public function getQuery($id); |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get a CustomColumn for a specified (by ID) value |
||
| 279 | * |
||
| 280 | * @param string|integer $id the id of the searched value |
||
| 281 | * @return CustomColumn |
||
| 282 | */ |
||
| 283 | abstract public function getCustom($id); |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return an entry array for all possible (in the DB used) values of this column by querying the database |
||
| 287 | * |
||
| 288 | * @return Entry[] |
||
| 289 | */ |
||
| 290 | abstract protected function getAllCustomValuesFromDatabase(); |
||
| 291 | |||
| 292 | /** |
||
| 293 | * The description used in the index page |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | abstract public function getDescription(); |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Find the value of this column for a specific book |
||
| 301 | * |
||
| 302 | * @param Book $book |
||
| 303 | * @return CustomColumn |
||
| 304 | */ |
||
| 305 | public abstract function getCustomByBook($book); |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Is this column searchable by value |
||
| 309 | * only searchable columns can be displayed on the index page |
||
| 310 | * |
||
| 311 | * @return bool |
||
| 312 | */ |
||
| 313 | public abstract function isSearchable(); |
||
| 314 | } |
||
| 315 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.