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 |
||
36 | class CouchbaseConnection extends Connection |
||
37 | { |
||
38 | /** @var string */ |
||
39 | protected $bucket; |
||
40 | |||
41 | /** @var Cluster */ |
||
42 | protected $connection; |
||
43 | |||
44 | /** @var */ |
||
45 | protected $managerUser; |
||
46 | |||
47 | /** @var */ |
||
48 | protected $managerPassword; |
||
49 | |||
50 | /** @var array */ |
||
51 | protected $options = []; |
||
52 | |||
53 | /** @var int */ |
||
54 | protected $fetchMode = 0; |
||
55 | |||
56 | /** @var array */ |
||
57 | protected $enableN1qlServers = []; |
||
58 | |||
59 | /** @var string */ |
||
60 | protected $bucketPassword = ''; |
||
61 | |||
62 | /** @var string[] */ |
||
63 | protected $metrics; |
||
64 | |||
65 | /** @var int default consistency */ |
||
66 | protected $consistency = N1qlQuery::NOT_BOUNDED; |
||
67 | |||
68 | /** @var string[] function to handle the retrieval of various properties. */ |
||
69 | private $properties = [ |
||
70 | 'operationTimeout', |
||
71 | 'viewTimeout', |
||
72 | 'durabilityInterval', |
||
73 | 'durabilityTimeout', |
||
74 | 'httpTimeout', |
||
75 | 'configTimeout', |
||
76 | 'configDelay', |
||
77 | 'configNodeTimeout', |
||
78 | 'htconfigIdleTimeout', |
||
79 | ]; |
||
80 | |||
81 | /** @var array */ |
||
82 | protected $config = []; |
||
83 | |||
84 | /** @var string */ |
||
85 | private $name; |
||
86 | |||
87 | /** @var bool */ |
||
88 | private $crossBucket = true; |
||
89 | |||
90 | /** |
||
91 | * @param array $config |
||
92 | * @param string $name |
||
93 | */ |
||
94 | 92 | public function __construct(array $config, $name) |
|
104 | |||
105 | /** |
||
106 | * @param string $password |
||
107 | * |
||
108 | * @return CouchbaseConnection |
||
109 | */ |
||
110 | public function setBucketPassword(string $password): CouchbaseConnection |
||
116 | |||
117 | /** |
||
118 | * @param string $name |
||
119 | * |
||
120 | * @return Bucket |
||
121 | */ |
||
122 | 40 | public function openBucket(string $name): Bucket |
|
131 | |||
132 | /** |
||
133 | * @return ClusterManager |
||
134 | */ |
||
135 | 12 | public function manager(): ClusterManager |
|
139 | |||
140 | /** |
||
141 | * @param Bucket $bucket |
||
142 | * |
||
143 | * @return string[] |
||
144 | */ |
||
145 | 2 | public function getOptions(Bucket $bucket): array |
|
154 | |||
155 | /** |
||
156 | * @param Bucket $bucket |
||
157 | */ |
||
158 | 16 | protected function registerOption(Bucket $bucket) |
|
166 | |||
167 | /** |
||
168 | * @return Processor |
||
169 | */ |
||
170 | 92 | protected function getDefaultPostProcessor() |
|
174 | |||
175 | /** |
||
176 | * @return Grammar |
||
177 | */ |
||
178 | 92 | protected function getDefaultQueryGrammar() |
|
182 | |||
183 | /** |
||
184 | * @return Builder|\Illuminate\Database\Schema\Builder |
||
185 | */ |
||
186 | 16 | public function getSchemaBuilder() |
|
190 | |||
191 | /** |
||
192 | * @param array $config enable(array), options(array), administrator(array), bucket_password(string) |
||
193 | */ |
||
194 | 92 | protected function getManagedConfigure(array $config) |
|
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | 18 | public function getName() |
|
215 | |||
216 | /** |
||
217 | * @return \Couchbase\Cluster |
||
218 | */ |
||
219 | protected function createConnection(): Cluster |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function getDriverName() |
||
237 | |||
238 | /** |
||
239 | * @return Cluster |
||
240 | */ |
||
241 | 72 | public function getCouchbase(): Cluster |
|
249 | |||
250 | /** |
||
251 | * @param string $table |
||
252 | * |
||
253 | * @return QueryBuilder |
||
254 | */ |
||
255 | 16 | public function table($table) |
|
259 | |||
260 | /** |
||
261 | * @param int $consistency |
||
262 | * @param callable $callback |
||
263 | * |
||
264 | * @return mixed |
||
265 | */ |
||
266 | 2 | public function callableConsistency(int $consistency, callable $callback) |
|
273 | |||
274 | /** |
||
275 | * @param int $consistency |
||
276 | * |
||
277 | * @return CouchbaseConnection |
||
278 | */ |
||
279 | public function consistency(int $consistency): CouchbaseConnection |
||
285 | |||
286 | /** |
||
287 | * @param bool $cross |
||
288 | */ |
||
289 | public function crossBucket(bool $cross): void |
||
293 | |||
294 | /** |
||
295 | * @param string $bucket |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | 18 | public function bucket(string $bucket): CouchbaseConnection |
|
305 | |||
306 | /** |
||
307 | * @param N1qlQuery $query |
||
308 | * |
||
309 | * @return mixed |
||
310 | */ |
||
311 | 16 | protected function executeQuery(N1qlQuery $query) |
|
321 | |||
322 | /** |
||
323 | * @param string $query |
||
324 | * @param array $bindings |
||
325 | * |
||
326 | * @return \stdClass |
||
327 | */ |
||
328 | 10 | protected function execute(string $query, array $bindings = []) |
|
339 | |||
340 | /** |
||
341 | * {@inheritdoc} |
||
342 | */ |
||
343 | public function select($query, $bindings = [], $useReadPdo = true) |
||
364 | |||
365 | /** |
||
366 | * {@inheritdoc} |
||
367 | */ |
||
368 | public function cursor($query, $bindings = [], $useReadPdo = true) |
||
383 | |||
384 | /** |
||
385 | * @param string $query |
||
386 | * @param array $bindings |
||
387 | * |
||
388 | * @return int|mixed |
||
389 | */ |
||
390 | 10 | public function insert($query, $bindings = []) |
|
394 | |||
395 | /** |
||
396 | * {@inheritdoc} |
||
397 | */ |
||
398 | public function affectingStatement($query, $bindings = []) |
||
417 | |||
418 | /** |
||
419 | * @param string $query |
||
420 | * @param array $bindings |
||
421 | * |
||
422 | * @return mixed |
||
423 | */ |
||
424 | public function positionalStatement(string $query, array $bindings = []) |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | 2 | public function transaction(Closure $callback, $attempts = 1) |
|
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | 2 | public function beginTransaction() |
|
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | 2 | public function commit() |
|
467 | |||
468 | /** |
||
469 | * {@inheritdoc} |
||
470 | */ |
||
471 | 2 | public function rollBack($toLevel = null) |
|
475 | |||
476 | /** |
||
477 | * {@inheritdoc} |
||
478 | */ |
||
479 | 16 | protected function reconnectIfMissingConnection() |
|
485 | |||
486 | /** |
||
487 | * {@inheritdoc} |
||
488 | */ |
||
489 | 14 | public function disconnect() |
|
493 | |||
494 | /** |
||
495 | * N1QL upsert query. |
||
496 | * |
||
497 | * @param string $query |
||
498 | * @param array $bindings |
||
499 | * |
||
500 | * @return int |
||
501 | */ |
||
502 | 2 | public function upsert(string $query, array $bindings = []) |
|
506 | |||
507 | /** |
||
508 | * Get a new query builder instance. |
||
509 | * |
||
510 | * @return QueryBuilder |
||
511 | */ |
||
512 | 16 | public function query() |
|
518 | |||
519 | /** |
||
520 | * @param string|null $bucket |
||
521 | * |
||
522 | * @return View |
||
523 | */ |
||
524 | 2 | public function view(string $bucket = null): View |
|
530 | |||
531 | /** |
||
532 | * Run an update statement against the database. |
||
533 | * |
||
534 | * @param string $query |
||
535 | * @param array $bindings |
||
536 | * |
||
537 | * @return int|\stdClass |
||
538 | */ |
||
539 | 2 | public function update($query, $bindings = []) |
|
543 | |||
544 | /** |
||
545 | * Run a delete statement against the database. |
||
546 | * |
||
547 | * @param string $query |
||
548 | * @param array $bindings |
||
549 | * |
||
550 | * @return int|\stdClass |
||
551 | */ |
||
552 | 4 | public function delete($query, $bindings = []) |
|
556 | |||
557 | /** |
||
558 | * @return \string[] |
||
559 | */ |
||
560 | 2 | public function metrics(): array |
|
564 | |||
565 | /** |
||
566 | * @param N1qlQuery $queryObject |
||
567 | */ |
||
568 | 16 | protected function firePreparedQuery(N1qlQuery $queryObject) |
|
574 | |||
575 | /** |
||
576 | * @param mixed $returning |
||
577 | */ |
||
578 | 16 | protected function fireReturning($returning) |
|
584 | |||
585 | /** |
||
586 | * @param null|\PDO $pdo |
||
587 | * |
||
588 | * @return $this |
||
589 | */ |
||
590 | 14 | public function setPdo($pdo) |
|
599 | } |
||
600 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.