Completed
Pull Request — master (#75)
by mw
04:29 queued 02:37
created

PropertyAnnotators/CreatorPropertyAnnotator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
	 * Predefined property ID
25
	 */
26
	const PROP_ID = '___CUSER';
27
28
	/**
29
	 * @var AppFactory
30
	 */
31
	private $appFactory;
32
33
	/**
34
	 * @since 2.0
35
	 *
36
	 * @param AppFactory $appFactory
37
	 */
38 3
	public function __construct( AppFactory $appFactory ) {
39 3
		$this->appFactory = $appFactory;
40 3
	}
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 1
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
57
58 1
		$page = $this->appFactory->newWikiPage( $semanticData->getSubject()->getTitle() );
0 ignored issues
show
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...
59 1
		$creator = $page->getCreator();
60 1
		$dataItem = null;
61
62 1
		if ( $creator ) {
63 1
			$dataItem = DIWikiPage::newFromTitle( $creator->getUserPage() );
64 1
		}
65
66 1
		if ( $dataItem instanceof DataItem ) {
67 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
68 1
		}
69 1
	}
70
71
}
72