Completed
Push — refact-v2 ( 9bddae...f926b0 )
by mw
05:08
created

initDefaultPropertyAnnotators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 64
ccs 33
cts 33
cp 1
crap 1
rs 9.3956
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 15
	public function __construct( AppFactory $appFactory ) {
59 15
		$this->appFactory = $appFactory;
60 15
	}
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 14
	public function findPropertyAnnotator( DIProperty $property ) {
107
108 14
		$key = $property->getKey();
109
110 14
		if ( $this->propertyAnnotators === array() ) {
111 14
			$this->initDefaultPropertyAnnotators();
112 14
		}
113
114 14
		if ( isset( $this->propertyAnnotators[$key] ) && is_callable( $this->propertyAnnotators[$key] ) ) {
115 13
			return call_user_func( $this->propertyAnnotators[$key], $this->appFactory );
116 1
		} elseif( isset( $this->propertyAnnotators[$key] ) ) {
117
			return $this->propertyAnnotators[$key];
118
		}
119
120 1
		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 14
	private function initDefaultPropertyAnnotators() {
145
146 14
		$this->localPropertyAnnotator = new LocalPropertyAnnotator(
147 14
			$this->appFactory
148 14
		);
149
150
		// Encapsulate each instance to avoid direct instantiation for unused
151
		// matches
152 14
		$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 1
				return new CreatorPropertyAnnotator( $appFactory );
156 14
			},
157
158
			PageViewsPropertyAnnotator::PROP_ID => function( $appFactory ) {
159 1
				return new PageViewsPropertyAnnotator( $appFactory );
160 14
			},
161
162
			UserRegistrationDatePropertyAnnotator::PROP_ID => function( $appFactory ) {
163 1
				return new UserRegistrationDatePropertyAnnotator( $appFactory );
164 14
			},
165
166
			UserEditCountPropertyAnnotator::PROP_ID => function( $appFactory ) {
167 1
				return new UserEditCountPropertyAnnotator( $appFactory );
168 14
			},
169
170
			PageIDPropertyAnnotator::PROP_ID => function( $appFactory ) {
171 1
				return new PageIDPropertyAnnotator( $appFactory );
172 14
			},
173
174
			PageLengthPropertyAnnotator::PROP_ID => function( $appFactory ) {
175 1
				return new PageLengthPropertyAnnotator( $appFactory );
176 14
			},
177
178
			RevisionIDPropertyAnnotator::PROP_ID => function( $appFactory ) {
179 1
				return new RevisionIDPropertyAnnotator( $appFactory );
180 14
			},
181
182
			PageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) {
183 1
				return new PageNumRevisionPropertyAnnotator( $appFactory );
184 14
			},
185
186
			TalkPageNumRevisionPropertyAnnotator::PROP_ID => function( $appFactory ) {
187 1
				return new TalkPageNumRevisionPropertyAnnotator( $appFactory );
188 14
			},
189
190
			PageContributorsPropertyAnnotator::PROP_ID => function( $appFactory ) {
191 1
				return new PageContributorsPropertyAnnotator( $appFactory );
192 14
			},
193
194
			SubPagePropertyAnnotator::PROP_ID => function( $appFactory ) {
195 1
				return new SubPagePropertyAnnotator( $appFactory );
196 14
			},
197
198
			ShortUrlPropertyAnnotator::PROP_ID => function( $appFactory ) {
199 1
				return new ShortUrlPropertyAnnotator( $appFactory );
200 14
			},
201
202 14
			ExifPropertyAnnotator::PROP_ID => function( $appFactory ) {
203 1
				return new ExifPropertyAnnotator( $appFactory );
204 14
			},
205
206
		);
207 14
	}
208
209
}
210