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:
1 | <?php |
||
9 | class Connection extends \Illuminate\Database\Connection |
||
10 | { |
||
11 | /** |
||
12 | * The Cassandra keyspace |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $keyspace; |
||
17 | |||
18 | /** |
||
19 | * The Cassandra cluster |
||
20 | * |
||
21 | * @var \Cassandra\Cluster |
||
22 | */ |
||
23 | protected $cluster; |
||
24 | |||
25 | /** |
||
26 | * The Cassandra connection handler. |
||
27 | * |
||
28 | * @var \Cassandra\Session |
||
29 | */ |
||
30 | protected $session; |
||
31 | |||
32 | /** |
||
33 | * Create a new database connection instance. |
||
34 | * |
||
35 | * @param array $config |
||
36 | */ |
||
37 | public function __construct(array $config) |
||
58 | |||
59 | /** |
||
60 | * Begin a fluent query against a database table. |
||
61 | * |
||
62 | * @param string $table |
||
63 | * @return Query\Builder |
||
64 | */ |
||
65 | public function table($table) |
||
73 | |||
74 | /** |
||
75 | * return Cassandra cluster. |
||
76 | * |
||
77 | * @return \Cassandra\Cluster |
||
78 | */ |
||
79 | public function getCassandraCluster() |
||
83 | |||
84 | /** |
||
85 | * return Cassandra Session. |
||
86 | * |
||
87 | * @return \Cassandra\Session |
||
88 | */ |
||
89 | public function getCassandraSession() |
||
93 | |||
94 | /** |
||
95 | * Return the Cassandra keyspace |
||
96 | * |
||
97 | * @return string |
||
98 | */ |
||
99 | public function getKeyspace() |
||
103 | |||
104 | /** |
||
105 | * Create a new Cassandra cluster object. |
||
106 | * |
||
107 | * @param string $dsn |
||
108 | * @param array $config |
||
109 | * @param array $options |
||
110 | * @return \Cassandra\Cluster |
||
111 | */ |
||
112 | protected function createCluster($dsn, array $config, array $options) |
||
153 | |||
154 | /** |
||
155 | * Disconnect from the underlying Cassandra connection. |
||
156 | */ |
||
157 | public function disconnect() |
||
161 | |||
162 | /** |
||
163 | * Get the PDO driver name. |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getDriverName() |
||
171 | |||
172 | /** |
||
173 | * Run a select statement against the database. |
||
174 | * |
||
175 | * @param string $query |
||
176 | * @param array $bindings |
||
177 | * @param bool $useReadPdo |
||
178 | * @return array |
||
179 | */ |
||
180 | View Code Duplication | public function select($query, $bindings = [], $useReadPdo = true) |
|
192 | |||
193 | /** |
||
194 | * Run an bulk insert statement against the database. |
||
195 | * |
||
196 | * @param array $queries |
||
197 | * @param array $bindings |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
204 | |||
205 | /** |
||
206 | * Execute a group of queries inside a batch statement against the database. |
||
207 | * |
||
208 | * @param array $queries |
||
209 | * @param array $bindings |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
229 | |||
230 | /** |
||
231 | * Execute an SQL statement and return the boolean result. |
||
232 | * |
||
233 | * @param string $query |
||
234 | * @param array $bindings |
||
235 | * @return bool |
||
236 | */ |
||
237 | View Code Duplication | public function statement($query, $bindings = []) |
|
249 | |||
250 | /** |
||
251 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
252 | * the affected count for statements so we're just going to return 0, based on the idea |
||
253 | * that if the query fails somehow, an exception will be thrown |
||
254 | * |
||
255 | * @param string $query |
||
256 | * @param array $bindings |
||
257 | * @return int |
||
258 | */ |
||
259 | View Code Duplication | public function affectingStatement($query, $bindings = []) |
|
273 | |||
274 | /** |
||
275 | * @inheritdoc |
||
276 | */ |
||
277 | protected function getDefaultPostProcessor() |
||
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | */ |
||
285 | protected function getDefaultQueryGrammar() |
||
289 | |||
290 | /** |
||
291 | * @inheritdoc |
||
292 | */ |
||
293 | protected function getDefaultSchemaGrammar() |
||
297 | |||
298 | /** |
||
299 | * Dynamically pass methods to the connection. |
||
300 | * |
||
301 | * @param string $method |
||
302 | * @param array $parameters |
||
303 | * @return mixed |
||
304 | */ |
||
305 | public function __call($method, $parameters) |
||
309 | } |
||
310 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.