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 |
||
| 16 | class Database { |
||
| 17 | private $dbType; |
||
| 18 | private $serverName; |
||
| 19 | private $port; |
||
| 20 | private $dbName; |
||
| 21 | private $user; |
||
| 22 | private $password; |
||
| 23 | private $pdoObject; |
||
| 24 | private $statements = [ ]; |
||
| 25 | private $cache; |
||
| 26 | private $options; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | * |
||
| 31 | * @param string $dbName |
||
| 32 | * @param string $serverName |
||
| 33 | * @param string $port |
||
| 34 | * @param string $user |
||
| 35 | * @param string $password |
||
| 36 | * @param array $options |
||
| 37 | * @param boolean|string $cache |
||
| 38 | */ |
||
| 39 | public function __construct($dbType, $dbName, $serverName = "localhost", $port = "3306", $user = "root", $password = "", $options = [], $cache = false) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Creates the PDO instance and realize a safe connection |
||
| 64 | * @return boolean true if connection is established |
||
| 65 | */ |
||
| 66 | public function connect() { |
||
| 74 | |||
| 75 | public function _connect(){ |
||
| 79 | |||
| 80 | public function getDSN(){ |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Executes an SQL statement, returning a result set as a PDOStatement object |
||
| 86 | * |
||
| 87 | * @param string $sql |
||
| 88 | * @return \PDOStatement |
||
| 89 | */ |
||
| 90 | public function query($sql) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * |
||
| 96 | * @param string $tableName |
||
| 97 | * @param string $condition |
||
| 98 | * @param array|string $fields |
||
| 99 | * @param boolean|null $useCache |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function prepareAndExecute($tableName, $condition, $fields, $useCache = NULL) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | * @param string $sql |
||
| 124 | * @return \PDOStatement |
||
| 125 | */ |
||
| 126 | private function getStatement($sql) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Execute an SQL statement and return the number of affected rows (INSERT, UPDATE or DELETE) |
||
| 136 | * |
||
| 137 | * @param string $sql |
||
| 138 | * @return int the number of rows that were modified or deleted by the SQL statement you issued |
||
| 139 | */ |
||
| 140 | public function execute($sql) { |
||
| 143 | |||
| 144 | public function getServerName() { |
||
| 147 | |||
| 148 | public function setServerName($serverName) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Prepares a statement for execution and returns a statement object |
||
| 154 | * |
||
| 155 | * @param String $sql |
||
| 156 | * @return \PDOStatement |
||
| 157 | */ |
||
| 158 | public function prepareStatement($sql) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets $value to $parameter |
||
| 164 | * |
||
| 165 | * @param \PDOStatement $statement |
||
| 166 | * @param String $parameter |
||
| 167 | * @param mixed $value |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | public function bindValueFromStatement(\PDOStatement $statement, $parameter, $value) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the last insert id |
||
| 176 | * |
||
| 177 | * @return integer |
||
| 178 | */ |
||
| 179 | public function lastInserId() { |
||
| 182 | |||
| 183 | public function getTablesName() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns the number of records in $tableName that respects the condition passed as a parameter |
||
| 191 | * |
||
| 192 | * @param string $tableName |
||
| 193 | * @param string $condition |
||
| 194 | * Partie suivant le WHERE d'une instruction SQL |
||
| 195 | */ |
||
| 196 | public function count($tableName, $condition = '') { |
||
| 201 | |||
| 202 | public function queryColumn($query){ |
||
| 205 | |||
| 206 | public function fetchAll($query){ |
||
| 209 | |||
| 210 | public function isConnected() { |
||
| 213 | |||
| 214 | public function setDbType($dbType) { |
||
| 218 | |||
| 219 | public function ping() { |
||
| 222 | |||
| 223 | public function getPort() { |
||
| 226 | |||
| 227 | public function getDbName() { |
||
| 230 | |||
| 231 | public function getUser() { |
||
| 234 | |||
| 235 | public function getPdoObject() { |
||
| 238 | |||
| 239 | public static function getAvailableDrivers(){ |
||
| 242 | } |
||
| 243 |
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: