Completed
Push — master ( 6f7547...dda6b2 )
by mw
02:42
created

UserBlockPropertyAnnotator::addAnnotation()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.042

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
ccs 17
cts 19
cp 0.8947
rs 8.439
cc 6
eloc 17
nc 5
nop 2
crap 6.042
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDIBlob as DIBlob;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
use User;
12
13
/**
14
 * @private
15
 * @ingroup SESP
16
 *
17
 * @license GNU GPL v2+
18
 * @since 2.0
19
 *
20
 * @author mwjames
21
 */
22
class UserBlockPropertyAnnotator implements PropertyAnnotator {
23
24
	/**
25
	 * Predefined property ID
26
	 */
27
	const PROP_ID = '___USERBLOCK';
28
29
	/**
30
	 * @var AppFactory
31
	 */
32
	private $appFactory;
33
34
	/**
35
	 * @since 2.0
36
	 *
37
	 * @param AppFactory $appFactory
38
	 */
39 8
	public function __construct( AppFactory $appFactory ) {
40 8
		$this->appFactory = $appFactory;
41 8
	}
42
43
	/**
44
	 * @since 2.0
45
	 *
46
	 * {@inheritDoc}
47
	 */
48 1
	public function isAnnotatorFor( DIProperty $property ) {
49 1
		return $property->getKey() === self::PROP_ID;
50
	}
51
52
	/**
53
	 * @since 2.0
54
	 *
55
	 * {@inheritDoc}
56
	 */
57 6
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
58
59 6
		$title = $semanticData->getSubject()->getTitle();
60
61 6
		if ( !$title->inNamespace( NS_USER ) ) {
62
			return;
63
		}
64
65 6
		$user = $this->appFactory->newUserFromTitle(
66
			$title
0 ignored issues
show
Bug introduced by
It seems like $title defined by $semanticData->getSubject()->getTitle() on line 59 can be null; however, SESP\AppFactory::newUserFromTitle() 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 6
		);
68
69 6
		if ( !$user instanceof User || ( $block = $user->getBlock() ) === null ) {
0 ignored issues
show
Bug introduced by
The class User does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
70
			return;
71
		}
72
73
		$actions = [
74 6
			'edit',
75 6
			'createaccount',
76 6
			'sendemail',
77 6
			'editownusertalk',
78
			'read'
79 6
		];
80
81 6
		foreach ( $actions as $action ) {
82 6
			if ( $block->prevents( $action ) ) {
83 5
				$semanticData->addPropertyObjectValue( $property, new DIBlob( $action ) );
84 5
			}
85 6
		}
86 6
	}
87
88
}
89