Completed
Pull Request — master (#75)
by mw
05:07 queued 03:21
created

ExtraPropertyAnnotator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
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
use SESP\PropertyAnnotators\PageLengthPropertyAnnotator;
22
23
/**
24
 * @private
25
 *
26
 * @license GNU GPL v2+
27
 * @since 2.0
28
 *
29
 * @author mwjames
30
 */
31
class ExtraPropertyAnnotator {
32
33
	/**
34
	 * @var AppFactory
35
	 */
36
	private $appFactory;
37
38
	/**
39
	 * @var PropertyAnnotator[]
40
	 */
41
	private $propertyAnnotators = array();
42
43
	/**
44
	 * @var PropertyAnnotator
45
	 */
46
	private $localPropertyAnnotator;
47
48
	/**
49
	 * @var array
50
	 */
51
	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...
52
53
	/**
54
	 * @since 2.0
55
	 *
56
	 * @param AppFactory $appFactory
57
	 */
58
	public function __construct( AppFactory $appFactory ) {
59
		$this->appFactory = $appFactory;
60
	}
61
62
	/**
63
	 * @since 2.0
64
	 *
65
	 * @param SemanticData $semanticData
66
	 */
67
	public function addAnnotation( SemanticData $semanticData ) {
68
69
		$time = microtime( true );
70
71
		if ( !$this->canAnnotate( $semanticData->getSubject() ) ) {
72
			return;
73
		}
74
75
		$propertyDefinitions = $this->appFactory->getPropertyDefinitions();
76
77
		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...
78
79
			if ( !$propertyDefinitions->deepHas( $key, 'id' ) ) {
80
				continue;
81
			}
82
83
			$property = new DIProperty(
84
				$propertyDefinitions->deepGet( $key, 'id' )
85
			);
86
87
			if ( $propertyDefinitions->isLocalDef( $key ) ) {
88
				$this->localPropertyAnnotator->addAnnotation( $property, $semanticData );
89
			} else {
90
				$this->findPropertyAnnotator( $property )->addAnnotation( $property, $semanticData );
91
			}
92
		}
93
94
		$this->appFactory->getLogger()->info(
95
			__METHOD__ . ' (procTime in sec: '. ( microtime( true ) - $time ) . ')'
96
		);
97
	}
98
99
	/**
100
	 * @since 2.0
101
	 *
102
	 * @param DIProperty $property
103
	 *
104
	 * @return PropertyAnnotator
105
	 */
106
	public function findPropertyAnnotator( DIProperty $property ) {
107
108
		$key = $property->getKey();
109
110
		if ( $this->propertyAnnotators === array() ) {
111
			$this->initDefaultPropertyAnnotators();
112
		}
113
114
		if ( isset( $this->propertyAnnotators[$key] ) && is_callable( $this->propertyAnnotators[$key] ) ) {
115
			return call_user_func( $this->propertyAnnotators[$key], $this->appFactory );
116
		} elseif( isset( $this->propertyAnnotators[$key] ) ) {
117
			return $this->propertyAnnotators[$key];
118
		}
119
120
		return new NullPropertyAnnotator();
121
	}
122
123
	/**
124
	 * @since 2.0
125
	 *
126
	 * @param string $key
127
	 * @param Closure $callbak
0 ignored issues
show
Documentation introduced by
There is no parameter named $callbak. Did you maybe mean $callback?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
128
	 */
129
	public function addPropertyAnnotator( $key, Closure $callback ) {
130
		$this->propertyAnnotators[$key] = $callback;
131
	}
132
133
	private function canAnnotate( $subject ) {
134
135
		if ( $subject === null || $subject->getTitle() === null || $subject->getTitle()->isSpecialPage() ) {
136
			return false;
137
		}
138
139
		$this->initDefaultPropertyAnnotators();
140
141
		return true;
142
	}
143
144
	private function initDefaultPropertyAnnotators() {
145
146
		$this->localPropertyAnnotator = new LocalPropertyAnnotator(
147
			$this->appFactory
148
		);
149
150
		// Encapsulate each instance to avoid direct instantiation for unused
151
		// matches
152
		$this->propertyAnnotators = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array(\SESP\PropertyAnno...otator($appFactory); }) of type array<string|integer,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...
153
154
			CreatorPropertyAnnotator::PROP_ID => function( $appFactory ) {
155
				return new CreatorPropertyAnnotator( $appFactory );
156
			},
157
158
			PageViewsPropertyAnnotator::PROP_ID => function( $appFactory ) {
159
				return new PageViewsPropertyAnnotator( $appFactory );
160
			},
161
162
			UserRegistrationDatePropertyAnnotator::PROP_ID => function( $appFactory ) {
163
				return new UserRegistrationDatePropertyAnnotator( $appFactory );
164
			},
165
166
			UserEditCountPropertyAnnotator::PROP_ID => function( $appFactory ) {
167
				return new UserEditCountPropertyAnnotator( $appFactory );
168
			},
169
170
			PageIDPropertyAnnotator::PROP_ID => function( $appFactory ) {
171
				return new PageIDPropertyAnnotator( $appFactory );
172
			},
173
174
			PageLengthPropertyAnnotator::PROP_ID => function( $appFactory ) {
175
				return new PageLengthPropertyAnnotator( $appFactory );
176
			},
177
178
			RevisionIDPropertyAnnotator::PROP_ID => function( $appFactory ) {
179
				return new RevisionIDPropertyAnnotator( $appFactory );
180
			},
181
182
			PageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) {
183
				return new PageNumRevisionPropertyAnnotator( $appFactory );
184
			},
185
186
			TalkPageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) {
187
				return new TalkPageNumRevisionPropertyAnnotator( $appFactory );
188
			},
189
190
			PageContributorsPropertyAnnotator::PROP_ID => function( $appFactory ) {
191
				return new PageContributorsPropertyAnnotator( $appFactory );
192
			},
193
194
			SubPagePropertyAnnotator::PROP_ID => function( $appFactory ) {
195
				return new SubPagePropertyAnnotator( $appFactory );
196
			},
197
198
			ShortUrlPropertyAnnotator::PROP_ID => function( $appFactory ) {
199
				return new ShortUrlPropertyAnnotator( $appFactory );
200
			},
201
202
			ExifPropertyAnnotator::PROP_ID => function( $appFactory ) {
203
				return new ExifPropertyAnnotator( $appFactory );
204
			},
205
206
		);
207
	}
208
209
}
210