Completed
Push — master ( eb7ec6...39bb91 )
by mw
93:13 queued 58:24
created

CircularReferenceGuard   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setMaxRecursionDepth() 0 3 1
A mark() 0 8 2
A unmark() 0 8 3
A isCircularByRecursionFor() 0 3 1
A get() 0 8 2
A reset() 0 3 1
1
<?php
2
3
namespace SMW\Utils;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 2.2
8
 *
9
 * @author mwjames
10
 */
11
class CircularReferenceGuard {
12
13
	/**
14
	 * @var array
15
	 */
16
	private static $circularRefGuard = array();
17
18
	/**
19
	 * @var string
20
	 */
21
	private $namespace = '';
22
23
	/**
24
	 * @var integer
25
	 */
26
	private $maxRecursionDepth = 1;
27
28
	/**
29
	 * @since 2.2
30
	 *
31
	 * @param string $namespace
32
	 */
33
	public function __construct( $namespace = '' ) {
34
		$this->namespace = $namespace;
35
	}
36
37
	/**
38
	 * @since 2.2
39
	 *
40
	 * @param integer $maxRecursionDepth
41
	 */
42
	public function setMaxRecursionDepth( $maxRecursionDepth ) {
43
		$this->maxRecursionDepth = (int)$maxRecursionDepth;
44
	}
45
46
	/**
47
	 * @since 2.2
48
	 *
49
	 * @param string $hash
50
	 */
51
	public function mark( $hash ) {
52
53
		if ( !isset( self::$circularRefGuard[$this->namespace][$hash] ) ) {
54
			self::$circularRefGuard[$this->namespace][$hash] = 0;
55
		}
56
57
		self::$circularRefGuard[$this->namespace][$hash]++;
58
	}
59
60
	/**
61
	 * @since 2.2
62
	 *
63
	 * @param string $hash
64
	 */
65
	public function unmark( $hash ) {
66
67
		if ( isset( self::$circularRefGuard[$this->namespace][$hash] ) && self::$circularRefGuard[$this->namespace][$hash] > 0 ) {
68
			return self::$circularRefGuard[$this->namespace][$hash]--;
69
		}
70
71
		unset( self::$circularRefGuard[$this->namespace][$hash] );
72
	}
73
74
	/**
75
	 * @since 2.2
76
	 *
77
	 * @param string $hash
78
	 *
79
	 * @return boolean
80
	 */
81
	public function isCircularByRecursionFor( $hash ) {
82
		return $this->get( $hash ) > $this->maxRecursionDepth;
83
	}
84
85
	/**
86
	 * @since 2.2
87
	 *
88
	 * @param string $hash
89
	 *
90
	 * @return integer
91
	 */
92
	public function get( $hash ) {
93
94
		if ( isset( self::$circularRefGuard[$this->namespace][$hash] ) ) {
95
			return self::$circularRefGuard[$this->namespace][$hash];
96
		}
97
98
		return 0;
99
	}
100
101
	/**
102
	 * @since 2.2
103
	 *
104
	 * @param string $namespace
105
	 */
106
	public function reset( $namespace ) {
107
		self::$circularRefGuard[$namespace] =  array();
108
	}
109
110
}
111