Completed
Branch master (420c52)
by
unknown
26:22
created

AtomicSectionUpdate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 22.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 9
loc 40
rs 10
wmc 7
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A doUpdate() 0 5 2
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 via IDatabase::doAtomicSection()
5
 * @since 1.27
6
 */
7
class AtomicSectionUpdate implements DeferrableUpdate, DeferrableCallback {
8
	/** @var IDatabase */
9
	private $dbw;
10
	/** @var string */
11
	private $fname;
12
	/** @var callable */
13
	private $callback;
14
15
	/**
16
	 * @param IDatabase $dbw
17
	 * @param string $fname Caller name (usually __METHOD__)
18
	 * @param callable $callback
19
	 * @see IDatabase::doAtomicSection()
20
	 */
21 View Code Duplication
	public function __construct( IDatabase $dbw, $fname, callable $callback ) {
22
		$this->dbw = $dbw;
23
		$this->fname = $fname;
24
		$this->callback = $callback;
25
26
		if ( $this->dbw->trxLevel() ) {
27
			$this->dbw->onTransactionResolution( [ $this, 'cancelOnRollback' ] );
28
		}
29
	}
30
31
	public function doUpdate() {
32
		if ( $this->callback ) {
33
			$this->dbw->doAtomicSection( $this->fname, $this->callback );
34
		}
35
	}
36
37
	public function cancelOnRollback( $trigger ) {
38
		if ( $trigger === IDatabase::TRIGGER_ROLLBACK ) {
39
			$this->callback = null;
40
		}
41
	}
42
43
	public function getOrigin() {
44
		return $this->fname;
45
	}
46
}
47