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:
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Database { |
||
| 18 | private $dbType; |
||
| 19 | private $serverName; |
||
| 20 | private $port; |
||
| 21 | private $dbName; |
||
| 22 | private $user; |
||
| 23 | private $password; |
||
| 24 | private $pdoObject; |
||
| 25 | private $statements = [ ]; |
||
| 26 | private $cache; |
||
| 27 | private $options; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | * |
||
| 32 | * @param string $dbName |
||
| 33 | * @param string $serverName |
||
| 34 | * @param string $port |
||
| 35 | * @param string $user |
||
| 36 | * @param string $password |
||
| 37 | * @param array $options |
||
| 38 | * @param boolean|string $cache |
||
| 39 | */ |
||
| 40 | public function __construct($dbType, $dbName, $serverName = "localhost", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Creates the PDO instance and realize a safe connection |
||
| 65 | * @return boolean true if connection is established |
||
| 66 | */ |
||
| 67 | public function connect() { |
||
| 75 | |||
| 76 | public function _connect(){ |
||
| 80 | |||
| 81 | public function getDSN(){ |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Executes an SQL statement, returning a result set as a PDOStatement object |
||
| 87 | * |
||
| 88 | * @param string $sql |
||
| 89 | * @return \PDOStatement |
||
| 90 | */ |
||
| 91 | public function query($sql) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * |
||
| 97 | * @param string $tableName |
||
| 98 | * @param string $condition |
||
| 99 | * @param array|string $fields |
||
| 100 | * @param array $parameters |
||
| 101 | * @param boolean|null $useCache |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | public function prepareAndExecute($tableName, $condition, $fields, $parameters=null,$useCache = NULL) { |
||
| 119 | |||
| 120 | View Code Duplication | public function prepareAndFetchAll($sql,$parameters=null){ |
|
| 130 | |||
| 131 | View Code Duplication | public function prepareAndFetchColumn($sql,$parameters=null,$column=null){ |
|
| 141 | |||
| 142 | /** |
||
| 143 | * |
||
| 144 | * @param string $sql |
||
| 145 | * @return \PDOStatement |
||
| 146 | */ |
||
| 147 | private function getStatement($sql) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
||
| 157 | * |
||
| 158 | * @param string $sql |
||
| 159 | * @return int the number of rows that were modified or deleted by the SQL statement you issued |
||
| 160 | */ |
||
| 161 | public function execute($sql) { |
||
| 164 | |||
| 165 | public function getServerName() { |
||
| 168 | |||
| 169 | public function setServerName($serverName) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Prepares a statement for execution and returns a statement object |
||
| 175 | * |
||
| 176 | * @param String $sql |
||
| 177 | * @return \PDOStatement |
||
| 178 | */ |
||
| 179 | public function prepareStatement($sql) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Sets $value to $parameter |
||
| 185 | * |
||
| 186 | * @param \PDOStatement $statement |
||
| 187 | * @param String $parameter |
||
| 188 | * @param mixed $value |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the last insert id |
||
| 197 | * |
||
| 198 | * @return integer |
||
| 199 | */ |
||
| 200 | public function lastInserId() { |
||
| 203 | |||
| 204 | public function getTablesName() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the number of records in $tableName that respects the condition passed as a parameter |
||
| 212 | * |
||
| 213 | * @param string $tableName |
||
| 214 | * @param string $condition |
||
| 215 | * Partie suivant le WHERE d'une instruction SQL |
||
| 216 | */ |
||
| 217 | public function count($tableName, $condition = '') { |
||
| 222 | |||
| 223 | public function queryColumn($query){ |
||
| 226 | |||
| 227 | public function fetchAll($query){ |
||
| 230 | |||
| 231 | public function isConnected() { |
||
| 234 | |||
| 235 | public function setDbType($dbType) { |
||
| 239 | |||
| 240 | public function ping() { |
||
| 243 | |||
| 244 | public function getPort() { |
||
| 247 | |||
| 248 | public function getDbName() { |
||
| 251 | |||
| 252 | public function getUser() { |
||
| 255 | |||
| 256 | public function getPdoObject() { |
||
| 259 | |||
| 260 | public static function getAvailableDrivers(){ |
||
| 263 | } |
||
| 264 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: