Completed
Push — master ( 76f9d5...d59f0b )
by Jeroen De
9s
created

includes/Maps_MappingService.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 {
13
14
	/**
15
	 * The internal name of the service.
16
	 *
17
	 * @var string
18
	 */
19
	protected $serviceName;
20
21
	/**
22
	 * A list of aliases for the internal name.
23
	 *
24
	 * @var array
25
	 */
26
	protected $aliases;
27
28
	/**
29
	 * A list of features that support the service, used for validation and defaulting.
30
	 *
31
	 * @var array
32
	 */
33
	protected $features;
34
35
	/**
36
	 * A list of names of resource modules to add.
37
	 *
38
	 * @var array
39
	 */
40
	protected $resourceModules = [];
41
42
	/**
43
	 * A list of dependencies (header items) that have been added.
44
	 *
45
	 * @var array
46
	 */
47
	private $addedDependencies = [];
48
49
	/**
50
	 * A list of dependencies (header items) that need to be added.
51
	 *
52
	 * @var array
53
	 */
54
	private $dependencies = [];
55
56
	/**
57
	 * @param string $serviceName
58
	 * @param array $aliases
59
	 */
60
	public function __construct( $serviceName, array $aliases = [] ) {
61
		$this->serviceName = $serviceName;
62
		$this->aliases = $aliases;
63
	}
64
65
	/**
66
	 * @since 0.7
67
	 *
68
	 * @param $parameterInfo array of IParam
69
	 */
70
	public function addParameterInfo( array &$parameterInfo ) {
71
	}
72
73
	/**
74
	 * @since 0.6.3
75
	 */
76
	public function addFeature( $featureName, $handlingClass ) {
77
		$this->features[$featureName] = $handlingClass;
78
	}
79
80
	/**
81
	 * @since 0.6.3
82
	 */
83
	public final function addDependencies( &$parserOrOut ) {
84
		$dependencies = $this->getDependencyHtml();
85
86
		// Only add a head item when there are dependencies.
87
		if ( $parserOrOut instanceof Parser ) {
0 ignored issues
show
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...
88
			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...
89
				$parserOrOut->getOutput()->addHeadItem( $dependencies );
90
			}
91
92
			$parserOrOut->getOutput()->addModules( $this->getResourceModules() );
93
		}
94
		elseif ( $parserOrOut instanceof OutputPage ) {
0 ignored issues
show
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...
95
			if ( $dependencies !== false ) {
96
				$parserOrOut->addHeadItem( md5( $dependencies ), $dependencies );
97
			}
98
99
			$parserOrOut->addModules( $this->getResourceModules() );
100
		}
101
	}
102
	
103
	/**
104
	 * Returns a list of all config variables that should be passed to the JS.
105
	 * 
106
	 * @since 1.0.1
107
	 * 
108
	 * @return array
109
	 */
110
	public function getConfigVariables() {
111
		return [];
112
	}
113
114
	/**
115
	 * @since 0.6.3
116
	 */
117
	public final function getDependencyHtml() {
118
		$allDependencies = array_merge( $this->getDependencies(), $this->dependencies );
119
		$dependencies = [];
120
121
		// Only add dependnecies that have not yet been added.
122
		foreach ( $allDependencies as $dependency ) {
123
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
124
				$dependencies[] = $dependency;
125
				$this->addedDependencies[] = $dependency;
126
			}
127
		}
128
129
		// If there are dependencies, put them all together in a string, otherwise return false.
130
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
131
	}
132
133
	/**
134
	 * Returns a list of html fragments, such as script includes, the current service depends on.
135
	 *
136
	 * @since 0.6.3
137
	 *
138
	 * @return array
139
	 */
140
	protected function getDependencies() {
141
		return [];
142
	}
143
144
	/**
145
	 * @since 0.6.3
146
	 */
147
	public function getName() {
148
		return $this->serviceName;
149
	}
150
151
	/**
152
	 * @since 0.6.3
153
	 */
154
	public function getFeature( $featureName ) {
155
		return array_key_exists( $featureName, $this->features ) ? $this->features[$featureName] : false;
156
	}
157
158
	/**
159
	 * @since 0.6.6
160
	 */
161
	public function getFeatureInstance( $featureName ) {
162
		$className = $this->getFeature( $featureName );
163
164
		if ( $className === false || !class_exists( $className ) ) {
165
			throw new MWException( 'Could not create a mapping feature class instance' );
166
		}
167
168
		return new $className( $this );
169
	}
170
171
	/**
172
	 * @since 0.6.3
173
	 */
174
	public function getAliases() {
175
		return $this->aliases;
176
	}
177
178
	/**
179
	 * @since 0.6.3
180
	 */
181
	public function hasAlias( $alias ) {
182
		return in_array( $alias, $this->aliases );
183
	}
184
185
	/**
186
	 * Returns the resource modules that need to be loaded to use this mapping service.
187
	 *
188
	 * @since 0.7.3
189
	 *
190
	 * @return array of string
191
	 */
192
	public function getResourceModules() {
193
		return $this->resourceModules;
194
	}
195
196
	/**
197
	 * Add one or more names of resource modules that should be loaded.
198
	 *
199
	 * @since 0.7.3
200
	 *
201
	 * @param mixed $modules Array of string or string
202
	 */
203
	public function addResourceModules( $modules ) {
204
		$this->resourceModules = array_merge( $this->resourceModules, (array)$modules );
205
	}
206
207
	/**
208
	 * @since 0.6.3
209
	 *
210
	 * @param $dependencyHtml
211
	 */
212
	public final function addHtmlDependency( $dependencyHtml ) {
213
		$this->dependencies[] = $dependencyHtml;
214
	}
215
216
	/**
217
	 * @param array $dependencies
218
	 */
219
	public function addHtmlDependencies(array $dependencies ) {
220
		foreach ( $dependencies as $dependency ) {
221
			$this->addHtmlDependency( $dependency );
222
		}
223
	}
224
225
	/**
226
	 * @since 1.0
227
	 */
228
	public function getEarthZoom() {
229
		return 1;
230
	}
231
232
}
233