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 | * Connect to a database using driver with parameters |
||
| 30 | * @param string $database Database name |
||
| 31 | * @param string $username Database username |
||
| 32 | * @param string $password Database password |
||
| 33 | * @param string $host Database host(localhost by default) |
||
| 34 | * @param int $port Database port(3306 by default) |
||
| 35 | * @param string $driver Database driver for interaction(MySQL by default) |
||
| 36 | * @param string $charset Database character set |
||
| 37 | * @return bool True if connection to database was successful |
||
| 38 | */ |
||
| 39 | public function connect( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get database name |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function database() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Create new database record |
||
| 73 | * @param string $entity Entity class name |
||
| 74 | * @return RecordInterface|null Entity instance |
||
| 75 | */ |
||
| 76 | public function entity($entity) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get entity query manager |
||
| 87 | * @param string $entity Entity identifier |
||
| 88 | * @return Query Query manager instance |
||
| 89 | */ |
||
| 90 | public function manager($entity) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * High-level database query executor |
||
| 97 | * @param string $sql SQL statement |
||
| 98 | * @return mixed Database query result |
||
| 99 | */ |
||
| 100 | public function &query($sql) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Retrieve array of records from a database, if $className is passed method |
||
| 127 | * will try to create an object of that type. If request has failed than |
||
| 128 | * method will return empty array of stdClass all arrays regarding to $className is |
||
| 129 | * passed or not. |
||
| 130 | * |
||
| 131 | * @param string $sql Query text |
||
| 132 | * @param string $className Class name if we want to create object |
||
| 133 | * @return array Collection of arrays or objects |
||
| 134 | */ |
||
| 135 | View Code Duplication | public function &fetch($sql, $className = null) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Special accelerated function to retrieve db record fields instead of objects |
||
| 167 | * |
||
| 168 | * @param string $className |
||
| 169 | * @param mixed $query |
||
| 170 | * @param string $field |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public function &fetchColumn($className, $query, $field) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Retrieve one record from a database, if $className is passed method |
||
| 207 | * will try to create an object of that type. If request has failed than |
||
| 208 | * method will return empty array or stdClass regarding to $className is |
||
| 209 | * passed or not. |
||
| 210 | * |
||
| 211 | * @param string $sql Query text |
||
| 212 | * @param string $className Class name if we want to create object |
||
| 213 | * @return array|object Record as array or object |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function &fetchOne($sql, $className = null) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Get one record from database by its field value |
||
| 248 | * @param string $className Enitity |
||
| 249 | * @param string $fieldName Field name |
||
| 250 | * @param string $fieldValue Field value |
||
| 251 | * @return object Found object instance or an empty stdClass instance |
||
| 252 | */ |
||
| 253 | public function fetchField($className, $fieldName, $fieldValue) |
||
| 262 | |||
| 263 | public function create($className, &$object = null) |
||
| 278 | |||
| 279 | public function update($className, &$object) |
||
| 290 | |||
| 291 | public function delete($className, &$object) |
||
| 298 | |||
| 299 | /** Count query result */ |
||
| 300 | public function count($className, $query) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Выполнить защиту значения поля для его безопасного использования в запросах |
||
| 313 | * |
||
| 314 | * @param string $value Значения поля для запроса |
||
| 315 | * @return string $value Безопасное представление значения поля для запроса |
||
| 316 | */ |
||
| 317 | protected function protectQueryValue($value) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Prepare create & update SQL statements fields |
||
| 333 | * @param string $className Entity name |
||
| 334 | * @param Record $object Database object to get values(if needed) |
||
| 335 | * @param bool $straight Way of forming SQL field statements |
||
| 336 | * @return array Collection of key => value with SQL fields statements |
||
| 337 | */ |
||
| 338 | protected function &getQueryFields($className, & $object = null, $straight = false) |
||
| 368 | |||
| 369 | /** @deprecated Use query() */ |
||
| 370 | public function &simple_query($sql) |
||
| 374 | } |
||
| 375 |
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..