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  | 
            ||
| 15 | class Database  | 
            ||
| 16 | { | 
            ||
| 17 | /**  | 
            ||
| 18 | * @var Connector  | 
            ||
| 19 | */  | 
            ||
| 20 | protected $connector;  | 
            ||
| 21 | |||
| 22 | /**  | 
            ||
| 23 | * @var \PDO  | 
            ||
| 24 | */  | 
            ||
| 25 | protected $pdo;  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * @var \PDOStatement  | 
            ||
| 29 | */  | 
            ||
| 30 | protected $statement;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @var Query  | 
            ||
| 34 | */  | 
            ||
| 35 | protected $query;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @var Grammar  | 
            ||
| 39 | */  | 
            ||
| 40 | protected $grammar;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * @var array  | 
            ||
| 44 | */  | 
            ||
| 45 | protected $driver = [  | 
            ||
| 46 | "mysql" => [  | 
            ||
| 47 | "connector" => ConnectorMySql::class,  | 
            ||
| 48 | "grammar" => GrammarMySql::class,  | 
            ||
| 49 | ]  | 
            ||
| 50 | ];  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * @var array  | 
            ||
| 54 | */  | 
            ||
| 55 | protected $config = [  | 
            ||
| 56 | "driver" => "mysql",  | 
            ||
| 57 | |||
| 58 | "host" => "",  | 
            ||
| 59 | "username" => "",  | 
            ||
| 60 | "password" => "",  | 
            ||
| 61 | |||
| 62 | "dbname" => "",  | 
            ||
| 63 | "options" => [  | 
            ||
| 64 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  | 
            ||
| 65 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC  | 
            ||
| 66 | ]  | 
            ||
| 67 | ];  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * @var string  | 
            ||
| 71 | */  | 
            ||
| 72 | protected $model = null;  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * @param $config  | 
            ||
| 76 | */  | 
            ||
| 77 | public function __construct($config)  | 
            ||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * @param $config  | 
            ||
| 93 | * @return static  | 
            ||
| 94 | */  | 
            ||
| 95 | public static function create($config)  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | *  | 
            ||
| 102 | */  | 
            ||
| 103 | protected function initialize()  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * @return PDO  | 
            ||
| 116 | */  | 
            ||
| 117 | public function createConnection()  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * @param $query  | 
            ||
| 125 | * @param null $params  | 
            ||
| 126 | * @return $this  | 
            ||
| 127 | */  | 
            ||
| 128 | public function sql($query, $params = null)  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * @param $table  | 
            ||
| 136 | * @return $this  | 
            ||
| 137 | */  | 
            ||
| 138 | public function table($table)  | 
            ||
| 143 | |||
| 144 | /**  | 
            ||
| 145 | * @param null $columns  | 
            ||
| 146 | * @return $this  | 
            ||
| 147 | */  | 
            ||
| 148 | public function select($columns = null)  | 
            ||
| 153 | |||
| 154 | /**  | 
            ||
| 155 | * @param array $columnsVal  | 
            ||
| 156 | * @param bool $runExecute  | 
            ||
| 157 | * @return mixed  | 
            ||
| 158 | */  | 
            ||
| 159 | public function insert(array $columnsVal, $runExecute = false)  | 
            ||
| 172 | |||
| 173 | /**  | 
            ||
| 174 | * @param array $columnsVal  | 
            ||
| 175 | * @param null $primaryKey  | 
            ||
| 176 | * @return mixed  | 
            ||
| 177 | */  | 
            ||
| 178 | public function update(array $columnsVal, $primaryKey = null)  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @param null $primaryKey  | 
            ||
| 199 | * @param null $primaryKeyVal  | 
            ||
| 200 | * @return mixed  | 
            ||
| 201 | */  | 
            ||
| 202 | public function delete($primaryKey = null, $primaryKeyVal = null)  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * @param $query  | 
            ||
| 219 | * @param string $type  | 
            ||
| 220 | * @return Query  | 
            ||
| 221 | */  | 
            ||
| 222 | public function whereQ($query, $type = "AND")  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * @param $column  | 
            ||
| 230 | * @param $value  | 
            ||
| 231 | * @param string $operator  | 
            ||
| 232 | * @param string $type  | 
            ||
| 233 | * @return $this  | 
            ||
| 234 | */  | 
            ||
| 235 | public function where($column, $value, $operator = "=", $type = "AND")  | 
            ||
| 240 | |||
| 241 | /**  | 
            ||
| 242 | * @param $column  | 
            ||
| 243 | * @param $value  | 
            ||
| 244 | * @param string $operator  | 
            ||
| 245 | * @return $this  | 
            ||
| 246 | */  | 
            ||
| 247 | public function andWhere($column, $value, $operator = "=")  | 
            ||
| 252 | |||
| 253 | /**  | 
            ||
| 254 | * @param $column  | 
            ||
| 255 | * @param $value  | 
            ||
| 256 | * @param string $operator  | 
            ||
| 257 | * @return $this  | 
            ||
| 258 | */  | 
            ||
| 259 | public function orWhere($column, $value, $operator = "=")  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * @param $column  | 
            ||
| 267 | * @param $value  | 
            ||
| 268 | * @param string $type  | 
            ||
| 269 | * @return $this  | 
            ||
| 270 | */  | 
            ||
| 271 | public function like($column, $value, $type = "AND")  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @param $column  | 
            ||
| 279 | * @param $value  | 
            ||
| 280 | * @return $this  | 
            ||
| 281 | */  | 
            ||
| 282 | public function andLike($column, $value)  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * @param $column  | 
            ||
| 290 | * @param $value  | 
            ||
| 291 | * @return $this  | 
            ||
| 292 | */  | 
            ||
| 293 | public function orLike($column, $value)  | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * @param $column  | 
            ||
| 301 | * @param $value  | 
            ||
| 302 | * @param string $type  | 
            ||
| 303 | * @return $this  | 
            ||
| 304 | */  | 
            ||
| 305 | public function between($column, $value, $type = "AND")  | 
            ||
| 310 | |||
| 311 | /**  | 
            ||
| 312 | * @param $column  | 
            ||
| 313 | * @param $value  | 
            ||
| 314 | * @return $this  | 
            ||
| 315 | */  | 
            ||
| 316 | public function andBetween($column, $value)  | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * @param $column  | 
            ||
| 324 | * @param $value  | 
            ||
| 325 | * @return $this  | 
            ||
| 326 | */  | 
            ||
| 327 | public function orBetween($column, $value)  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * @return bool  | 
            ||
| 335 | */  | 
            ||
| 336 | public function execute()  | 
            ||
| 344 | |||
| 345 | /**  | 
            ||
| 346 | * @return array  | 
            ||
| 347 | * @throws \Exception  | 
            ||
| 348 | */  | 
            ||
| 349 | public function all()  | 
            ||
| 365 | |||
| 366 | /**  | 
            ||
| 367 | * @return mixed  | 
            ||
| 368 | */  | 
            ||
| 369 | public function first()  | 
            ||
| 376 | |||
| 377 | /**  | 
            ||
| 378 | * @return mixed  | 
            ||
| 379 | * @throws \Exception  | 
            ||
| 380 | */  | 
            ||
| 381 | protected function get()  | 
            ||
| 392 | |||
| 393 | /**  | 
            ||
| 394 | * @throws \Exception  | 
            ||
| 395 | */  | 
            ||
| 396 | protected function executeIfNotAlreadyExecutedPreviously()  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * Clear statement PDO and query builder  | 
            ||
| 405 | */  | 
            ||
| 406 | public function clear()  | 
            ||
| 414 | |||
| 415 | /**  | 
            ||
| 416 | *  | 
            ||
| 417 | */  | 
            ||
| 418 | public function clearUsingModel()  | 
            ||
| 422 | |||
| 423 | /**  | 
            ||
| 424 | * @return string  | 
            ||
| 425 | */  | 
            ||
| 426 | public function getModel()  | 
            ||
| 430 | |||
| 431 | /**  | 
            ||
| 432 | * @param string $model  | 
            ||
| 433 | * @param null $table  | 
            ||
| 434 | * @throws \Exception  | 
            ||
| 435 | */  | 
            ||
| 436 | public function setModel($model, $table = null)  | 
            ||
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * @return string  | 
            ||
| 450 | */  | 
            ||
| 451 | public function lastInsertId()  | 
            ||
| 455 | }  | 
            ||
| 456 |