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 |
||
| 14 | class Database |
||
| 15 | { |
||
| 16 | /** @var \PDO Database driver */ |
||
| 17 | protected $driver; |
||
| 18 | |||
| 19 | /** @var string Database name */ |
||
| 20 | protected $database; |
||
| 21 | |||
| 22 | /** @var int Amount of miliseconds spent on queries */ |
||
| 23 | protected $elapsed; |
||
| 24 | |||
| 25 | /** @var int Amount queries executed */ |
||
| 26 | protected $count; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Get database name |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | public function database() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Connect to a database using driver with parameters |
||
| 39 | * @param string $database Database name |
||
| 40 | * @param string $username Database username |
||
| 41 | * @param string $password Database password |
||
| 42 | * @param string $host Database host(localhost by default) |
||
| 43 | * @param int $port Database port(3306 by default) |
||
| 44 | * @param string $driver Database driver for interaction(MySQL by default) |
||
| 45 | * @param string $charset Database character set |
||
| 46 | * @return bool True if connection to database was successful |
||
| 47 | */ |
||
| 48 | public function connect( |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create new database record |
||
| 74 | * @param string $className Entity class name |
||
| 75 | */ |
||
| 76 | public function entity($className) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * High-level database query executor |
||
| 83 | * @param string $sql SQL statement |
||
| 84 | * @return mixed Database query result |
||
| 85 | */ |
||
| 86 | public function &query($sql) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Retrieve array of records from a database, if $className is passed method |
||
| 113 | * will try to create an object of that type. If request has failed than |
||
| 114 | * method will return empty array of stdClass all arrays regarding to $className is |
||
| 115 | * passed or not. |
||
| 116 | * |
||
| 117 | * @param string $sql Query text |
||
| 118 | * @param string $className Class name if we want to create object |
||
| 119 | * @return array Collection of arrays or objects |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function &fetch($sql, $className = null) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Special accelerated function to retrieve db record fields instead of objects |
||
| 153 | * |
||
| 154 | * @param string $className |
||
| 155 | * @param mixed $query |
||
| 156 | * @param string $field |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | */ |
||
| 160 | public function &fetchColumn($className, $query, $field) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Retrieve one record from a database, if $className is passed method |
||
| 193 | * will try to create an object of that type. If request has failed than |
||
| 194 | * method will return empty array or stdClass regarding to $className is |
||
| 195 | * passed or not. |
||
| 196 | * |
||
| 197 | * @param string $sql Query text |
||
| 198 | * @param string $className Class name if we want to create object |
||
| 199 | * @return array|object Record as array or object |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function &fetchOne($sql, $className = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get collection record from database by its field value |
||
| 234 | * @param string $className Enitity |
||
| 235 | * @param string $fieldName Field name |
||
| 236 | * @param string $fieldValue Field value |
||
| 237 | * @return object[] Found object instance or an empty stdClass instance |
||
| 238 | */ |
||
| 239 | View Code Duplication | public function fetchCollectionByField($className, $fieldName, $fieldValue) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Get one record from database by its field value |
||
| 251 | * @param string $className Enitity |
||
| 252 | * @param string $fieldName Field name |
||
| 253 | * @param string $fieldValue Field value |
||
| 254 | * @return object Found object instance or an empty stdClass instance |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function fetchField($className, $fieldName, $fieldValue) |
|
| 265 | |||
| 266 | public function create($className, & $object = null) |
||
| 281 | |||
| 282 | public function update($className, & $object) |
||
| 293 | |||
| 294 | public function delete($className, & $object) |
||
| 301 | |||
| 302 | /** Count query result */ |
||
| 303 | public function count($className, $query) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Выполнить защиту значения поля для его безопасного использования в запросах |
||
| 316 | * |
||
| 317 | * @param string $value Значения поля для запроса |
||
| 318 | * @return string $value Безопасное представление значения поля для запроса |
||
| 319 | */ |
||
| 320 | protected function protectQueryValue($value) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Prepare create & update SQL statements fields |
||
| 336 | * @param string $className Entity name |
||
| 337 | * @param Record $object Database object to get values(if needed) |
||
| 338 | * @param bool $straight Way of forming SQL field statements |
||
| 339 | * @return array Collection of key => value with SQL fields statements |
||
| 340 | */ |
||
| 341 | protected function &getQueryFields($className, & $object = null, $straight = false) |
||
| 371 | |||
| 372 | /** @deprecated Use query() */ |
||
| 373 | public function &simple_query($sql) |
||
| 377 | } |
||
| 378 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..