Completed
Push — master ( 49ddd3...7189a8 )
by mw
02:22
created

LabelFetcher   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 113
ccs 41
cts 43
cp 0.9535
rs 10
wmc 13
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setLabelCacheVersion() 0 3 1
A getLabel() 0 3 1
B getLabelsFrom() 0 32 6
A matchLabel() 0 14 3
1
<?php
2
3
namespace SESP;
4
5
use Onoi\Cache\Cache;
6
use Onoi\Cache\NullCache;
7
use SMW\Message;
8
9
/**
10
 * @ingroup SESP
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.0
14
 *
15
 * @author mwjames
16
 */
17
class LabelFetcher {
18
19
	/**
20
	 * Namespace of the cache instance
21
	 */
22
	const LABEL_CACHE_NAMESPACE = 'sesp:labels';
23
24
	/**
25
	 * @var Cache
26
	 */
27
	private $cache;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $languageCode = 'en';
33
34
	/**
35
	 * @var string
36
	 */
37
	private $labelCacheVersion = 0;
38
39
	/**
40
	 * @since 2.0
41
	 *
42
	 * @param Cache|null $cache
43
	 * @param string $languageCode
44
	 */
45 5
	public function __construct( Cache $cache = null, $languageCode = 'en' ) {
46 5
		$this->cache = $cache;
47 5
		$this->languageCode = $languageCode;
48
49 5
		if ( $this->cache === null ) {
50
			$this->cache = new NullCache();
51
		}
52 5
	}
53
54
	/**
55
	 * @since 2.0
56
	 *
57
	 * @param integer|string $labelCacheVersion
58
	 */
59 1
	public function setLabelCacheVersion( $labelCacheVersion ) {
60 1
		$this->labelCacheVersion = $labelCacheVersion;
0 ignored issues
show
Documentation Bug introduced by
It seems like $labelCacheVersion can also be of type integer. However, the property $labelCacheVersion is declared as type string. 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...
61 1
	}
62
63
	/**
64
	 * @since 2.0
65
	 *
66
	 * @param string $key
67
	 *
68
	 * @return string
69
	 */
70 1
	public function getLabel( $key ) {
71 1
		return Message::get( $key, null, Message::USER_LANGUAGE );
72
	}
73
74
	/**
75
	 * @since 2.0
76
	 *
77
	 * @param PropertyDefinitions $propertyDefinitions
78
	 *
79
	 * @return array
80
	 */
81 3
	public function getLabelsFrom( PropertyDefinitions $propertyDefinitions ) {
82
83 3
		$hash = smwfCacheKey(
84 3
			self::LABEL_CACHE_NAMESPACE,
85
			array(
0 ignored issues
show
Documentation introduced by
array($propertyDefinitio...his->labelCacheVersion) is of type array<integer,object<SES..."string","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86 3
				$propertyDefinitions,
87 3
				$this->languageCode,
88 3
				$this->labelCacheVersion
89 3
			)
90 3
		);
91
92 3
		if ( $this->labelCacheVersion !== false && ( $labels = $this->cache->fetch( $hash ) ) !== false ) {
93 2
			return $labels;
94
		}
95
96 1
		$labels = array();
97 1
		$exifDefinitions = array();
0 ignored issues
show
Unused Code introduced by
$exifDefinitions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
99 1
		foreach ( $propertyDefinitions as $key => $definition ) {
100 1
			$this->matchLabel( $labels, $definition );
101 1
		}
102
103 1
		foreach ( $propertyDefinitions->safeGet( '_EXIF', array() ) as $key => $definition ) {
104 1
			$this->matchLabel( $labels, $definition );
105 1
		}
106
107 1
		if ( $labels !== array() ) {
108 1
			$this->cache->save( $hash, $labels, 3600 * 24 );
109 1
		}
110
111 1
		return $labels;
112
	}
113
114 1
	private function matchLabel( &$labels, $definition ) {
115
116 1
		if ( !isset( $definition['id'] ) ) {
117 1
			return;
118
		}
119
120 1
		$alias = 'sesp-property-unknown-label';
121
122 1
		if ( isset( $definition['alias'] ) ) {
123 1
			$alias = $definition['alias'];
124 1
		}
125
126 1
		$labels[$definition['id']] = Message::get( $alias, null, $this->languageCode );
127 1
	}
128
129
}
130