Completed
Push — master ( 1811ff...1fe2bc )
by mw
08:02
created

ApprovedDatePropertyAnnotator::getDataItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
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
	public function __construct( AppFactory $appFactory ) {
39
		$this->appFactory = $appFactory;
40
	}
41
42
	/**
43
	 * @since 2.0
44
	 *
45
	 * @param Integer $approvedDate
46
	 */
47
	public function setApprovedDate( $approvedDate ) {
48
		$this->approvedDate = $approvedDate;
49
	}
50
51
	/**
52
	 * {@inheritDoc}
53
	 */
54
	public function isAnnotatorFor( DIProperty $property ) {
55
		return $property->getKey() === self::PROP_ID;
56
	}
57
58
	public function getDataItem() {
59
		if ( $this->approvedDate ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->approvedDate of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
60
			$date = $this->approvedDate;
61
			return new DITime(
62
				DITime::CM_GREGORIAN,
63
				$date->format( 'Y' ),
0 ignored issues
show
Bug introduced by
The method format cannot be called on $date (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
				$date->format( 'm' ),
0 ignored issues
show
Bug introduced by
The method format cannot be called on $date (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
				$date->format( 'd' ),
0 ignored issues
show
Bug introduced by
The method format cannot be called on $date (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
				$date->format( 'H' ),
0 ignored issues
show
Bug introduced by
The method format cannot be called on $date (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
				$date->format( 'i' )
0 ignored issues
show
Bug introduced by
The method format cannot be called on $date (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
			);
69
		}
70
	}
71
72
	/**
73
	 * {@inheritDoc}
74
	 */
75
	public function addAnnotation(
76
		DIProperty $property, SemanticData $semanticData
77
	) {
78
		if ( $this->approvedDate === null && class_exists( 'ApprovedRevs' ) ) {
79
			$logReader = $this->appFactory->newDatabaseLogReader(
80
				$semanticData->getSubject()->getTitle(), 'approval'
81
			);
82
			$this->approvedDate = $logReader->getDateOfLogEntry();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getDateOfLogEntry() 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...
83
		}
84
85
		$dataItem = $this->getDataItem();
86
		if ( $dataItem ) {
87
			$semanticData->addPropertyObjectValue( $property, $dataItem );
88
		} else {
89
			$semanticData->removeProperty( $property );
90
		}
91
	}
92
}
93