ShortUrlPropertyAnnotator::isAnnotatorFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDIUri as DIUri;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
use Title;
12
use RuntimeException;
13
14
/**
15
 * @private
16
 * @ingroup SESP
17
 *
18
 * @license GNU GPL v2+
19
 * @since 2.0
20
 *
21
 * @author mwjames
22
 * @author rotsee
23
 */
24
class ShortUrlPropertyAnnotator implements PropertyAnnotator {
25
26
	/**
27
	 * Predefined property ID
28
	 */
29
	const PROP_ID = '___SHORTURL';
30
31
	/**
32
	 * @var AppFactory
33
	 */
34
	private $appFactory;
35
36
	/**
37
	 * @since 2.0
38
	 *
39
	 * @param AppFactory $appFactory
40
	 */
41 2
	public function __construct( AppFactory $appFactory ) {
42 2
		$this->appFactory = $appFactory;
43 2
	}
44
45
	/**
46
	 * @since 2.0
47
	 *
48
	 * {@inheritDoc}
49
	 */
50 1
	public function isAnnotatorFor( DIProperty $property ) {
51 1
		return $property->getKey() === self::PROP_ID;
52
	}
53
54
	/**
55
	 * @since 2.0
56
	 *
57
	 * {@inheritDoc}
58
	 */
59 2
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
60
61 2
		if ( !$this->hasShortUrlUtils() ) {
62 1
			throw new RuntimeException( 'Class ShortUrlUtils is not available' );
63
		}
64
65 1
		$dataItem = null;
66 1
		$shortUrl = $this->getShortUrl( $semanticData->getSubject()->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like $semanticData->getSubject()->getTitle() can be null; however, getShortUrl() 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...
67
68 1
		if ( $shortUrl !== null ) {
69 1
			$dataItem = new DIUri( 'http', $shortUrl, '', '' );
70 1
		}
71
72 1
		if ( $dataItem instanceof DataItem ) {
73 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
74 1
		}
75 1
	}
76
77
	protected function getShortUrl( Title $title ) {
78
79
		//FIXME handle internal and external links
80
		$shortUrl = null;
81
82
		if ( \ShortUrlUtils::needsShortUrl( $title ) ) {
83
			$shortUrl = $this->getUrlPrefix() . \ShortUrlUtils::encodeTitle( $title );
84
		}
85
86
		return $shortUrl;
87
	}
88
89
	protected function getUrlPrefix() {
90
91
		$shortUrlPrefix = $this->appFactory->getOption( 'wgShortUrlPrefix', '' );
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
93
		if ( $shortUrlPrefix === '' ) {
94
			return SpecialPage::getTitleFor( 'ShortUrl' )->getFullUrl() . '/';
95
		}
96
97
		return $shortUrlPrefix;
98
	}
99
100
	protected function hasShortUrlUtils() {
101
		return class_exists( 'ShortUrlUtils' );
102
	}
103
104
}
105