AutoCommitUpdate   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 54
rs 10
wmc 10
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
B doUpdate() 0 20 5
A cancelOnRollback() 0 5 2
A getOrigin() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * Deferrable Update for closure/callback updates that should use auto-commit mode
5
 * @since 1.28
6
 */
7
class AutoCommitUpdate implements DeferrableUpdate, DeferrableCallback {
8
	/** @var IDatabase */
9
	private $dbw;
10
	/** @var string */
11
	private $fname;
12
	/** @var callable|null */
13
	private $callback;
14
15
	/**
16
	 * @param IDatabase $dbw
17
	 * @param string $fname Caller name (usually __METHOD__)
18
	 * @param callable $callback Callback that takes (IDatabase, method name string)
19
	 */
20 View Code Duplication
	public function __construct( IDatabase $dbw, $fname, callable $callback ) {
21
		$this->dbw = $dbw;
22
		$this->fname = $fname;
23
		$this->callback = $callback;
24
25
		if ( $this->dbw->trxLevel() ) {
26
			$this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ], $fname );
27
		}
28
	}
29
30
	public function doUpdate() {
31
		if ( !$this->callback ) {
32
			return;
33
		}
34
35
		$autoTrx = $this->dbw->getFlag( DBO_TRX );
36
		$this->dbw->clearFlag( DBO_TRX );
37
		try {
38
			/** @var Exception $e */
39
			$e = null;
40
			call_user_func_array( $this->callback, [ $this->dbw, $this->fname ] );
41
		} catch ( Exception $e ) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
		}
43
		if ( $autoTrx ) {
44
			$this->dbw->setFlag( DBO_TRX );
45
		}
46
		if ( $e ) {
47
			throw $e;
48
		}
49
	}
50
51
	public function cancelOnRollback( $trigger ) {
52
		if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
53
			$this->callback = null;
54
		}
55
	}
56
57
	public function getOrigin() {
58
		return $this->fname;
59
	}
60
}
61