Completed
Push — refact-v2 ( 0317e0 )
by mw
04:47
created

UserRegistrationDatePropertyAnnotator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
ccs 0
cts 29
cp 0
wmc 6
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isAnnotatorFor() 0 3 1
B addAnnotation() 0 30 4
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
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 UserRegistrationDatePropertyAnnotator implements PropertyAnnotator {
23
24
	/**
25
	 * @var AppFactory
26
	 */
27
	private $appFactory;
28
29
	/**
30
	 * @since 2.0
31
	 *
32
	 * @param AppFactory $appFactory
33
	 */
34
	public function __construct( AppFactory $appFactory ) {
35
		$this->appFactory = $appFactory;
36
	}
37
38
	/**
39
	 * @since 2.0
40
	 *
41
	 * {@inheritDoc}
42
	 */
43
	public function isAnnotatorFor( DIProperty $property ) {
44
		return $property->getKey() === '___USERREG' ;
45
	}
46
47
	/**
48
	 * @since 2.0
49
	 *
50
	 * {@inheritDoc}
51
	 */
52
	public function addAnnotation( DIProperty $property, SemanticData $semanticData ) {
53
54
		$title = $semanticData->getSubject()->getTitle();
55
56
		if ( !$title->inNamespace( NS_USER ) ) {
57
			return;
58
		}
59
60
		$user = $this->appFactory->newUserFromTitle( $title );
0 ignored issues
show
Bug introduced by
It seems like $title defined by $semanticData->getSubject()->getTitle() on line 54 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...
61
		$dataItem = null;
62
63
		if ( $user 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...
64
65
			$timestamp = wfTimestamp( TS_ISO_8601, $user->getRegistration() );
66
			$date = new \DateTime( $timestamp );
67
68
			$dataItem = new DITime(
69
				DITime::CM_GREGORIAN,
70
				$date->format('Y'),
71
				$date->format('m'),
0 ignored issues
show
Documentation introduced by
$date->format('m') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
				$date->format('d'),
0 ignored issues
show
Documentation introduced by
$date->format('d') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
				$date->format('H'),
0 ignored issues
show
Documentation introduced by
$date->format('H') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
				$date->format('i')
0 ignored issues
show
Documentation introduced by
$date->format('i') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
			);
76
		}
77
78
		if ( $dataItem instanceof DataItem ) {
79
			$semanticData->addPropertyObjectValue( $property, $dataItem );
80
		}
81
	}
82
83
}
84