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 | public function prepareAndFetchAll($sql,$parameters=null){ |
||
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @param string $sql |
||
| 134 | * @return \PDOStatement |
||
| 135 | */ |
||
| 136 | private function getStatement($sql) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
||
| 146 | * |
||
| 147 | * @param string $sql |
||
| 148 | * @return int the number of rows that were modified or deleted by the SQL statement you issued |
||
| 149 | */ |
||
| 150 | public function execute($sql) { |
||
| 153 | |||
| 154 | public function getServerName() { |
||
| 157 | |||
| 158 | public function setServerName($serverName) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Prepares a statement for execution and returns a statement object |
||
| 164 | * |
||
| 165 | * @param String $sql |
||
| 166 | * @return \PDOStatement |
||
| 167 | */ |
||
| 168 | public function prepareStatement($sql) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Sets $value to $parameter |
||
| 174 | * |
||
| 175 | * @param \PDOStatement $statement |
||
| 176 | * @param String $parameter |
||
| 177 | * @param mixed $value |
||
| 178 | * @return boolean |
||
| 179 | */ |
||
| 180 | public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns the last insert id |
||
| 186 | * |
||
| 187 | * @return integer |
||
| 188 | */ |
||
| 189 | public function lastInserId() { |
||
| 192 | |||
| 193 | public function getTablesName() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the number of records in $tableName that respects the condition passed as a parameter |
||
| 201 | * |
||
| 202 | * @param string $tableName |
||
| 203 | * @param string $condition |
||
| 204 | * Partie suivant le WHERE d'une instruction SQL |
||
| 205 | */ |
||
| 206 | public function count($tableName, $condition = '') { |
||
| 211 | |||
| 212 | public function queryColumn($query){ |
||
| 215 | |||
| 216 | public function fetchAll($query){ |
||
| 219 | |||
| 220 | public function isConnected() { |
||
| 223 | |||
| 224 | public function setDbType($dbType) { |
||
| 228 | |||
| 229 | public function ping() { |
||
| 232 | |||
| 233 | public function getPort() { |
||
| 236 | |||
| 237 | public function getDbName() { |
||
| 240 | |||
| 241 | public function getUser() { |
||
| 244 | |||
| 245 | public function getPdoObject() { |
||
| 248 | |||
| 249 | public static function getAvailableDrivers(){ |
||
| 252 | } |
||
| 253 |
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: