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 | * Get a schema builder instance for the connection. |
||
| 76 | * |
||
| 77 | * @return Schema\Builder |
||
| 78 | */ |
||
| 79 | public function getSchemaBuilder() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * return Cassandra cluster. |
||
| 86 | * |
||
| 87 | * @return \Cassandra\Cluster |
||
| 88 | */ |
||
| 89 | public function getCassandraCluster() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * return Cassandra Session. |
||
| 96 | * |
||
| 97 | * @return \Cassandra\Session |
||
| 98 | */ |
||
| 99 | public function getCassandraSession() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Return the Cassandra keyspace |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getKeyspace() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Create a new Cassandra cluster object. |
||
| 116 | * |
||
| 117 | * @param string $dsn |
||
| 118 | * @param array $config |
||
| 119 | * @param array $options |
||
| 120 | * @return \Cassandra\Cluster |
||
| 121 | */ |
||
| 122 | protected function createCluster($dsn, array $config, array $options) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Disconnect from the underlying Cassandra connection. |
||
| 166 | */ |
||
| 167 | public function disconnect() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the PDO driver name. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getDriverName() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Run a select statement against the database. |
||
| 184 | * |
||
| 185 | * @param string $query |
||
| 186 | * @param array $bindings |
||
| 187 | * @param bool $useReadPdo |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function select($query, $bindings = [], $useReadPdo = true) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Run an bulk insert statement against the database. |
||
| 205 | * |
||
| 206 | * @param array $queries |
||
| 207 | * @param array $bindings |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function insertBulk($queries, $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Execute a group of queries inside a batch statement against the database. |
||
| 217 | * |
||
| 218 | * @param array $queries |
||
| 219 | * @param array $bindings |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Execute an SQL statement and return the boolean result. |
||
| 242 | * |
||
| 243 | * @param string $query |
||
| 244 | * @param array $bindings |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function statement($query, $bindings = []) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
| 262 | * the affected count for statements so we're just going to return 0, based on the idea |
||
| 263 | * that if the query fails somehow, an exception will be thrown |
||
| 264 | * |
||
| 265 | * @param string $query |
||
| 266 | * @param array $bindings |
||
| 267 | * @return int |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function affectingStatement($query, $bindings = []) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @inheritdoc |
||
| 286 | */ |
||
| 287 | protected function getDefaultPostProcessor() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | protected function getDefaultQueryGrammar() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @inheritdoc |
||
| 302 | */ |
||
| 303 | protected function getDefaultSchemaGrammar() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Dynamically pass methods to the connection. |
||
| 310 | * |
||
| 311 | * @param string $method |
||
| 312 | * @param array $parameters |
||
| 313 | * @return mixed |
||
| 314 | */ |
||
| 315 | public function __call($method, $parameters) |
||
| 319 | } |
||
| 320 |
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.