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

ExtraPropertyAnnotator::addAnnotation()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 1
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
ccs 0
cts 20
cp 0
crap 30
1
<?php
2
3
namespace SESP;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SESP\PropertyAnnotators\NullPropertyAnnotator;
8
use SESP\PropertyAnnotators\CreatorPropertyAnnotator;
9
use SESP\PropertyAnnotators\PageViewsPropertyAnnotator;
10
use SESP\PropertyAnnotators\LocalPropertyAnnotator;
11
use SESP\PropertyAnnotators\UserRegistrationDatePropertyAnnotator;
12
use SESP\PropertyAnnotators\UserEditCountPropertyAnnotator;
13
use SESP\PropertyAnnotators\PageIDPropertyAnnotator;
14
use SESP\PropertyAnnotators\ShortUrlPropertyAnnotator;
15
use SESP\PropertyAnnotators\ExifPropertyAnnotator;
16
use SESP\PropertyAnnotators\RevisionIDPropertyAnnotator;
17
use SESP\PropertyAnnotators\PageNumRevisionPropertyAnnotator;
18
use SESP\PropertyAnnotators\TalkPageNumRevisionPropertyAnnotator;
19
use SESP\PropertyAnnotators\PageContributorsPropertyAnnotator;
20
use SESP\PropertyAnnotators\SubPagePropertyAnnotator;
21
22
/**
23
 * @private
24
 *
25
 * @license GNU GPL v2+
26
 * @since 2.0
27
 *
28
 * @author mwjames
29
 */
30
class ExtraPropertyAnnotator {
31
32
	/**
33
	 * @var AppFactory
34
	 */
35
	private $appFactory;
36
37
	/**
38
	 * @var PropertyAnnotator[]
39
	 */
40
	private $propertyAnnotators = array();
41
42
	/**
43
	 * @var PropertyAnnotator
44
	 */
45
	private $localPropertyAnnotator;
46
47
	/**
48
	 * @var array
49
	 */
50
	private $options;
0 ignored issues
show
Unused Code introduced by
The property $options is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
51
52
	/**
53
	 * @since 2.0
54
	 *
55
	 * @param AppFactory $appFactory
56
	 */
57
	public function __construct( AppFactory $appFactory ) {
58
		$this->appFactory = $appFactory;
59
	}
60
61
	/**
62
	 * @since 2.0
63
	 *
64
	 * @param SemanticData $semanticData
65
	 */
66
	public function addAnnotation( SemanticData $semanticData ) {
67
68
		$time = microtime( true );
69
70
		if ( !$this->canAnnotate( $semanticData->getSubject() ) ) {
71
			return;
72
		}
73
74
		$propertyDefinitions = $this->appFactory->getPropertyDefinitions();
75
76
		foreach ( $this->appFactory->getOption( 'sespSpecialProperties', array() ) as $key ) {
0 ignored issues
show
Documentation introduced by
array() is of type array, 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...
77
78
			if ( !$propertyDefinitions->deepHas( $key, 'id' ) ) {
79
				continue;
80
			}
81
82
			$property = new DIProperty(
83
				$propertyDefinitions->deepGet( $key, 'id' )
84
			);
85
86
			if ( $propertyDefinitions->isLocalDef( $key ) ) {
87
				$this->localPropertyAnnotator->addAnnotation( $property, $semanticData );
88
			} else {
89
				$this->findPropertyAnnotator( $property )->addAnnotation( $property, $semanticData );
90
			}
91
		}
92
93
		$this->appFactory->getLogger()->info(
94
			__METHOD__ . ' (procTime in sec: '. ( microtime( true ) - $time ) . ')'
95
		);
96
	}
97
98
	/**
99
	 * @since 2.0
100
	 *
101
	 * @param DIProperty $property
102
	 *
103
	 * @return PropertyAnnotator
104
	 */
105
	public function findPropertyAnnotator( $property ) {
106
107
		$key = $property->getKey();
108
109
		if ( $this->propertyAnnotators === array() ) {
110
			$this->initDefaultPropertyAnnotators();
111
		}
112
113
		if ( isset( $this->propertyAnnotators[$key] ) && is_callable( $this->propertyAnnotators[$key] ) ) {
114
			return call_user_func( $this->propertyAnnotators[$key], $this->appFactory );
115
		} elseif( isset( $this->propertyAnnotators[$key] ) ) {
116
			return $this->propertyAnnotators[$key];
117
		}
118
119
		return new NullPropertyAnnotator();
120
	}
121
122
	/**
123
	 * @since 2.0
124
	 *
125
	 * @param PropertyAnnotator $propertyAnnotator
0 ignored issues
show
Bug introduced by
There is no parameter named $propertyAnnotator. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
126
	 */
127
	public function addPropertyAnnotator( $key, Closure $callback ) {
128
		$this->propertyAnnotators[$key] = $callback;
129
	}
130
131
	private function canAnnotate( $subject ) {
132
133
		if ( $subject === null || $subject->getTitle() === null || $subject->getTitle()->isSpecialPage() ) {
134
			return false;
135
		}
136
137
		$this->initDefaultPropertyAnnotators();
138
139
		return true;
140
	}
141
142
	private function initDefaultPropertyAnnotators() {
143
144
		$this->localPropertyAnnotator = new LocalPropertyAnnotator(
145
			$this->appFactory
146
		);
147
148
		// Encapsulate each instance to avoid direct instantiation for unused
149
		// matches
150
		$this->propertyAnnotators = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('___CUSER' => func...otator($appFactory); }) of type array<string,object<Clos...TA":"object<Closure>"}> is incompatible with the declared type array<integer,object<SESP\PropertyAnnotator>> of property $propertyAnnotators.

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...
151
152
			'___CUSER' => function( $appFactory ) {
153
				return new CreatorPropertyAnnotator( $appFactory );
154
			},
155
156
			'___VIEWS' => function( $appFactory ) {
157
				return new PageViewsPropertyAnnotator( $appFactory );
158
			},
159
160
			'___USERREG' => function( $appFactory ) {
161
				return new UserRegistrationDatePropertyAnnotator( $appFactory );
162
			},
163
164
			'___USEREDITCNT' => function( $appFactory ) {
165
				return new UserEditCountPropertyAnnotator( $appFactory );
166
			},
167
168
			'___PAGEID' => function( $appFactory ) {
169
				return new PageIDPropertyAnnotator( $appFactory );
170
			},
171
172
			'___REVID' => function( $appFactory ) {
173
				return new RevisionIDPropertyAnnotator( $appFactory );
174
			},
175
176
			'___NREV' => function( $appFactory ) {
177
				return new PageNumRevisionPropertyAnnotator( $appFactory );
178
			},
179
180
			'___NTREV' => function( $appFactory ) {
181
				return new TalkPageNumRevisionPropertyAnnotator( $appFactory );
182
			},
183
184
			'___EUSER' => function( $appFactory ) {
185
				return new PageContributorsPropertyAnnotator( $appFactory );
186
			},
187
188
			'___SUBP' => function( $appFactory ) {
189
				return new SubPagePropertyAnnotator( $appFactory );
190
			},
191
192
			'___SHORTURL' => function( $appFactory ) {
193
				return new ShortUrlPropertyAnnotator( $appFactory );
194
			},
195
196
			'___EXIFDATA' => function( $appFactory ) {
197
				return new ExifPropertyAnnotator( $appFactory );
198
			},
199
200
		);
201
	}
202
203
}
204