Completed
Branch master (dc3656)
by
unknown
30:14
created

DBConnRef   D

Complexity

Total Complexity 129

Size/Duplication

Total Lines 557
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 557
rs 4.8717
wmc 129
lcom 1
cbo 1

125 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __call() 0 8 2
A getServerInfo() 0 3 1
A bufferResults() 0 3 1
A trxLevel() 0 3 1
A trxTimestamp() 0 3 1
A tablePrefix() 0 3 1
A dbSchema() 0 3 1
A getLBInfo() 0 3 1
A setLBInfo() 0 3 1
A implicitGroupby() 0 3 1
A implicitOrderby() 0 3 1
A lastQuery() 0 3 1
A doneWrites() 0 3 1
A lastDoneWrites() 0 3 1
A writesPending() 0 3 1
A writesOrCallbacksPending() 0 3 1
A pendingWriteCallers() 0 3 1
A isOpen() 0 3 1
A setFlag() 0 3 1
A getFlag() 0 3 1
A getProperty() 0 3 1
A getWikiID() 0 3 1
A getType() 0 3 1
A open() 0 3 1
A fetchObject() 0 3 1
A fetchRow() 0 3 1
A numRows() 0 3 1
A numFields() 0 3 1
A fieldName() 0 3 1
A insertId() 0 3 1
A dataSeek() 0 3 1
A lastErrno() 0 3 1
A lastError() 0 3 1
A fieldInfo() 0 3 1
A affectedRows() 0 3 1
A getSoftwareLink() 0 3 1
A getServerVersion() 0 3 1
A close() 0 3 1
A reportConnectionError() 0 3 1
A query() 0 3 1
A reportQueryError() 0 3 1
A freeResult() 0 3 1
A selectField() 0 5 1
A selectFieldValues() 0 5 1
A select() 0 6 1
A selectSQLText() 0 6 1
A selectRow() 0 6 1
A estimateRowCount() 0 5 1
A selectRowCount() 0 5 1
A fieldExists() 0 3 1
A indexExists() 0 3 1
A tableExists() 0 3 1
A indexUnique() 0 3 1
A insert() 0 3 1
A update() 0 3 1
A makeList() 0 3 1
A makeWhereFrom2d() 0 3 1
A bitNot() 0 3 1
A bitAnd() 0 3 1
A bitOr() 0 3 1
A buildConcat() 0 3 1
A buildGroupConcatField() 0 5 1
A selectDB() 0 3 1
A getDBname() 0 3 1
A getServer() 0 3 1
A addQuotes() 0 3 1
A buildLike() 0 3 1
A anyChar() 0 3 1
A anyString() 0 3 1
A nextSequenceValue() 0 3 1
A replace() 0 3 1
A upsert() 0 5 1
A deleteJoin() 0 5 1
A delete() 0 3 1
A insertSelect() 0 6 1
A unionSupportsOrderAndLimit() 0 3 1
A unionQueries() 0 3 1
A conditional() 0 3 1
A strreplace() 0 3 1
A getServerUptime() 0 3 1
A wasDeadlock() 0 3 1
A wasLockTimeout() 0 3 1
A wasErrorReissuable() 0 3 1
A wasReadOnlyError() 0 3 1
A masterPosWait() 0 3 1
A getSlavePos() 0 3 1
A getMasterPos() 0 3 1
A serverIsReadOnly() 0 3 1
A onTransactionResolution() 0 3 1
A onTransactionIdle() 0 3 1
A onTransactionPreCommitOrIdle() 0 3 1
A explicitTrxActive() 0 3 1
A pendingWriteQueryDuration() 0 3 1
A clearFlag() 0 3 1
A restoreFlags() 0 3 1
A setTransactionListener() 0 3 1
A startAtomic() 0 3 1
A endAtomic() 0 3 1
A doAtomicSection() 0 3 1
A begin() 0 3 1
A commit() 0 3 1
A rollback() 0 3 1
A listTables() 0 3 1
A timestamp() 0 3 1
A timestampOrNull() 0 3 1
A ping() 0 5 2
A getLag() 0 3 1
A getSessionLagStatus() 0 3 1
A maxListLen() 0 3 1
A encodeBlob() 0 3 1
A decodeBlob() 0 3 1
A setSessionOptions() 0 3 1
A setSchemaVars() 0 3 1
A lockIsFree() 0 3 1
A lock() 0 3 1
A unlock() 0 3 1
A getScopedLockAndFlush() 0 3 1
A namedLocksEnqueue() 0 3 1
A getInfinity() 0 3 1
A encodeExpiry() 0 3 1
A decodeExpiry() 0 3 1
A setBigSelects() 0 3 1
A isReadOnly() 0 3 1
A __destruct() 0 5 2

How to fix   Complexity   

Complex Class

Complex classes like DBConnRef 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 DBConnRef, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
4
 * as well handling deferring the actual network connection until the handle is used
5
 *
6
 * @note: proxy methods are defined explicity to avoid interface errors
7
 * @ingroup Database
8
 * @since 1.22
9
 */
10
class DBConnRef implements IDatabase {
11
	/** @var LoadBalancer */
12
	private $lb;
13
14
	/** @var DatabaseBase|null */
15
	private $conn;
16
17
	/** @var array|null */
18
	private $params;
19
20
	/**
21
	 * @param LoadBalancer $lb
22
	 * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
23
	 */
24
	public function __construct( LoadBalancer $lb, $conn ) {
25
		$this->lb = $lb;
26
		if ( $conn instanceof DatabaseBase ) {
27
			$this->conn = $conn;
28
		} else {
29
			$this->params = $conn;
30
		}
31
	}
32
33
	function __call( $name, array $arguments ) {
34
		if ( $this->conn === null ) {
35
			list( $db, $groups, $wiki ) = $this->params;
36
			$this->conn = $this->lb->getConnection( $db, $groups, $wiki );
37
		}
38
39
		return call_user_func_array( [ $this->conn, $name ], $arguments );
40
	}
41
42
	public function getServerInfo() {
43
		return $this->__call( __FUNCTION__, func_get_args() );
44
	}
45
46
	public function bufferResults( $buffer = null ) {
47
		return $this->__call( __FUNCTION__, func_get_args() );
48
	}
49
50
	public function trxLevel() {
51
		return $this->__call( __FUNCTION__, func_get_args() );
52
	}
53
54
	public function trxTimestamp() {
55
		return $this->__call( __FUNCTION__, func_get_args() );
56
	}
57
58
	public function explicitTrxActive() {
59
		return $this->__call( __FUNCTION__, func_get_args() );
60
	}
61
62
	public function tablePrefix( $prefix = null ) {
63
		return $this->__call( __FUNCTION__, func_get_args() );
64
	}
65
66
	public function dbSchema( $schema = null ) {
67
		return $this->__call( __FUNCTION__, func_get_args() );
68
	}
69
70
	public function getLBInfo( $name = null ) {
71
		return $this->__call( __FUNCTION__, func_get_args() );
72
	}
73
74
	public function setLBInfo( $name, $value = null ) {
75
		return $this->__call( __FUNCTION__, func_get_args() );
76
	}
77
78
	public function implicitGroupby() {
79
		return $this->__call( __FUNCTION__, func_get_args() );
80
	}
81
82
	public function implicitOrderby() {
83
		return $this->__call( __FUNCTION__, func_get_args() );
84
	}
85
86
	public function lastQuery() {
87
		return $this->__call( __FUNCTION__, func_get_args() );
88
	}
89
90
	public function doneWrites() {
91
		return $this->__call( __FUNCTION__, func_get_args() );
92
	}
93
94
	public function lastDoneWrites() {
95
		return $this->__call( __FUNCTION__, func_get_args() );
96
	}
97
98
	public function writesPending() {
99
		return $this->__call( __FUNCTION__, func_get_args() );
100
	}
101
102
	public function writesOrCallbacksPending() {
103
		return $this->__call( __FUNCTION__, func_get_args() );
104
	}
105
106
	public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
107
		return $this->__call( __FUNCTION__, func_get_args() );
108
	}
109
110
	public function pendingWriteCallers() {
111
		return $this->__call( __FUNCTION__, func_get_args() );
112
	}
113
114
	public function isOpen() {
115
		return $this->__call( __FUNCTION__, func_get_args() );
116
	}
117
118
	public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
119
		return $this->__call( __FUNCTION__, func_get_args() );
120
	}
121
122
	public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
123
		return $this->__call( __FUNCTION__, func_get_args() );
124
	}
125
126
	public function restoreFlags( $state = self::RESTORE_PRIOR ) {
127
		return $this->__call( __FUNCTION__, func_get_args() );
128
	}
129
130
	public function getFlag( $flag ) {
131
		return $this->__call( __FUNCTION__, func_get_args() );
132
	}
133
134
	public function getProperty( $name ) {
135
		return $this->__call( __FUNCTION__, func_get_args() );
136
	}
137
138
	public function getWikiID() {
139
		return $this->__call( __FUNCTION__, func_get_args() );
140
	}
141
142
	public function getType() {
143
		return $this->__call( __FUNCTION__, func_get_args() );
144
	}
145
146
	public function open( $server, $user, $password, $dbName ) {
147
		return $this->__call( __FUNCTION__, func_get_args() );
148
	}
149
150
	public function fetchObject( $res ) {
151
		return $this->__call( __FUNCTION__, func_get_args() );
152
	}
153
154
	public function fetchRow( $res ) {
155
		return $this->__call( __FUNCTION__, func_get_args() );
156
	}
157
158
	public function numRows( $res ) {
159
		return $this->__call( __FUNCTION__, func_get_args() );
160
	}
161
162
	public function numFields( $res ) {
163
		return $this->__call( __FUNCTION__, func_get_args() );
164
	}
165
166
	public function fieldName( $res, $n ) {
167
		return $this->__call( __FUNCTION__, func_get_args() );
168
	}
169
170
	public function insertId() {
171
		return $this->__call( __FUNCTION__, func_get_args() );
172
	}
173
174
	public function dataSeek( $res, $row ) {
175
		return $this->__call( __FUNCTION__, func_get_args() );
176
	}
177
178
	public function lastErrno() {
179
		return $this->__call( __FUNCTION__, func_get_args() );
180
	}
181
182
	public function lastError() {
183
		return $this->__call( __FUNCTION__, func_get_args() );
184
	}
185
186
	public function fieldInfo( $table, $field ) {
187
		return $this->__call( __FUNCTION__, func_get_args() );
188
	}
189
190
	public function affectedRows() {
191
		return $this->__call( __FUNCTION__, func_get_args() );
192
	}
193
194
	public function getSoftwareLink() {
195
		return $this->__call( __FUNCTION__, func_get_args() );
196
	}
197
198
	public function getServerVersion() {
199
		return $this->__call( __FUNCTION__, func_get_args() );
200
	}
201
202
	public function close() {
203
		return $this->__call( __FUNCTION__, func_get_args() );
204
	}
205
206
	public function reportConnectionError( $error = 'Unknown error' ) {
207
		return $this->__call( __FUNCTION__, func_get_args() );
208
	}
209
210
	public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
211
		return $this->__call( __FUNCTION__, func_get_args() );
212
	}
213
214
	public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
215
		return $this->__call( __FUNCTION__, func_get_args() );
216
	}
217
218
	public function freeResult( $res ) {
219
		return $this->__call( __FUNCTION__, func_get_args() );
220
	}
221
222
	public function selectField(
223
		$table, $var, $cond = '', $fname = __METHOD__, $options = []
224
	) {
225
		return $this->__call( __FUNCTION__, func_get_args() );
226
	}
227
228
	public function selectFieldValues(
229
		$table, $var, $cond = '', $fname = __METHOD__, $options = []
230
	) {
231
		return $this->__call( __FUNCTION__, func_get_args() );
232
	}
233
234
	public function select(
235
		$table, $vars, $conds = '', $fname = __METHOD__,
236
		$options = [], $join_conds = []
237
	) {
238
		return $this->__call( __FUNCTION__, func_get_args() );
239
	}
240
241
	public function selectSQLText(
242
		$table, $vars, $conds = '', $fname = __METHOD__,
243
		$options = [], $join_conds = []
244
	) {
245
		return $this->__call( __FUNCTION__, func_get_args() );
246
	}
247
248
	public function selectRow(
249
		$table, $vars, $conds, $fname = __METHOD__,
250
		$options = [], $join_conds = []
251
	) {
252
		return $this->__call( __FUNCTION__, func_get_args() );
253
	}
254
255
	public function estimateRowCount(
256
		$table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
257
	) {
258
		return $this->__call( __FUNCTION__, func_get_args() );
259
	}
260
261
	public function selectRowCount(
262
		$tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
263
	) {
264
		return $this->__call( __FUNCTION__, func_get_args() );
265
	}
266
267
	public function fieldExists( $table, $field, $fname = __METHOD__ ) {
268
		return $this->__call( __FUNCTION__, func_get_args() );
269
	}
270
271
	public function indexExists( $table, $index, $fname = __METHOD__ ) {
272
		return $this->__call( __FUNCTION__, func_get_args() );
273
	}
274
275
	public function tableExists( $table, $fname = __METHOD__ ) {
276
		return $this->__call( __FUNCTION__, func_get_args() );
277
	}
278
279
	public function indexUnique( $table, $index ) {
280
		return $this->__call( __FUNCTION__, func_get_args() );
281
	}
282
283
	public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
284
		return $this->__call( __FUNCTION__, func_get_args() );
285
	}
286
287
	public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
288
		return $this->__call( __FUNCTION__, func_get_args() );
289
	}
290
291
	public function makeList( $a, $mode = LIST_COMMA ) {
292
		return $this->__call( __FUNCTION__, func_get_args() );
293
	}
294
295
	public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
296
		return $this->__call( __FUNCTION__, func_get_args() );
297
	}
298
299
	public function bitNot( $field ) {
300
		return $this->__call( __FUNCTION__, func_get_args() );
301
	}
302
303
	public function bitAnd( $fieldLeft, $fieldRight ) {
304
		return $this->__call( __FUNCTION__, func_get_args() );
305
	}
306
307
	public function bitOr( $fieldLeft, $fieldRight ) {
308
		return $this->__call( __FUNCTION__, func_get_args() );
309
	}
310
311
	public function buildConcat( $stringList ) {
312
		return $this->__call( __FUNCTION__, func_get_args() );
313
	}
314
315
	public function buildGroupConcatField(
316
		$delim, $table, $field, $conds = '', $join_conds = []
317
	) {
318
		return $this->__call( __FUNCTION__, func_get_args() );
319
	}
320
321
	public function selectDB( $db ) {
322
		return $this->__call( __FUNCTION__, func_get_args() );
323
	}
324
325
	public function getDBname() {
326
		return $this->__call( __FUNCTION__, func_get_args() );
327
	}
328
329
	public function getServer() {
330
		return $this->__call( __FUNCTION__, func_get_args() );
331
	}
332
333
	public function addQuotes( $s ) {
334
		return $this->__call( __FUNCTION__, func_get_args() );
335
	}
336
337
	public function buildLike() {
338
		return $this->__call( __FUNCTION__, func_get_args() );
339
	}
340
341
	public function anyChar() {
342
		return $this->__call( __FUNCTION__, func_get_args() );
343
	}
344
345
	public function anyString() {
346
		return $this->__call( __FUNCTION__, func_get_args() );
347
	}
348
349
	public function nextSequenceValue( $seqName ) {
350
		return $this->__call( __FUNCTION__, func_get_args() );
351
	}
352
353
	public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
354
		return $this->__call( __FUNCTION__, func_get_args() );
355
	}
356
357
	public function upsert(
358
		$table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
359
	) {
360
		return $this->__call( __FUNCTION__, func_get_args() );
361
	}
362
363
	public function deleteJoin(
364
		$delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
365
	) {
366
		return $this->__call( __FUNCTION__, func_get_args() );
367
	}
368
369
	public function delete( $table, $conds, $fname = __METHOD__ ) {
370
		return $this->__call( __FUNCTION__, func_get_args() );
371
	}
372
373
	public function insertSelect(
374
		$destTable, $srcTable, $varMap, $conds,
375
		$fname = __METHOD__, $insertOptions = [], $selectOptions = []
376
	) {
377
		return $this->__call( __FUNCTION__, func_get_args() );
378
	}
379
380
	public function unionSupportsOrderAndLimit() {
381
		return $this->__call( __FUNCTION__, func_get_args() );
382
	}
383
384
	public function unionQueries( $sqls, $all ) {
385
		return $this->__call( __FUNCTION__, func_get_args() );
386
	}
387
388
	public function conditional( $cond, $trueVal, $falseVal ) {
389
		return $this->__call( __FUNCTION__, func_get_args() );
390
	}
391
392
	public function strreplace( $orig, $old, $new ) {
393
		return $this->__call( __FUNCTION__, func_get_args() );
394
	}
395
396
	public function getServerUptime() {
397
		return $this->__call( __FUNCTION__, func_get_args() );
398
	}
399
400
	public function wasDeadlock() {
401
		return $this->__call( __FUNCTION__, func_get_args() );
402
	}
403
404
	public function wasLockTimeout() {
405
		return $this->__call( __FUNCTION__, func_get_args() );
406
	}
407
408
	public function wasErrorReissuable() {
409
		return $this->__call( __FUNCTION__, func_get_args() );
410
	}
411
412
	public function wasReadOnlyError() {
413
		return $this->__call( __FUNCTION__, func_get_args() );
414
	}
415
416
	public function masterPosWait( DBMasterPos $pos, $timeout ) {
417
		return $this->__call( __FUNCTION__, func_get_args() );
418
	}
419
420
	public function getSlavePos() {
421
		return $this->__call( __FUNCTION__, func_get_args() );
422
	}
423
424
	public function getMasterPos() {
425
		return $this->__call( __FUNCTION__, func_get_args() );
426
	}
427
428
	public function serverIsReadOnly() {
429
		return $this->__call( __FUNCTION__, func_get_args() );
430
	}
431
432
	public function onTransactionResolution( callable $callback ) {
433
		return $this->__call( __FUNCTION__, func_get_args() );
434
	}
435
436
	public function onTransactionIdle( callable $callback ) {
437
		return $this->__call( __FUNCTION__, func_get_args() );
438
	}
439
440
	public function onTransactionPreCommitOrIdle( callable $callback ) {
441
		return $this->__call( __FUNCTION__, func_get_args() );
442
	}
443
444
	public function setTransactionListener( $name, callable $callback = null ) {
445
		return $this->__call( __FUNCTION__, func_get_args() );
446
	}
447
448
	public function startAtomic( $fname = __METHOD__ ) {
449
		return $this->__call( __FUNCTION__, func_get_args() );
450
	}
451
452
	public function endAtomic( $fname = __METHOD__ ) {
453
		return $this->__call( __FUNCTION__, func_get_args() );
454
	}
455
456
	public function doAtomicSection( $fname, callable $callback ) {
457
		return $this->__call( __FUNCTION__, func_get_args() );
458
	}
459
460
	public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
461
		return $this->__call( __FUNCTION__, func_get_args() );
462
	}
463
464
	public function commit( $fname = __METHOD__, $flush = '' ) {
465
		return $this->__call( __FUNCTION__, func_get_args() );
466
	}
467
468
	public function rollback( $fname = __METHOD__, $flush = '' ) {
469
		return $this->__call( __FUNCTION__, func_get_args() );
470
	}
471
472
	public function listTables( $prefix = null, $fname = __METHOD__ ) {
473
		return $this->__call( __FUNCTION__, func_get_args() );
474
	}
475
476
	public function timestamp( $ts = 0 ) {
477
		return $this->__call( __FUNCTION__, func_get_args() );
478
	}
479
480
	public function timestampOrNull( $ts = null ) {
481
		return $this->__call( __FUNCTION__, func_get_args() );
482
	}
483
484
	public function ping( &$rtt = null ) {
485
		return func_num_args()
486
			? $this->__call( __FUNCTION__, [ &$rtt ] )
487
			: $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
488
	}
489
490
	public function getLag() {
491
		return $this->__call( __FUNCTION__, func_get_args() );
492
	}
493
494
	public function getSessionLagStatus() {
495
		return $this->__call( __FUNCTION__, func_get_args() );
496
	}
497
498
	public function maxListLen() {
499
		return $this->__call( __FUNCTION__, func_get_args() );
500
	}
501
502
	public function encodeBlob( $b ) {
503
		return $this->__call( __FUNCTION__, func_get_args() );
504
	}
505
506
	public function decodeBlob( $b ) {
507
		return $this->__call( __FUNCTION__, func_get_args() );
508
	}
509
510
	public function setSessionOptions( array $options ) {
511
		return $this->__call( __FUNCTION__, func_get_args() );
512
	}
513
514
	public function setSchemaVars( $vars ) {
515
		return $this->__call( __FUNCTION__, func_get_args() );
516
	}
517
518
	public function lockIsFree( $lockName, $method ) {
519
		return $this->__call( __FUNCTION__, func_get_args() );
520
	}
521
522
	public function lock( $lockName, $method, $timeout = 5 ) {
523
		return $this->__call( __FUNCTION__, func_get_args() );
524
	}
525
526
	public function unlock( $lockName, $method ) {
527
		return $this->__call( __FUNCTION__, func_get_args() );
528
	}
529
530
	public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
531
		return $this->__call( __FUNCTION__, func_get_args() );
532
	}
533
534
	public function namedLocksEnqueue() {
535
		return $this->__call( __FUNCTION__, func_get_args() );
536
	}
537
538
	public function getInfinity() {
539
		return $this->__call( __FUNCTION__, func_get_args() );
540
	}
541
542
	public function encodeExpiry( $expiry ) {
543
		return $this->__call( __FUNCTION__, func_get_args() );
544
	}
545
546
	public function decodeExpiry( $expiry, $format = TS_MW ) {
547
		return $this->__call( __FUNCTION__, func_get_args() );
548
	}
549
550
	public function setBigSelects( $value = true ) {
551
		return $this->__call( __FUNCTION__, func_get_args() );
552
	}
553
554
	public function isReadOnly() {
555
		return $this->__call( __FUNCTION__, func_get_args() );
556
	}
557
558
	/**
559
	 * Clean up the connection when out of scope
560
	 */
561
	function __destruct() {
562
		if ( $this->conn !== null ) {
563
			$this->lb->reuseConnection( $this->conn );
564
		}
565
	}
566
}
567