Completed
Push — refact-v2 ( 0317e0 )
by mw
04:47
created

CreatorPropertyAnnotator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
wmc 5
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAnnotatorFor() 0 3 1
A addAnnotation() 0 14 3
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use SMW\SemanticData;
8
use SMWDataItem as DataItem;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
12
/**
13
 * @private
14
 * @ingroup SESP
15
 *
16
 * @license GNU GPL v2+
17
 * @since 2.0
18
 *
19
 * @author mwjames
20
 */
21
class CreatorPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * @var AppFactory
25
	 */
26
	private $appFactory;
27
28
	/**
29
	 * @since 2.0
30
	 *
31
	 * @param AppFactory $appFactory
32
	 */
33 3
	public function __construct( AppFactory $appFactory ) {
34 3
		$this->appFactory = $appFactory;
35 3
	}
36
37
	/**
38
	 * @since 2.0
39
	 *
40
	 * {@inheritDoc}
41
	 */
42 1
	public function isAnnotatorFor( DIProperty $property ) {
43 1
		return $property->getKey() === '___CUSER' ;
44
	}
45
46
	/**
47
	 * @since 2.0
48
	 *
49
	 * {@inheritDoc}
50
	 */
51 1
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
52
53 1
		$page = $this->appFactory->newWikiPage( $semanticData->getSubject()->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like $semanticData->getSubject()->getTitle() can be null; however, newWikiPage() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
54 1
		$creator = $page->getCreator();
55 1
		$dataItem = null;
56
57 1
		if ( $creator ) {
58 1
			$dataItem = DIWikiPage::newFromTitle( $creator->getUserPage() );
59 1
		}
60
61 1
		if ( $dataItem instanceof DataItem ) {
62 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
63 1
		}
64 1
	}
65
66
}
67