Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 8 | class Connection extends \Illuminate\Database\Connection |
||
| 9 | { |
||
| 10 | const DEFAULT_PAGE_SIZE = 5000; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * The Cassandra keyspace |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $keyspace; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The Cassandra cluster |
||
| 21 | * |
||
| 22 | * @var \Cassandra\Cluster |
||
| 23 | */ |
||
| 24 | protected $cluster; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The Cassandra connection handler. |
||
| 28 | * |
||
| 29 | * @var \Cassandra\Session |
||
| 30 | */ |
||
| 31 | protected $session; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new database connection instance. |
||
| 35 | * |
||
| 36 | * @param array $config |
||
| 37 | */ |
||
| 38 | public function __construct(array $config) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Begin a fluent query against a database table. |
||
| 65 | * |
||
| 66 | * @param string $table |
||
| 67 | * @return Query\Builder |
||
| 68 | */ |
||
| 69 | public function table($table) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * return Cassandra cluster. |
||
| 80 | * |
||
| 81 | * @return \Cassandra\Cluster |
||
| 82 | */ |
||
| 83 | public function getCassandraCluster() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * return Cassandra Session. |
||
| 90 | * |
||
| 91 | * @return \Cassandra\Session |
||
| 92 | */ |
||
| 93 | public function getCassandraSession() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return the Cassandra keyspace |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getKeyspace() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create a new Cassandra cluster object. |
||
| 110 | * |
||
| 111 | * @param string $dsn |
||
| 112 | * @param array $config |
||
| 113 | * @param array $options |
||
| 114 | * @return \Cassandra\Cluster |
||
| 115 | */ |
||
| 116 | protected function createCluster($dsn, array $config, array $options) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Disconnect from the underlying Cassandra connection. |
||
| 160 | */ |
||
| 161 | public function disconnect() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the PDO driver name. |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getDriverName() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Run a select statement against the database. |
||
| 178 | * |
||
| 179 | * @param string $query |
||
| 180 | * @param array $bindings |
||
| 181 | * @param bool $useReadPdo |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function select($query, $bindings = [], $useReadPdo = true) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Run an bulk insert statement against the database. |
||
| 199 | * |
||
| 200 | * @param array $queries |
||
| 201 | * @param array $bindings |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Execute a group of queries inside a batch statement against the database. |
||
| 211 | * |
||
| 212 | * @param array $queries |
||
| 213 | * @param array $bindings |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Execute an SQL statement and return the boolean result. |
||
| 236 | * |
||
| 237 | * @param string $query |
||
| 238 | * @param array $bindings |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function statement($query, $bindings = []) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
| 256 | * the affected count for statements so we're just going to return 0, based on the idea |
||
| 257 | * that if the query fails somehow, an exception will be thrown |
||
| 258 | * |
||
| 259 | * @param string $query |
||
| 260 | * @param array $bindings |
||
| 261 | * @return int |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function affectingStatement($query, $bindings = []) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @inheritdoc |
||
| 280 | */ |
||
| 281 | protected function getDefaultPostProcessor() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | protected function getDefaultQueryGrammar() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @inheritdoc |
||
| 296 | */ |
||
| 297 | protected function getDefaultSchemaGrammar() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Dynamically pass methods to the connection. |
||
| 304 | * |
||
| 305 | * @param string $method |
||
| 306 | * @param array $parameters |
||
| 307 | * @return mixed |
||
| 308 | */ |
||
| 309 | public function __call($method, $parameters) |
||
| 313 | } |
||
| 314 |
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: