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 |
||
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 | public function __construct(array $config) |
||
72 | |||
73 | /** |
||
74 | * Begin a fluent query against a database table. |
||
75 | * |
||
76 | * @param string $table |
||
77 | * @return Query\Builder |
||
78 | */ |
||
79 | public function table($table) |
||
87 | |||
88 | /** |
||
89 | * return Cassandra cluster. |
||
90 | * |
||
91 | * @return \Cassandra\Cluster |
||
92 | */ |
||
93 | public function getCassandraCluster() |
||
97 | |||
98 | /** |
||
99 | * return Cassandra Session. |
||
100 | * |
||
101 | * @return \Cassandra\Session |
||
102 | */ |
||
103 | public function getCassandraSession() |
||
107 | |||
108 | /** |
||
109 | * Return the Cassandra keyspace |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getKeyspace() |
||
117 | |||
118 | /** |
||
119 | * Create a new Cassandra cluster object. |
||
120 | * |
||
121 | * @param array $config |
||
122 | * @param array $options |
||
123 | * @return \Cassandra\Cluster |
||
124 | */ |
||
125 | protected function createCluster(array $config, array $options) |
||
190 | |||
191 | /** |
||
192 | * Disconnect from the underlying Cassandra connection. |
||
193 | */ |
||
194 | public function disconnect() |
||
198 | |||
199 | /** |
||
200 | * Get the PDO driver name. |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public function getDriverName() |
||
208 | |||
209 | /** |
||
210 | * Run a select statement against the database. |
||
211 | * |
||
212 | * @param string $query |
||
213 | * @param array $bindings |
||
214 | * @param bool $useReadPdo |
||
215 | * @param array $customOptions |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
||
223 | |||
224 | /** |
||
225 | * Run an bulk insert statement against the database. |
||
226 | * |
||
227 | * @param array $queries |
||
228 | * @param array $bindings |
||
229 | * @param int $type |
||
230 | * @param array $customOptions |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
238 | |||
239 | /** |
||
240 | * Execute a group of queries inside a batch statement against the database. |
||
241 | * |
||
242 | * @param array $queries |
||
243 | * @param array $bindings |
||
244 | * @param int $type |
||
245 | * @param array $customOptions |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
266 | |||
267 | /** |
||
268 | * Execute an CQL statement and return the boolean result. |
||
269 | * |
||
270 | * @param string $query |
||
271 | * @param array $bindings |
||
272 | * @param array $customOptions |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | public function statement($query, $bindings = [], array $customOptions = []) |
||
280 | |||
281 | /** |
||
282 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
283 | * the affected count for statements so we're just going to return 0, based on the idea |
||
284 | * that if the query fails somehow, an exception will be thrown |
||
285 | * |
||
286 | * @param string $query |
||
287 | * @param array $bindings |
||
288 | * @param array $customOptions |
||
289 | * |
||
290 | * @return int |
||
291 | */ |
||
292 | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
||
296 | |||
297 | /** |
||
298 | * @inheritdoc |
||
299 | */ |
||
300 | protected function getDefaultPostProcessor() |
||
304 | |||
305 | /** |
||
306 | * @inheritdoc |
||
307 | */ |
||
308 | protected function getDefaultQueryGrammar() |
||
312 | |||
313 | /** |
||
314 | * @inheritdoc |
||
315 | */ |
||
316 | protected function getDefaultSchemaGrammar() |
||
320 | |||
321 | /** |
||
322 | * Reconnect to the database if connection is missing. |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | protected function reconnectIfMissingConnection() |
||
332 | |||
333 | /** |
||
334 | * Dynamically pass methods to the connection. |
||
335 | * |
||
336 | * @param string $method |
||
337 | * @param array $parameters |
||
338 | * @return mixed |
||
339 | */ |
||
340 | public function __call($method, $parameters) |
||
344 | |||
345 | /** |
||
346 | * Execute an CQL statement and return the boolean result. |
||
347 | * |
||
348 | * @param $query |
||
349 | * @param array $bindings |
||
350 | * @param array $customOptions |
||
351 | * @param array $defaultFailed |
||
352 | * @param null $defaultSuccess |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
||
373 | } |
||
374 |
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: