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 |
||
| 10 | class Connection extends \Illuminate\Database\Connection |
||
| 11 | { |
||
| 12 | const DEFAULT_PAGE_SIZE = 5000; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * The Cassandra keyspace |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $keyspace; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The Cassandra cluster |
||
| 23 | * |
||
| 24 | * @var \Cassandra\Cluster |
||
| 25 | */ |
||
| 26 | protected $cluster; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The Cassandra connection handler. |
||
| 30 | * |
||
| 31 | * @var \Cassandra\Session |
||
| 32 | */ |
||
| 33 | protected $session; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The config |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $config; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Create a new database connection instance. |
||
| 44 | * |
||
| 45 | * @param array $config |
||
| 46 | 48 | */ |
|
| 47 | public function __construct(array $config) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Begin a fluent query against a database table. |
||
| 73 | * |
||
| 74 | * @param string $table |
||
| 75 | * @return Query\Builder |
||
| 76 | 5 | */ |
|
| 77 | public function table($table) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * return Cassandra cluster. |
||
| 88 | * |
||
| 89 | * @return \Cassandra\Cluster |
||
| 90 | 1 | */ |
|
| 91 | public function getCassandraCluster() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * return Cassandra Session. |
||
| 98 | * |
||
| 99 | * @return \Cassandra\Session |
||
| 100 | 3 | */ |
|
| 101 | public function getCassandraSession() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return the Cassandra keyspace |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | 1 | */ |
|
| 111 | public function getKeyspace() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set username and password to cluster connection. |
||
| 118 | * Set cluster contact points (IP addresses) |
||
| 119 | * Set connection communication port |
||
| 120 | * |
||
| 121 | * @param \Cassandra\Cluster\Builder $cluster |
||
| 122 | 48 | */ |
|
| 123 | protected function setConnectionOptions(ClusterBuilder $cluster) |
||
| 144 | |||
| 145 | /** |
||
| 146 | 48 | * Set default consistency level |
|
| 147 | * Set default timeouts to queries |
||
| 148 | 48 | * Set default response size to queries |
|
| 149 | 48 | * |
|
| 150 | * @param \Cassandra\Cluster\Builder $cluster |
||
| 151 | */ |
||
| 152 | protected function setDefaultQueryOptions(ClusterBuilder $cluster) |
||
| 178 | 3 | ||
| 179 | 3 | /** |
|
| 180 | 3 | * Create a new Cassandra cluster object. |
|
| 181 | * |
||
| 182 | * @return \Cassandra\Cluster |
||
| 183 | */ |
||
| 184 | protected function createCluster() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Disconnect from the underlying Cassandra connection. |
||
| 198 | */ |
||
| 199 | public function disconnect() |
||
| 204 | 48 | ||
| 205 | /** |
||
| 206 | * Get the PDO driver name. |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getDriverName() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Run a select statement against the database. |
||
| 217 | 6 | * |
|
| 218 | * @param string $query |
||
| 219 | 6 | * @param array $bindings |
|
| 220 | * @param bool $useReadPdo |
||
| 221 | * @param array $customOptions |
||
| 222 | * |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Run an bulk insert statement against the database. |
||
| 232 | * |
||
| 233 | * @param array $queries |
||
| 234 | 7 | * @param array $bindings |
|
| 235 | 7 | * @param int $type |
|
| 236 | 1 | * @param array $customOptions |
|
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | 6 | */ |
|
| 240 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
| 244 | |||
| 245 | /** |
||
| 246 | 6 | * Execute a group of queries inside a batch statement against the database. |
|
| 247 | 7 | * |
|
| 248 | * @param array $queries |
||
| 249 | * @param array $bindings |
||
| 250 | * @param int $type |
||
| 251 | * @param array $customOptions |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Execute an CQL statement and return the boolean result. |
||
| 275 | 10 | * |
|
| 276 | * @param string $query |
||
| 277 | 10 | * @param array $bindings |
|
| 278 | * @param array $customOptions |
||
| 279 | * |
||
| 280 | * @return mixed |
||
| 281 | */ |
||
| 282 | public function statement($query, $bindings = [], array $customOptions = []) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
| 289 | * the affected count for statements so we're just going to return 0, based on the idea |
||
| 290 | * that if the query fails somehow, an exception will be thrown |
||
| 291 | 48 | * |
|
| 292 | * @param string $query |
||
| 293 | 48 | * @param array $bindings |
|
| 294 | * @param array $customOptions |
||
| 295 | * |
||
| 296 | * @return int |
||
| 297 | */ |
||
| 298 | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
||
| 302 | 48 | ||
| 303 | /** |
||
| 304 | * @inheritdoc |
||
| 305 | */ |
||
| 306 | protected function getDefaultPostProcessor() |
||
| 310 | |||
| 311 | 48 | /** |
|
| 312 | 1 | * @inheritdoc |
|
| 313 | */ |
||
| 314 | 48 | protected function getDefaultQueryGrammar() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @inheritdoc |
||
| 321 | */ |
||
| 322 | protected function getDefaultSchemaGrammar() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Reconnect to the database if connection is missing. |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | protected function reconnectIfMissingConnection() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Dynamically pass methods to the connection. |
||
| 341 | 48 | * |
|
| 342 | 48 | * @param string $method |
|
| 343 | 2 | * @param array $parameters |
|
| 344 | * @return mixed |
||
| 345 | */ |
||
| 346 | 48 | public function __call($method, $parameters) |
|
| 350 | |||
| 351 | 48 | /** |
|
| 352 | * Execute an CQL statement and return the boolean result. |
||
| 353 | 48 | * |
|
| 354 | 48 | * @param string $query |
|
| 355 | * @param array $bindings |
||
| 356 | * @param array $customOptions |
||
| 357 | * @param mixed $defaultFailed |
||
| 358 | * @param mixed $defaultSuccess |
||
| 359 | * |
||
| 360 | 1 | * @return mixed |
|
| 361 | */ |
||
| 362 | 1 | protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @inheritDoc |
||
| 382 | */ |
||
| 383 | public function transaction(\Closure $callback, $attempts = 1) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @inheritDoc |
||
| 390 | */ |
||
| 391 | public function beginTransaction() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @inheritDoc |
||
| 398 | */ |
||
| 399 | public function commit() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @inheritDoc |
||
| 406 | */ |
||
| 407 | public function rollBack() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @inheritDoc |
||
| 414 | */ |
||
| 415 | public function transactionLevel() |
||
| 419 | |||
| 420 | //TODO: override isDoctrineAvailable method |
||
| 421 | //TODO: override getDoctrineColumn method |
||
| 422 | //TODO: override getDoctrineSchemaManager method |
||
| 423 | //TODO: override getDoctrineConnection method |
||
| 424 | //TODO: override getPdo method |
||
| 425 | //TODO: override getReadPdo method |
||
| 426 | //TODO: override setPdo method |
||
| 427 | //TODO: override setReadPdo method |
||
| 428 | //TODO: override setReconnector method |
||
| 429 | //TODO: override reconnect method |
||
| 430 | //TODO: override query method |
||
| 431 | |||
| 432 | //TODO: override bindValues method |
||
| 433 | //TODO: override cursor method |
||
| 434 | //TODO: override unprepared method |
||
| 435 | |||
| 436 | //TODO: check prepareBindings method |
||
| 437 | |||
| 438 | //TODO: provide interface for $this->session->executeAsync |
||
| 439 | //TODO: provide interface for $this->session->prepareAsync |
||
| 440 | //TODO: provide interface for $this->session->closeAsync |
||
| 441 | //TODO: provide interface for $this->session->schema |
||
| 442 | } |
||
| 443 |
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: