Completed
Push — master ( 21ccfa...6f5178 )
by Mark A.
08:49
created

NamespacePropertyAnnotator::getDataItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use MediaWiki\MediaWikiServices;
6
use SESP\AppFactory;
7
use SESP\PropertyAnnotator;
8
use SESP\DatabaseLogReader;
9
use SMWDataItem as DataItem;
10
use SMWDIString as DIString;
11
use SMW\DIProperty;
12
use SMW\SemanticData;
13
use Title;
14
use User;
15
16
/**
17
 * @private
18
 * @ingroup SESP
19
 *
20
 * @license GNU GPL v2+
21
 */
22
class NamespacePropertyAnnotator implements PropertyAnnotator {
23
24
	/**
25
	 * Predefined property ID
26
	 */
27
	const PROP_ID = '___NAMESPACE';
28
29
	/**
30
	 * @var AppFactory
31
	 */
32
	private $appFactory;
33
34
	/**
35
	 * @var Integer|null
36
	 */
37
	private $namespace;
38
39
	/**
40
	 * @param AppFactory $appFactory
41
	 */
42
	public function __construct( AppFactory $appFactory ) {
43
		$this->appFactory = $appFactory;
44
	}
45
46
	/**
47
	 * @since 2.0
48
	 *
49
	 * @param User $namespace
50
	 */
51
	public function setNamespace( $namespace ) {
52
		$this->namespace = $namespace;
0 ignored issues
show
Documentation Bug introduced by
It seems like $namespace of type object<User> is incompatible with the declared type integer|null of property $namespace.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
	}
54
55
	/**
56
	 * {@inheritDoc}
57
	 */
58
	public function isAnnotatorFor( DIProperty $property ) {
59
		return $property->getKey() === self::PROP_ID;
60
	}
61
62
	/**
63
	 * {@inheritDoc}
64
	 */
65
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
66
		if ( $this->namespace === null ) {
67
			$title = $semanticData->getSubject()->getTitle();
68
			$nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
69
70
			$this->namespace = $nsInfo->getCanonicalName( $title->getNamespace() );
71
			if ( "" === $this->namespace ) {
72
				$this->namespace = "Main";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Main' of type string is incompatible with the declared type integer|null of property $namespace.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
			}
74
		}
75
76
		$dataItem = $this->getDataItem();
77
78
		if ( $dataItem ) {
79
			$semanticData->addPropertyObjectValue( $property, $dataItem );
80
		} else {
81
			$semanticData->removeProperty( $property );
82
		}
83
	}
84
85
	private function getDataItem() {
86
		return new DIString( $this->namespace );
87
	}
88
89
}
90