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

ApprovedStatusPropertyAnnotator::addAnnotation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.1384

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 9
cts 14
cp 0.6429
rs 8.8571
cc 5
eloc 11
nc 4
nop 2
crap 6.1384
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDIString as DIString;
8
use SESP\PropertyAnnotator;
9
use SESP\AppFactory;
10
use LogReader;
11
12
/**
13
 * @private
14
 * @ingroup SESP
15
 *
16
 * @license GNU GPL v2+
17
 */
18
class ApprovedStatusPropertyAnnotator implements PropertyAnnotator {
19
20
	/**
21
	 * Predefined property ID
22
	 */
23
	const PROP_ID = '___APPROVEDSTATUS';
24
25
	/**
26
	 * @var AppFactory
27
	 */
28
	private $appFactory;
29
30
	/**
31
	 * @var Integer|null
32
	 */
33
	private $approvedStatus;
34
35
	/**
36
	 * @param AppFactory $appFactory
37
	 */
38 4
	public function __construct( AppFactory $appFactory ) {
39 4
		$this->appFactory = $appFactory;
40 4
	}
41
42
	/**
43
	 * @since 2.0
44
	 *
45
	 * @param string $approvedStatus
46
	 */
47 2
	public function setApprovedStatus( $approvedStatus ) {
48 2
		$this->approvedStatus = $approvedStatus;
0 ignored issues
show
Documentation Bug introduced by
It seems like $approvedStatus of type string is incompatible with the declared type integer|null of property $approvedStatus.

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...
49 2
	}
50
51
	/**
52
	 * {@inheritDoc}
53
	 */
54 1
	public function isAnnotatorFor( DIProperty $property ) {
55 1
		return $property->getKey() === self::PROP_ID;
56
	}
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61 2
	public function addAnnotation(
62
		DIProperty $property, SemanticData $semanticData
63
	) {
64 2
		if ( $this->approvedStatus === null && class_exists( 'ApprovedRevs' ) ) {
65
			$logReader = $this->appFactory->newDatabaseLogReader(
66
				$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...
67
			);
68
			$this->approvedStatus = $logReader->getStatus();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getStatus() of type object<SESP\Timestamp> is incompatible with the declared type integer|null of property $approvedStatus.

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...
69
		}
70
71 2
		if ( is_string( $this->approvedStatus ) && $this->approvedStatus !== "" ) {
72 1
			$semanticData->addPropertyObjectValue(
73 1
				$property, new DIString( $this->approvedStatus )
0 ignored issues
show
Deprecated Code introduced by
The class SMWDIString has been deprecated with message: Will be removed after SMW 1.9; use SMWDIBlob instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
74 1
			);
75 1
		} else {
76 1
			$semanticData->removeProperty( $property );
77
		}
78 2
	}
79
}
80