Completed
Pull Request — master (#98)
by Mark A.
02:00
created

ApprovedByPropertyAnnotator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 81.48%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 69
ccs 22
cts 27
cp 0.8148
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setApprovedBy() 0 3 1
A isAnnotatorFor() 0 3 1
B addAnnotation() 0 25 6
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SESP\AppFactory;
6
use SESP\PropertyAnnotator;
7
use SESP\DatabaseLogReader;
8
use SMW\DIWikiPage;
9
use SMWDataItem as DataItem;
10
use SMW\DIProperty;
11
use SMW\SemanticData;
12
use Title;
13
use User;
14
15
/**
16
 * @private
17
 * @ingroup SESP
18
 *
19
 * @license GNU GPL v2+
20
 */
21
class ApprovedByPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * Predefined property ID
25
	 */
26
	const PROP_ID = '___APPROVEDBY';
27
28
	/**
29
	 * @var AppFactory
30
	 */
31
	private $appFactory;
32
33
	/**
34
	 * @var Integer|null
35
	 */
36
	private $approvedBy;
37
38
	/**
39
	 * @param AppFactory $appFactory
40
	 */
41 4
	public function __construct( AppFactory $appFactory ) {
42 4
		$this->appFactory = $appFactory;
43 4
	}
44
45
	/**
46
	 * @since 2.0
47
	 *
48
	 * @param User $approvedBy
49
	 */
50 2
	public function setApprovedBy( $approvedBy ) {
51 2
		$this->approvedBy = $approvedBy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $approvedBy of type object<User> is incompatible with the declared type integer|null of property $approvedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52 2
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57 1
	public function isAnnotatorFor( DIProperty $property ) {
58 1
		return $property->getKey() === self::PROP_ID;
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64 2
	public function addAnnotation(
65
		DIProperty $property, SemanticData $semanticData
66
	) {
67 2
		if ( $this->approvedBy === null && class_exists( 'ApprovedRevs' ) ) {
68
			$logReader = $this->appFactory->newDatabaseLogReader(
69
				$semanticData->getSubject()->getTitle(), 'approval'
0 ignored issues
show
Bug introduced by
It seems like $semanticData->getSubject()->getTitle() can be null; however, newDatabaseLogReader() 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...
70
			);
71
			$this->approvedBy = $logReader->getUser();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getUser() of type object<User> is incompatible with the declared type integer|null of property $approvedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
		}
73
74 2
		$dataItem = null;
75 2
		if ( $this->approvedBy instanceof User ) {
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...
76 1
			$userPage = $this->approvedBy->getUserPage();
77
78 1
			if ( $userPage instanceof Title ) {
0 ignored issues
show
Bug introduced by
The class Title 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...
79 1
				$dataItem = DIWikiPage::newFromTitle( $userPage );
80 1
			}
81 1
		}
82
83 2
		if ( $dataItem instanceof DataItem ) {
84 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
85 1
		} else {
86 1
			$semanticData->removeProperty( $property );
87
		}
88 2
	}
89
}
90