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 |
||
| 8 | |||
| 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 connection handler. |
||
| 20 | * |
||
| 21 | * @var \Cassandra\Session |
||
| 22 | */ |
||
| 23 | protected $session; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The config |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $config; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The Cassandra cluster |
||
| 34 | * |
||
| 35 | * @var \Cassandra\Cluster |
||
| 36 | */ |
||
| 37 | protected $cluster; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Connection constructor. |
||
| 41 | * @param Cassandra\Cluster $cluster |
||
| 42 | * @param array $config |
||
| 43 | */ |
||
| 44 | 127 | public function __construct(\Cassandra\Cluster $cluster, array $config) |
|
| 45 | { |
||
| 46 | 127 | $this->config = $config; |
|
| 47 | |||
| 48 | 127 | $this->cluster = $cluster; |
|
| 49 | |||
| 50 | 127 | $this->keyspace = $this->getDatabase($config); |
|
| 51 | |||
| 52 | 127 | $this->session = $this->cluster->connect($this->keyspace); |
|
| 53 | |||
| 54 | 127 | $this->useDefaultPostProcessor(); |
|
| 55 | |||
| 56 | 127 | $this->useDefaultSchemaGrammar(); |
|
| 57 | |||
| 58 | 127 | $this->setQueryGrammar($this->getDefaultQueryGrammar()); |
|
| 59 | 127 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Get keyspace name from config |
||
| 63 | * |
||
| 64 | * @param array $config |
||
| 65 | * |
||
| 66 | * @return string|null |
||
| 67 | */ |
||
| 68 | 127 | protected function getDatabase(array $config) |
|
| 69 | { |
||
| 70 | 127 | $keyspaceName = null; |
|
| 71 | |||
| 72 | 127 | if (isset($config['keyspace'])) { |
|
| 73 | 127 | $keyspaceName = $config['keyspace']; |
|
| 74 | } |
||
| 75 | |||
| 76 | 127 | return $keyspaceName; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Begin a fluent query against a database table. |
||
| 81 | * |
||
| 82 | * @param string $table |
||
| 83 | * @return Query\Builder |
||
| 84 | */ |
||
| 85 | 43 | public function table($table) |
|
| 86 | { |
||
| 87 | 43 | $processor = $this->getPostProcessor(); |
|
| 88 | |||
| 89 | 43 | $query = new Query\Builder($this, null, $processor); |
|
|
|
|||
| 90 | |||
| 91 | 43 | return $query->from($table); |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * return Cassandra cluster. |
||
| 96 | * |
||
| 97 | * @return \Cassandra\Cluster |
||
| 98 | */ |
||
| 99 | 1 | public function getCassandraCluster() |
|
| 100 | { |
||
| 101 | 1 | return $this->cluster; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * return Cassandra Session. |
||
| 106 | * |
||
| 107 | * @return \Cassandra\Session |
||
| 108 | */ |
||
| 109 | 3 | public function getCassandraSession() |
|
| 110 | { |
||
| 111 | 3 | return $this->session; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return the Cassandra keyspace |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 1 | public function getKeyspace() |
|
| 120 | { |
||
| 121 | 1 | return $this->keyspace; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Disconnect from the underlying Cassandra connection. |
||
| 126 | */ |
||
| 127 | 3 | public function disconnect() |
|
| 128 | { |
||
| 129 | 3 | $this->session->close(); |
|
| 130 | 3 | $this->session = null; |
|
| 131 | 3 | } |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Get the PDO driver name. |
||
| 135 | * |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 1 | public function getDriverName() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Run a select statement against the database. |
||
| 145 | * |
||
| 146 | * @param string $query |
||
| 147 | * @param array $bindings |
||
| 148 | * @param bool $useReadPdo |
||
| 149 | * @param array $customOptions |
||
| 150 | * |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | 127 | public function select($query, $bindings = [], $useReadPdo = true, array $customOptions = []) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Run an bulk insert statement against the database. |
||
| 160 | * |
||
| 161 | * @param array $queries |
||
| 162 | * @param array $bindings |
||
| 163 | * @param int $type |
||
| 164 | * @param array $customOptions |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 7 | public function insertBulk($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Execute a group of queries inside a batch statement against the database. |
||
| 175 | * |
||
| 176 | * @param array $queries |
||
| 177 | * @param array $bindings |
||
| 178 | * @param int $type |
||
| 179 | * @param array $customOptions |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function batchStatement($queries = [], $bindings = [], $type = Cassandra::BATCH_LOGGED, array $customOptions = []) |
||
| 184 | { |
||
| 185 | 8 | return $this->run($queries, $bindings, function ($queries, $bindings) use ($type, $customOptions) { |
|
| 186 | 8 | if ($this->pretending()) { |
|
| 187 | 1 | return []; |
|
| 188 | } |
||
| 189 | |||
| 190 | 7 | $batch = new BatchStatement($type); |
|
| 191 | |||
| 192 | 7 | foreach ($queries as $k => $query) { |
|
| 193 | 7 | $preparedStatement = $this->session->prepare($query); |
|
| 194 | 7 | $batch->add($preparedStatement, $bindings[$k]); |
|
| 195 | } |
||
| 196 | |||
| 197 | 7 | return $this->session->execute($batch, $customOptions); |
|
| 198 | 8 | }); |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Execute an CQL statement and return the boolean result. |
||
| 203 | * |
||
| 204 | * @param string $query |
||
| 205 | * @param array $bindings |
||
| 206 | * @param array $customOptions |
||
| 207 | * |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | 127 | public function statement($query, $bindings = [], array $customOptions = []) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Because Cassandra is an eventually consistent database, it's not possible to obtain |
||
| 217 | * the affected count for statements so we're just going to return 0, based on the idea |
||
| 218 | * that if the query fails somehow, an exception will be thrown |
||
| 219 | * |
||
| 220 | * @param string $query |
||
| 221 | * @param array $bindings |
||
| 222 | * @param array $customOptions |
||
| 223 | * |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | 11 | public function affectingStatement($query, $bindings = [], array $customOptions = []) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritdoc |
||
| 233 | */ |
||
| 234 | 127 | protected function getDefaultPostProcessor() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritdoc |
||
| 241 | */ |
||
| 242 | 127 | protected function getDefaultQueryGrammar() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritdoc |
||
| 249 | */ |
||
| 250 | 127 | protected function getDefaultSchemaGrammar() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Reconnect to the database if connection is missing. |
||
| 257 | * |
||
| 258 | * @return void |
||
| 259 | */ |
||
| 260 | 127 | protected function reconnectIfMissingConnection() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Dynamically pass methods to the connection. |
||
| 269 | * |
||
| 270 | * @param string $method |
||
| 271 | * @param array $parameters |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | 1 | public function __call($method, $parameters) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Execute an CQL statement and return the boolean result. |
||
| 281 | * |
||
| 282 | * @param string $query |
||
| 283 | * @param array $bindings |
||
| 284 | * @param array $customOptions |
||
| 285 | * @param mixed $defaultFailed |
||
| 286 | * @param mixed $defaultSuccess |
||
| 287 | * |
||
| 288 | * @return mixed |
||
| 289 | */ |
||
| 290 | protected function runStatement($query, $bindings = [], array $customOptions = [], $defaultFailed = [], $defaultSuccess = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @inheritDoc |
||
| 310 | */ |
||
| 311 | 1 | public function transaction(\Closure $callback, $attempts = 1) |
|
| 312 | { |
||
| 313 | 1 | throw new CassandraNotSupportedException("Transactions is not supported by Cassandra database"); |
|
| 314 | } |
||
| 371 |
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: