Completed
Push — master ( d2d28e...1c2760 )
by mw
35:37
created

PropertyAnnotator/RedirectPropertyAnnotator.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 SMW\PropertyAnnotator;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use SMW\MediaWiki\RedirectTargetFinder;
8
use SMW\PropertyAnnotator;
9
10
/**
11
 * Handling redirect annotation
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.9
15
 *
16
 * @author mwjames
17
 */
18
class RedirectPropertyAnnotator extends PropertyAnnotatorDecorator {
19
20
	/**
21
	 * @var RedirectTargetFinder
22
	 */
23
	private $redirectTargetFinder;
24
25
	/**
26
	 * @since 1.9
27
	 *
28
	 * @param PropertyAnnotator $propertyAnnotator
29
	 * @param RedirectTargetFinder $redirectTargetFinder
30
	 */
31 193
	public function __construct( PropertyAnnotator $propertyAnnotator, RedirectTargetFinder $redirectTargetFinder ) {
32 193
		parent::__construct( $propertyAnnotator );
33 193
		$this->redirectTargetFinder = $redirectTargetFinder;
34 193
	}
35
36
	/**
37
	 * @see PropertyAnnotatorDecorator::addPropertyValues
38
	 */
39 192
	protected function addPropertyValues() {
40
41 192
		if ( !$this->redirectTargetFinder->hasRedirectTarget() ) {
42 190
			return;
43
		}
44
45 26
		$this->getSemanticData()->addPropertyObjectValue(
46 26
			new DIProperty( '_REDI' ),
47 26
			DIWikiPage::newFromTitle( $this->redirectTargetFinder->getRedirectTarget() )
0 ignored issues
show
It seems like $this->redirectTargetFinder->getRedirectTarget() can be null; however, newFromTitle() 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...
48
		);
49 26
	}
50
51
}
52