Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

MapsMappingService::addHtmlDependencies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Base class for mapping services. Deriving classes hold mapping service specific
5
 * information and functionality, which can be used by any mapping feature.
6
 *
7
 * @since 0.6.3
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
abstract class MapsMappingService implements iMappingService {
13
14
	/**
15
	 * The internal name of the service.
16
	 *
17
	 * @since 0.6.3
18
	 *
19
	 * @var string
20
	 */
21
	protected $serviceName;
22
23
	/**
24
	 * A list of aliases for the internal name.
25
	 *
26
	 * @since 0.6.3
27
	 *
28
	 * @var array
29
	 */
30
	protected $aliases;
31
32
	/**
33
	 * A list of features that support the service, used for validation and defaulting.
34
	 *
35
	 * @since 0.6.3
36
	 *
37
	 * @var array
38
	 */
39
	protected $features;
40
41
	/**
42
	 * A list of names of resource modules to add.
43
	 *
44
	 * @since 0.7.3
45
	 *
46
	 * @var array
47
	 */
48
	protected $resourceModules = [];
49
50
	/**
51
	 * A list of dependencies (header items) that have been added.
52
	 *
53
	 * @since 0.6.3
54
	 *
55
	 * @var array
56
	 */
57
	private $addedDependencies = [];
58
59
	/**
60
	 * A list of dependencies (header items) that need to be added.
61
	 *
62
	 * @since 0.6.3
63
	 *
64
	 * @var array
65
	 */
66
	private $dependencies = [];
67
68
	/**
69
	 * Constructor. Creates a new instance of MapsMappingService.
70
	 *
71
	 * @since 0.6.3
72
	 *
73
	 * @param string $serviceName
74
	 * @param array $aliases
75
	 */
76
	public function __construct( $serviceName, array $aliases = [] ) {
77
		$this->serviceName = $serviceName;
78
		$this->aliases = $aliases;
79
	}
80
81
	/**
82
	 * @see iMappingService::addParameterInfo
83
	 *
84
	 * @since 0.7
85
	 *
86
	 * @param $parameterInfo array of IParam
87
	 */
88
	public function addParameterInfo( array &$parameterInfo ) {
89
	}
90
91
	/**
92
	 * @see iMappingService::addFeature
93
	 *
94
	 * @since 0.6.3
95
	 */
96
	public function addFeature( $featureName, $handlingClass ) {
97
		$this->features[$featureName] = $handlingClass;
98
	}
99
100
	/**
101
	 * @see iMappingService::addDependencies
102
	 *
103
	 * @since 0.6.3
104
	 */
105
	public final function addDependencies( &$parserOrOut ) {
106
		$dependencies = $this->getDependencyHtml();
107
108
		// Only add a head item when there are dependencies.
109
		if ( $parserOrOut instanceof Parser ) {
0 ignored issues
show
Bug introduced by
The class Parser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
110
			if ( $dependencies ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dependencies of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

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

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
111
				$parserOrOut->getOutput()->addHeadItem( $dependencies );
112
			}
113
114
			$parserOrOut->getOutput()->addModules( $this->getResourceModules() );
115
		}
116
		elseif ( $parserOrOut instanceof OutputPage ) {
0 ignored issues
show
Bug introduced by
The class OutputPage does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117
			if ( $dependencies !== false ) {
118
				$parserOrOut->addHeadItem( md5( $dependencies ), $dependencies );
119
			}
120
121
			$parserOrOut->addModules( $this->getResourceModules() );
122
		}
123
	}
124
	
125
	/**
126
	 * Returns a list of all config variables that should be passed to the JS.
127
	 * 
128
	 * @since 1.0.1
129
	 * 
130
	 * @return array
131
	 */
132
	public function getConfigVariables() {
133
		return [];
134
	}
135
136
	/**
137
	 * @see iMappingService::getDependencyHtml
138
	 *
139
	 * @since 0.6.3
140
	 */
141
	public final function getDependencyHtml() {
142
		$allDependencies = array_merge( $this->getDependencies(), $this->dependencies );
143
		$dependencies = [];
144
145
		// Only add dependnecies that have not yet been added.
146
		foreach ( $allDependencies as $dependency ) {
147
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
148
				$dependencies[] = $dependency;
149
				$this->addedDependencies[] = $dependency;
150
			}
151
		}
152
153
		// If there are dependencies, put them all together in a string, otherwise return false.
154
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
155
	}
156
157
	/**
158
	 * Returns a list of html fragments, such as script includes, the current service depends on.
159
	 *
160
	 * @since 0.6.3
161
	 *
162
	 * @return array
163
	 */
164
	protected function getDependencies() {
165
		return [];
166
	}
167
168
	/**
169
	 * @see iMappingService::getName
170
	 *
171
	 * @since 0.6.3
172
	 */
173
	public function getName() {
174
		return $this->serviceName;
175
	}
176
177
	/**
178
	 * @see iMappingService::getFeature
179
	 *
180
	 * @since 0.6.3
181
	 */
182
	public function getFeature( $featureName ) {
183
		return array_key_exists( $featureName, $this->features ) ? $this->features[$featureName] : false;
184
	}
185
186
	/**
187
	 * @see iMappingService::getFeatureInstance
188
	 *
189
	 * @since 0.6.6
190
	 */
191
	public function getFeatureInstance( $featureName ) {
192
		$className = $this->getFeature( $featureName );
193
194
		if ( $className === false || !class_exists( $className ) ) {
195
			throw new MWException( 'Could not create a mapping feature class instance' );
196
		}
197
198
		return new $className( $this );
199
	}
200
201
	/**
202
	 * @see iMappingService::getAliases
203
	 *
204
	 * @since 0.6.3
205
	 */
206
	public function getAliases() {
207
		return $this->aliases;
208
	}
209
210
	/**
211
	 * @see iMappingService::hasAlias
212
	 *
213
	 * @since 0.6.3
214
	 */
215
	public function hasAlias( $alias ) {
216
		return in_array( $alias, $this->aliases );
217
	}
218
219
	/**
220
	 * Returns the resource modules that need to be loaded to use this mapping service.
221
	 *
222
	 * @since 0.7.3
223
	 *
224
	 * @return array of string
225
	 */
226
	public function getResourceModules() {
227
		return $this->resourceModules;
228
	}
229
230
	/**
231
	 * Add one or more names of resource modules that should be loaded.
232
	 *
233
	 * @since 0.7.3
234
	 *
235
	 * @param mixed $modules Array of string or string
236
	 */
237
	public function addResourceModules( $modules ) {
238
		$this->resourceModules = array_merge( $this->resourceModules, (array)$modules );
239
	}
240
241
	/**
242
	 * @since 0.6.3
243
	 *
244
	 * @param $dependencyHtml
245
	 */
246
	public final function addHtmlDependency( $dependencyHtml ) {
247
		$this->dependencies[] = $dependencyHtml;
248
	}
249
250
	/**
251
	 * Adds dependencies.
252
	 *
253
	 * @param array $dependencies
254
	 */
255
	public function addHtmlDependencies(array $dependencies ) {
256
		foreach ( $dependencies as $dependency ) {
257
			$this->addHtmlDependency( $dependency );
258
		}
259
	}
260
261
	/**
262
	 * @see iMappingService::getEarthZoom
263
	 *
264
	 * @since 1.0
265
	 */
266
	public function getEarthZoom() {
267
		return 1;
268
	}
269
270
}
271