Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Connection extends \Illuminate\Database\Connection |
||
| 10 | { |
||
| 11 | const DEFAULT_PAGE_SIZE = 5000; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * The Cassandra keyspace |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $keyspace; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The Cassandra cluster |
||
| 22 | * |
||
| 23 | * @var \Cassandra\Cluster |
||
| 24 | */ |
||
| 25 | protected $cluster; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Cassandra connection handler. |
||
| 29 | * |
||
| 30 | * @var \Cassandra\Session |
||
| 31 | */ |
||
| 32 | protected $session; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The config |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $config; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a new database connection instance. |
||
| 43 | * |
||
| 44 | * @param array $config |
||
| 45 | */ |
||
| 46 | 48 | public function __construct(array $config) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Begin a fluent query against a database table. |
||
| 72 | * |
||
| 73 | * @param string $table |
||
| 74 | * @return Query\Builder |
||
| 75 | */ |
||
| 76 | 5 | public function table($table) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * return Cassandra cluster. |
||
| 87 | * |
||
| 88 | * @return \Cassandra\Cluster |
||
| 89 | */ |
||
| 90 | 1 | public function getCassandraCluster() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * return Cassandra Session. |
||
| 97 | * |
||
| 98 | * @return \Cassandra\Session |
||
| 99 | */ |
||
| 100 | 3 | public function getCassandraSession() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Return the Cassandra keyspace |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | 1 | public function getKeyspace() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Set username and password to cluster connection. |
||
| 117 | * |
||
| 118 | * @param Cassandra\Cluster $cluster |
||
| 119 | */ |
||
| 120 | protected function setAuthCredentials(\Cassandra\Cluster\Builder $cluster) |
||
| 127 | 48 | ||
| 128 | 48 | /** |
|
| 129 | * Set cluster contact points (IP addresses) |
||
| 130 | * |
||
| 131 | * @param Cassandra\Cluster $cluster |
||
| 132 | 48 | */ |
|
| 133 | 48 | protected function setContactPoints(\Cassandra\Cluster\Builder $cluster) |
|
| 145 | |||
| 146 | 48 | /** |
|
| 147 | * Set connection communication port |
||
| 148 | 48 | * |
|
| 149 | 48 | * @param Cassandra\Cluster $cluster |
|
| 150 | */ |
||
| 151 | protected function setContactPort(\Cassandra\Cluster\Builder $cluster) |
||
| 157 | |||
| 158 | 48 | /** |
|
| 159 | 48 | * Set default consistency level |
|
| 160 | * |
||
| 161 | * @param Cassandra\Cluster $cluster |
||
| 162 | 48 | */ |
|
| 163 | 48 | protected function setConsistencyLevel(\Cassandra\Cluster\Builder $cluster) |
|
| 175 | |||
| 176 | 3 | /** |
|
| 177 | * Set default timeouts to queries |
||
| 178 | 3 | * |
|
| 179 | 3 | * @param Cassandra\Cluster $cluster |
|
| 180 | 3 | */ |
|
| 181 | protected function setTimeouts(\Cassandra\Cluster\Builder $cluster) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set default response size to queries |
||
| 198 | * |
||
| 199 | * @param Cassandra\Cluster $cluster |
||
| 200 | */ |
||
| 201 | protected function setDefaultPageSize(\Cassandra\Cluster\Builder $cluster) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Create a new Cassandra cluster object. |
||
| 208 | * |
||
| 209 | * @return \Cassandra\Cluster |
||
| 210 | */ |
||
| 211 | protected function createCluster() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Disconnect from the underlying Cassandra connection. |
||
| 234 | 7 | */ |
|
| 235 | 7 | public function disconnect() |
|
| 240 | |||
| 241 | 6 | /** |
|
| 242 | 6 | * Get the PDO driver name. |
|
| 243 | 6 | * |
|
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | 6 | public function getDriverName() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Run a select statement against the database. |
||
| 253 | * |
||
| 254 | * @param string $query |
||
| 255 | * @param array $bindings |
||
| 256 | * @param bool $useReadPdo |
||
| 257 | * @param array $customOptions |
||
| 258 | * |
||
| 259 | 48 | * @return mixed |
|
| 260 | */ |
||
| 261 | 48 | public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Run an bulk insert statement against the database. |
||
| 268 | * |
||
| 269 | * @param array $queries |
||
| 270 | * @param array $bindings |
||
| 271 | * @param int $type |
||
| 272 | * @param array $customOptions |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | 10 | */ |
|
| 276 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Execute a group of queries inside a batch statement against the database. |
||
| 283 | 48 | * |
|
| 284 | * @param array $queries |
||
| 285 | 48 | * @param array $bindings |
|
| 286 | * @param int $type |
||
| 287 | * @param array $customOptions |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 48 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
|
| 308 | |||
| 309 | 48 | /** |
|
| 310 | * Execute an CQL statement and return the boolean result. |
||
| 311 | 48 | * |
|
| 312 | 1 | * @param string $query |
|
| 313 | * @param array $bindings |
||
| 314 | 48 | * @param array $customOptions |
|
| 315 | * |
||
| 316 | * @return mixed |
||
| 317 | */ |
||
| 318 | public function statement($query, $bindings = [], array $customOptions = []) |
||
| 322 | |||
| 323 | 1 | /** |
|
| 324 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
| 325 | 1 | * the affected count for statements so we're just going to return 0, based on the idea |
|
| 326 | * that if the query fails somehow, an exception will be thrown |
||
| 327 | * |
||
| 328 | * @param string $query |
||
| 329 | * @param array $bindings |
||
| 330 | * @param array $customOptions |
||
| 331 | * |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @inheritdoc |
||
| 341 | 48 | */ |
|
| 342 | 48 | protected function getDefaultPostProcessor() |
|
| 346 | 48 | ||
| 347 | /** |
||
| 348 | * @inheritdoc |
||
| 349 | 48 | */ |
|
| 350 | protected function getDefaultQueryGrammar() |
||
| 354 | 48 | ||
| 355 | /** |
||
| 356 | * @inheritdoc |
||
| 357 | */ |
||
| 358 | protected function getDefaultSchemaGrammar() |
||
| 362 | 1 | ||
| 363 | /** |
||
| 364 | * Reconnect to the database if connection is missing. |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 1 | protected function reconnectIfMissingConnection() |
|
| 374 | |||
| 375 | /** |
||
| 376 | 1 | * Dynamically pass methods to the connection. |
|
| 377 | * |
||
| 378 | 1 | * @param string $method |
|
| 379 | * @param array $parameters |
||
| 380 | * @return mixed |
||
| 381 | */ |
||
| 382 | public function __call($method, $parameters) |
||
| 386 | 1 | ||
| 387 | /** |
||
| 388 | * Execute an CQL statement and return the boolean result. |
||
| 389 | * |
||
| 390 | * @param string $query |
||
| 391 | * @param array $bindings |
||
| 392 | 1 | * @param array $customOptions |
|
| 393 | * @param mixed $defaultFailed |
||
| 394 | 1 | * @param mixed $defaultSuccess |
|
| 395 | * |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @inheritDoc |
||
| 418 | */ |
||
| 419 | public function transaction(\Closure $callback, $attempts = 1) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @inheritDoc |
||
| 426 | */ |
||
| 427 | public function beginTransaction() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @inheritDoc |
||
| 434 | */ |
||
| 435 | public function commit() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @inheritDoc |
||
| 442 | */ |
||
| 443 | public function rollBack() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @inheritDoc |
||
| 450 | */ |
||
| 451 | public function transactionLevel() |
||
| 455 | |||
| 456 | //TODO: override isDoctrineAvailable method |
||
| 457 | //TODO: override getDoctrineColumn method |
||
| 458 | //TODO: override getDoctrineSchemaManager method |
||
| 459 | //TODO: override getDoctrineConnection method |
||
| 460 | //TODO: override getPdo method |
||
| 461 | //TODO: override getReadPdo method |
||
| 462 | //TODO: override setPdo method |
||
| 463 | //TODO: override setReadPdo method |
||
| 464 | //TODO: override setReconnector method |
||
| 465 | //TODO: override reconnect method |
||
| 466 | //TODO: override query method |
||
| 467 | |||
| 468 | //TODO: override bindValues method |
||
| 469 | //TODO: override cursor method |
||
| 470 | //TODO: override unprepared method |
||
| 471 | |||
| 472 | //TODO: check prepareBindings method |
||
| 473 | |||
| 474 | //TODO: provide interface for $this->session->executeAsync |
||
| 475 | //TODO: provide interface for $this->session->prepareAsync |
||
| 476 | //TODO: provide interface for $this->session->closeAsync |
||
| 477 | //TODO: provide interface for $this->session->schema |
||
| 478 | } |
||
| 479 |
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: