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 = []) |
||
222 | |||
223 | /** |
||
224 | * Run an bulk insert statement against the database. |
||
225 | * |
||
226 | * @param array $queries |
||
227 | * @param array $bindings |
||
228 | * @param int $type |
||
229 | * @param array $customOptions |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
237 | |||
238 | /** |
||
239 | * Execute a group of queries inside a batch statement against the database. |
||
240 | * |
||
241 | * @param array $queries |
||
242 | * @param array $bindings |
||
243 | * @param int $type |
||
244 | * @param array $customOptions |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
265 | |||
266 | /** |
||
267 | * Execute an CQL statement and return the boolean result. |
||
268 | * |
||
269 | * @param string $query |
||
270 | * @param array $bindings |
||
271 | * @param array $customOptions |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function statement($query, $bindings = [], array $customOptions = []) |
||
279 | |||
280 | /** |
||
281 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
282 | * the affected count for statements so we're just going to return 0, based on the idea |
||
283 | * that if the query fails somehow, an exception will be thrown |
||
284 | * |
||
285 | * @param string $query |
||
286 | * @param array $bindings |
||
287 | * @param array $customOptions |
||
288 | * |
||
289 | * @return int |
||
290 | */ |
||
291 | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
||
295 | |||
296 | /** |
||
297 | * @inheritdoc |
||
298 | */ |
||
299 | protected function getDefaultPostProcessor() |
||
303 | |||
304 | /** |
||
305 | * @inheritdoc |
||
306 | */ |
||
307 | protected function getDefaultQueryGrammar() |
||
311 | |||
312 | /** |
||
313 | * @inheritdoc |
||
314 | */ |
||
315 | protected function getDefaultSchemaGrammar() |
||
319 | |||
320 | /** |
||
321 | * Reconnect to the database if connection is missing. |
||
322 | * |
||
323 | * @return void |
||
324 | */ |
||
325 | protected function reconnectIfMissingConnection() |
||
331 | |||
332 | /** |
||
333 | * Dynamically pass methods to the connection. |
||
334 | * |
||
335 | * @param string $method |
||
336 | * @param array $parameters |
||
337 | * @return mixed |
||
338 | */ |
||
339 | public function __call($method, $parameters) |
||
343 | |||
344 | /** |
||
345 | * Execute an CQL statement and return the boolean result. |
||
346 | * |
||
347 | * @param string $query |
||
348 | * @param array $bindings |
||
349 | * @param array $customOptions |
||
350 | * @param mixed $defaultFailed |
||
351 | * @param mixed $defaultSuccess |
||
352 | * |
||
353 | * @return mixed |
||
354 | */ |
||
355 | protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
||
372 | } |
||
373 |
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: