RevisionIDPropertyAnnotator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAnnotatorFor() 0 3 1
A addAnnotation() 0 17 4
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDINumber as DINumber;
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 RevisionIDPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * Predefined property ID
25
	 */
26
	const PROP_ID = '___REVID';
27
28
	/**
29
	 * @var AppFactory
30
	 */
31
	private $appFactory;
32
33
	/**
34
	 * @since 2.0
35
	 *
36
	 * @param AppFactory $appFactory
37
	 */
38 4
	public function __construct( AppFactory $appFactory ) {
39 4
		$this->appFactory = $appFactory;
40 4
	}
41
42
	/**
43
	 * @since 2.0
44
	 *
45
	 * {@inheritDoc}
46
	 */
47 1
	public function isAnnotatorFor( DIProperty $property ) {
48 1
		return $property->getKey() === self::PROP_ID;
49
	}
50
51
	/**
52
	 * @since 2.0
53
	 *
54
	 * {@inheritDoc}
55
	 */
56 2
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
57
58 2
		$page = $this->appFactory->newWikiPage(
59 2
			$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...
60 2
		);
61
62 2
		$revID = $page->getLatest();
63 2
		$dataItem = null;
64
65 2
		if ( is_integer( $revID ) && $revID > 0 ) {
66 1
			$dataItem = new DINumber( $revID );
67 1
		}
68
69 2
		if ( $dataItem instanceof DataItem ) {
70 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
71 1
		}
72 2
	}
73
74
}
75