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 | class Database |
||
16 | { |
||
17 | /** @var \PDO Database driver */ |
||
18 | protected $driver; |
||
19 | |||
20 | /** @var string Database name */ |
||
21 | protected $database; |
||
22 | |||
23 | /** @var int Amount of milliseconds spent on queries */ |
||
24 | protected $elapsed; |
||
25 | |||
26 | /** @var int Amount queries executed */ |
||
27 | protected $count; |
||
28 | |||
29 | /** |
||
30 | * Connect to a database using driver with parameters |
||
31 | * @param string $database Database name |
||
32 | * @param string $username Database username |
||
33 | * @param string $password Database password |
||
34 | * @param string $host Database host(localhost by default) |
||
35 | * @param int $port Database port(3306 by default) |
||
36 | * @param string $driver Database driver for interaction(MySQL by default) |
||
37 | * @param string $charset Database character set |
||
38 | * @return bool True if connection to database was successful |
||
39 | */ |
||
40 | public function connect( |
||
62 | |||
63 | /** |
||
64 | * Get database name |
||
65 | * @return string |
||
66 | */ |
||
67 | public function database() |
||
71 | |||
72 | /** |
||
73 | * Create new database record |
||
74 | * @param string $entity Entity class name |
||
75 | * @return null|RecordInterface Entity instance |
||
76 | * @throws EntityNotFound |
||
77 | */ |
||
78 | public function entity($entity) |
||
86 | |||
87 | /** |
||
88 | * Get entity query manager |
||
89 | * @param string $entity Entity identifier |
||
90 | * @return Query Query manager instance |
||
91 | */ |
||
92 | public function manager($entity) |
||
96 | |||
97 | /** |
||
98 | * High-level database query executor |
||
99 | * @param string $sql SQL statement |
||
100 | * @return mixed Database query result |
||
101 | */ |
||
102 | View Code Duplication | public function &query($sql) |
|
103 | { |
||
104 | $result = array(); |
||
105 | |||
106 | if (isset($this->driver)) { |
||
107 | // Store timestamp |
||
108 | $tsLast = microtime(true); |
||
109 | |||
110 | try { |
||
111 | // Perform database query |
||
112 | $result = $this->driver->prepare($sql)->execute(); |
||
113 | } catch (\PDOException $e) { |
||
114 | echo("\n" . $sql . '-' . $e->getMessage()); |
||
115 | } |
||
116 | |||
117 | // Store queries count |
||
118 | $this->count++; |
||
119 | |||
120 | // Count elapsed time |
||
121 | $this->elapsed += microtime(true) - $tsLast; |
||
122 | } |
||
123 | |||
124 | return $result; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Retrieve array of records from a database, if $className is passed method |
||
129 | * will try to create an object of that type. If request has failed than |
||
130 | * method will return empty array of stdClass all arrays regarding to $className is |
||
131 | * passed or not. |
||
132 | * |
||
133 | * @param string $sql Query text |
||
134 | * @param string $className Class name if we want to create object |
||
135 | * @return array Collection of arrays or objects |
||
136 | */ |
||
137 | View Code Duplication | public function &fetch($sql, $className = null) |
|
166 | |||
167 | /** |
||
168 | * Special accelerated function to retrieve db record fields instead of objects |
||
169 | * |
||
170 | * @param string $className |
||
171 | * @param mixed $query |
||
172 | * @param string $field |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function &fetchColumn($className, $query, $field) |
||
206 | |||
207 | /** |
||
208 | * Retrieve one record from a database, if $className is passed method |
||
209 | * will try to create an object of that type. If request has failed than |
||
210 | * method will return empty array or stdClass regarding to $className is |
||
211 | * passed or not. |
||
212 | * |
||
213 | * @param string $sql Query text |
||
214 | * @param string $className Class name if we want to create object |
||
215 | * @return array|object Record as array or object |
||
216 | */ |
||
217 | View Code Duplication | public function &fetchOne($sql, $className = null) |
|
247 | |||
248 | public function create($className, &$object = null) |
||
261 | |||
262 | public function update($className, &$object) |
||
269 | |||
270 | public function delete($className, &$object) |
||
276 | |||
277 | /** Count query result */ |
||
278 | public function count($className, $query) |
||
288 | |||
289 | /** |
||
290 | * Выполнить защиту значения поля для его безопасного использования в запросах |
||
291 | * |
||
292 | * @param string $value Значения поля для запроса |
||
293 | * @return string $value Безопасное представление значения поля для запроса |
||
294 | */ |
||
295 | protected function protectQueryValue($value) |
||
308 | |||
309 | /** |
||
310 | * Prepare create & update SQL statements fields |
||
311 | * @param string $className Entity name |
||
312 | * @param Record $object Database object to get values(if needed) |
||
313 | * @param bool $straight Way of forming SQL field statements |
||
314 | * @return array Collection of key => value with SQL fields statements |
||
315 | */ |
||
316 | protected function &getQueryFields($className, & $object = null, $straight = false) |
||
346 | |||
347 | /** @deprecated Use query() */ |
||
348 | public function &simple_query($sql) |
||
352 | } |
||
353 |
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..