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 CouchbaseConnection 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 CouchbaseConnection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class CouchbaseConnection extends Connection |
||
34 | { |
||
35 | use VersionTrait; |
||
36 | |||
37 | /** @var string */ |
||
38 | protected $bucket; |
||
39 | |||
40 | /** @var \CouchbaseCluster */ |
||
41 | protected $connection; |
||
42 | |||
43 | /** @var */ |
||
44 | protected $managerUser; |
||
45 | |||
46 | /** @var */ |
||
47 | protected $managerPassword; |
||
48 | |||
49 | /** @var array */ |
||
50 | protected $options = []; |
||
51 | |||
52 | /** @var int */ |
||
53 | protected $fetchMode = 0; |
||
54 | |||
55 | /** @var array */ |
||
56 | protected $enableN1qlServers = []; |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $bucketPassword = ''; |
||
60 | |||
61 | /** @var string[] */ |
||
62 | protected $metrics; |
||
63 | |||
64 | /** @var int default consistency */ |
||
65 | protected $consistency = \CouchbaseN1qlQuery::NOT_BOUNDED; |
||
66 | |||
67 | /** @var string[] function to handle the retrieval of various properties. */ |
||
68 | private $properties = [ |
||
69 | 'operationTimeout', |
||
70 | 'viewTimeout', |
||
71 | 'durabilityInterval', |
||
72 | 'durabilityTimeout', |
||
73 | 'httpTimeout', |
||
74 | 'configTimeout', |
||
75 | 'configDelay', |
||
76 | 'configNodeTimeout', |
||
77 | 'htconfigIdleTimeout', |
||
78 | ]; |
||
79 | |||
80 | /** @var array */ |
||
81 | protected $config = []; |
||
82 | |||
83 | /** |
||
84 | * @param array $config |
||
85 | */ |
||
86 | 1 | public function __construct(array $config) |
|
95 | |||
96 | /** |
||
97 | * @param $password |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setBucketPassword($password) |
||
107 | |||
108 | /** |
||
109 | * @param string $name |
||
110 | * |
||
111 | * @return \CouchbaseBucket |
||
112 | * |
||
113 | * @throws \CouchbaseException |
||
114 | */ |
||
115 | 1 | public function openBucket($name) |
|
119 | |||
120 | /** |
||
121 | * @return \CouchbaseClusterManager |
||
122 | */ |
||
123 | public function manager() |
||
127 | |||
128 | /** |
||
129 | * @param CouchbaseBucket $bucket |
||
130 | * |
||
131 | * @return string[] |
||
132 | */ |
||
133 | public function getOptions(\CouchbaseBucket $bucket) |
||
142 | |||
143 | /** |
||
144 | * @param CouchbaseBucket $bucket |
||
145 | */ |
||
146 | protected function registerOption(\CouchbaseBucket $bucket) |
||
154 | |||
155 | /** |
||
156 | * @return Processor |
||
157 | */ |
||
158 | 1 | protected function getDefaultPostProcessor() |
|
162 | |||
163 | /** |
||
164 | * @return Grammar |
||
165 | */ |
||
166 | 1 | protected function getDefaultQueryGrammar() |
|
170 | |||
171 | /** |
||
172 | * @return Builder|\Illuminate\Database\Schema\Builder |
||
173 | */ |
||
174 | public function getSchemaBuilder() |
||
178 | |||
179 | /** |
||
180 | * |
||
181 | * @param array $config enable(array), options(array), administrator(array), bucket_password(string) |
||
182 | */ |
||
183 | 1 | protected function getManagedConfigure(array $config) |
|
196 | |||
197 | /** |
||
198 | * @return \CouchbaseCluster |
||
199 | */ |
||
200 | 1 | protected function createConnection() |
|
210 | |||
211 | /** |
||
212 | * {@inheritdoc} |
||
213 | */ |
||
214 | public function getDriverName() |
||
218 | |||
219 | /** |
||
220 | * @return \CouchbaseCluster |
||
221 | */ |
||
222 | 1 | public function getCouchbase() |
|
230 | |||
231 | /** |
||
232 | * @param string $table |
||
233 | * |
||
234 | * @return QueryBuilder |
||
235 | */ |
||
236 | public function table($table) |
||
240 | |||
241 | /** |
||
242 | * @param int $consistency |
||
243 | * @param callable $callback |
||
244 | * |
||
245 | * @return mixed |
||
246 | */ |
||
247 | public function callableConsistency($consistency, callable $callback) |
||
254 | |||
255 | /** |
||
256 | * @param int $consistency |
||
257 | * |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function consistency($consistency) |
||
266 | |||
267 | /** |
||
268 | * @param string $bucket |
||
269 | * |
||
270 | * @return $this |
||
271 | */ |
||
272 | public function bucket($bucket) |
||
278 | |||
279 | /** |
||
280 | * @param \CouchbaseN1qlQuery $query |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | protected function executeQuery(\CouchbaseN1qlQuery $query) |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function select($query, $bindings = [], $useReadPdo = true) |
||
321 | |||
322 | /** |
||
323 | * @param string $query |
||
324 | * @param array $bindings |
||
325 | * |
||
326 | * @return int|mixed |
||
327 | */ |
||
328 | public function insert($query, $bindings = []) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | public function affectingStatement($query, $bindings = []) |
||
364 | |||
365 | /** |
||
366 | * @param $query |
||
367 | * @param array $bindings |
||
368 | * |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function positionalStatement($query, array $bindings = []) |
||
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function transaction(Closure $callback) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function beginTransaction() |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | public function commit() |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function rollBack() |
||
429 | |||
430 | /** |
||
431 | * {@inheritdoc} |
||
432 | */ |
||
433 | protected function reconnectIfMissingConnection() |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function disconnect() |
||
447 | |||
448 | /** |
||
449 | * @param CouchbaseBucket $bucket |
||
450 | * |
||
451 | * @return CouchbaseBucket |
||
452 | */ |
||
453 | protected function enableN1ql(CouchbaseBucket $bucket) |
||
462 | |||
463 | /** |
||
464 | * N1QL upsert query. |
||
465 | * |
||
466 | * @param string $query |
||
467 | * @param array $bindings |
||
468 | * |
||
469 | * @return int |
||
470 | */ |
||
471 | public function upsert($query, $bindings = []) |
||
475 | |||
476 | /** |
||
477 | * Get a new query builder instance. |
||
478 | * |
||
479 | * @return QueryBuilder|\Illuminate\Database\Query\Builder|Builder |
||
480 | */ |
||
481 | public function query() |
||
487 | |||
488 | /** |
||
489 | * @param string|null $bucket |
||
490 | * |
||
491 | * @return View |
||
492 | */ |
||
493 | 1 | public function view($bucket = null) |
|
499 | |||
500 | /** |
||
501 | * Run an update statement against the database. |
||
502 | * |
||
503 | * @param string $query |
||
504 | * @param array $bindings |
||
505 | * |
||
506 | * @return int|\stdClass |
||
507 | */ |
||
508 | public function update($query, $bindings = []) |
||
512 | |||
513 | /** |
||
514 | * Run a delete statement against the database. |
||
515 | * |
||
516 | * @param string $query |
||
517 | * @param array $bindings |
||
518 | * |
||
519 | * @return int|\stdClass |
||
520 | */ |
||
521 | public function delete($query, $bindings = []) |
||
525 | |||
526 | /** |
||
527 | * @return \string[] |
||
528 | */ |
||
529 | public function metrics() |
||
533 | |||
534 | /** |
||
535 | * @param \CouchbaseN1qlQuery $queryObject |
||
536 | */ |
||
537 | protected function firePreparedQuery(\CouchbaseN1qlQuery $queryObject) |
||
543 | |||
544 | /** |
||
545 | * @param mixed $returning |
||
546 | */ |
||
547 | protected function fireReturning($returning) |
||
553 | } |
||
554 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.