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

ApprovedDatePropertyAnnotator::addAnnotation()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.2259

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
ccs 19
cts 24
cp 0.7917
rs 8.439
cc 5
eloc 20
nc 8
nop 2
crap 5.2259
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDITime as DITime;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
12
/**
13
 * @private
14
 * @ingroup SESP
15
 *
16
 * @license GNU GPL v2+
17
 */
18
class ApprovedDatePropertyAnnotator implements PropertyAnnotator {
19
20
	/**
21
	 * Predefined property ID
22
	 */
23
	const PROP_ID = '___APPROVEDDATE';
24
25
	/**
26
	 * @var AppFactory
27
	 */
28
	private $appFactory;
29
30
	/**
31
	 * @var Integer|null
32
	 */
33
	private $approvedDate;
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 Integer $approvedDate
46
	 */
47 2
	public function setApprovedDate( $approvedDate ) {
48 2
		$this->approvedDate = $approvedDate;
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->approvedDate === 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->approvedDate = $logReader->getDate();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getDate() can also be of type object<MWTimestamp>. However, the property $approvedDate is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
		}
70
71 2
		$dataItem = null;
72 2
		if ( $this->approvedDate ) {
73 1
			$date = $this->approvedDate;
74 1
			$dataItem = new DITime(
75 1
				DITime::CM_GREGORIAN,
76 1
				$date->format( 'Y' ),
77 1
				$date->format( 'm' ),
78 1
				$date->format( 'd' ),
79 1
				$date->format( 'H' ),
80 1
				$date->format( 'i' )
81 1
			);
82 1
		}
83
84 2
		if ( $dataItem instanceof DataItem ) {
85 1
			$semanticData->addPropertyObjectValue( $property, $dataItem );
86 1
		} else {
87 1
			$semanticData->removeProperty( $property );
88
		}
89 2
	}
90
}
91