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 | * The config |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $config; |
||
39 | |||
40 | /** |
||
41 | * Create a new database connection instance. |
||
42 | * |
||
43 | * @param array $config |
||
44 | */ |
||
45 | public function __construct(array $config) |
||
71 | |||
72 | /** |
||
73 | * Begin a fluent query against a database table. |
||
74 | * |
||
75 | * @param string $table |
||
76 | * @return Query\Builder |
||
77 | */ |
||
78 | public function table($table) |
||
86 | |||
87 | /** |
||
88 | * return Cassandra cluster. |
||
89 | * |
||
90 | * @return \Cassandra\Cluster |
||
91 | */ |
||
92 | public function getCassandraCluster() |
||
96 | |||
97 | /** |
||
98 | * return Cassandra Session. |
||
99 | * |
||
100 | * @return \Cassandra\Session |
||
101 | */ |
||
102 | public function getCassandraSession() |
||
106 | |||
107 | /** |
||
108 | * Return the Cassandra keyspace |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getKeyspace() |
||
116 | |||
117 | /** |
||
118 | * Create a new Cassandra cluster object. |
||
119 | * |
||
120 | * @param array $config |
||
121 | * @param array $options |
||
122 | * @return \Cassandra\Cluster |
||
123 | */ |
||
124 | protected function createCluster(array $config, array $options) |
||
189 | |||
190 | /** |
||
191 | * Disconnect from the underlying Cassandra connection. |
||
192 | */ |
||
193 | public function disconnect() |
||
197 | |||
198 | /** |
||
199 | * Get the PDO driver name. |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | public function getDriverName() |
||
207 | |||
208 | /** |
||
209 | * Run a select statement against the database. |
||
210 | * |
||
211 | * @param string $query |
||
212 | * @param array $bindings |
||
213 | * @param bool $useReadPdo |
||
214 | * @param array $customOptions |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
||
233 | |||
234 | /** |
||
235 | * Run an bulk insert statement against the database. |
||
236 | * |
||
237 | * @param array $queries |
||
238 | * @param array $bindings |
||
239 | * @param int $type |
||
240 | * @param array $customOptions |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
248 | |||
249 | /** |
||
250 | * Execute a group of queries inside a batch statement against the database. |
||
251 | * |
||
252 | * @param array $queries |
||
253 | * @param array $bindings |
||
254 | * @param int $type |
||
255 | * @param array $customOptions |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
276 | |||
277 | /** |
||
278 | * Execute an SQL statement and return the boolean result. |
||
279 | * |
||
280 | * @param string $query |
||
281 | * @param array $bindings |
||
282 | * @param array $customOptions |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | View Code Duplication | public function statement($query, $bindings = [], array $customOptions = []) |
|
302 | |||
303 | /** |
||
304 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
305 | * the affected count for statements so we're just going to return 0, based on the idea |
||
306 | * that if the query fails somehow, an exception will be thrown |
||
307 | * |
||
308 | * @param string $query |
||
309 | * @param array $bindings |
||
310 | * @param array $customOptions |
||
311 | * |
||
312 | * @return int |
||
313 | */ |
||
314 | View Code Duplication | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
|
332 | |||
333 | /** |
||
334 | * @inheritdoc |
||
335 | */ |
||
336 | protected function getDefaultPostProcessor() |
||
340 | |||
341 | /** |
||
342 | * @inheritdoc |
||
343 | */ |
||
344 | protected function getDefaultQueryGrammar() |
||
348 | |||
349 | /** |
||
350 | * @inheritdoc |
||
351 | */ |
||
352 | protected function getDefaultSchemaGrammar() |
||
356 | |||
357 | /** |
||
358 | * Reconnect to the database if connection is missing. |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | protected function reconnectIfMissingConnection() |
||
368 | |||
369 | /** |
||
370 | * Dynamically pass methods to the connection. |
||
371 | * |
||
372 | * @param string $method |
||
373 | * @param array $parameters |
||
374 | * @return mixed |
||
375 | */ |
||
376 | public function __call($method, $parameters) |
||
380 | } |
||
381 |
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: