Completed
Push — master ( fef851...52af6f )
by Stephan
04:43
created

GlossaryCache   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 66
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getKeyForSubject() 0 4 1
A getKeyForLingo() 0 5 1
A getCacheType() 0 8 3
A getCache() 0 8 2
1
<?php
2
3
namespace SG\Cache;
4
5
use SMW\DIWikiPage;
6
7
use ObjectCache;
8
use BagOStuff;
9
10
/**
11
 * @ingroup SG
12
 * @ingroup SemanticGlossary
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class GlossaryCache {
20
21
	/* @var BagOstuff */
22
	private $cache = null;
23
24
	/**
25
	 * @since 1.1
26
	 *
27
	 * @param BagOStuff|null $cache
28
	 */
29 6
	public function __construct( BagOStuff $cache = null ) {
30 6
		$this->cache = $cache;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache can also be of type object<BagOStuff>. However, the property $cache is declared as type object<SG\Cache\BagOstuff>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31 6
	}
32
33
	/**
34
	 * @since 1.0
35
	 *
36
	 * @return BagOStuff
37
	 */
38 4
	public function getCache() {
39
40 4
		if ( $this->cache === null ) {
41 3
			$this->cache = ObjectCache::getInstance( self::getCacheType() );
42
		}
43
44 4
		return $this->cache;
45
	}
46
47
	/**
48
	 * @since 1.0
49
	 *
50
	 * @param DIWikiPage $subject
51
	 *
52
	 * @return string
53
	 */
54 4
	public function getKeyForSubject( DIWikiPage $subject ) {
55
		// FIXME Remove wfMemcKey dep.
56 4
		return wfMemcKey( 'ext', 'semanticglossary', $subject->getSerialization() );
57
	}
58
59
	/**
60
	 * @since 1.1
61
	 *
62
	 * @return string
63
	 */
64 3
	public function getKeyForLingo() {
65
		// FIXME Remove wfMemcKey dep.
66
		// This key should come from something like LingoCache::getKey()
67 3
		return wfMemcKey( 'ext', 'lingo', 'lingotree' );
68
	}
69
70
	/**
71
	 * @since 1.0
72
	 *
73
	 * @return string
74
	 */
75 5
	public function getCacheType() {
76
77 5
		if ( isset( $GLOBAL['wgexLingoCacheType'] ) && $GLOBAL['wgexLingoCacheType'] !== null ) {
0 ignored issues
show
Bug introduced by
The variable $GLOBAL seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
78
			return $GLOBAL['wgexLingoCacheType'];
79
		}
80
81 5
		return $GLOBALS['wgMainCacheType'];
82
	}
83
84
}
85