Completed
Push — cleanz ( 70f885...f1882e )
by Jeroen De
26:41 queued 16:46
created

MapsFunctions::getEvenMawrCommonParameters()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 8.109
c 0
b 0
f 0
cc 3
nc 3
nop 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 Maps;
4
5
use ImagePage;
6
use Title;
7
use Xml;
8
9
/**
10
 * A class that holds static helper functions for generic mapping-related functions.
11
 *
12
 * @deprecated
13
 *
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
final class MapsFunctions {
18
19
	/**
20
	 * Encode a variable of unknown type to JavaScript.
21
	 * Arrays are converted to JS arrays, objects are converted to JS associative
22
	 * arrays (objects). So cast your PHP associative arrays to objects before
23
	 * passing them to here.
24
	 *
25
	 * This is a copy of
26
	 *
27
	 * @see Xml::encodeJsVar
28
	 * which fixes incorrect behaviour with floats.
29
	 *
30
	 * @since 0.7.1
31
	 *
32
	 * @param mixed $value
33
	 *
34
	 * @return string
35
	 */
36
	public static function encodeJsVar( $value ) {
37
		if ( is_bool( $value ) ) {
38
			$s = $value ? 'true' : 'false';
39
		} elseif ( is_null( $value ) ) {
40
			$s = 'null';
41
		} elseif ( is_int( $value ) || is_float( $value ) ) {
42
			$s = $value;
43
		} elseif ( is_array( $value ) && // Make sure it's not associative.
44
			array_keys( $value ) === range( 0, count( $value ) - 1 ) ||
45
			count( $value ) == 0
46
		) {
47
			$s = '[';
48
			foreach ( $value as $elt ) {
0 ignored issues
show
Bug introduced by
The expression $value of type object|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
				if ( $s != '[' ) {
50
					$s .= ', ';
51
				}
52
				$s .= self::encodeJsVar( $elt );
53
			}
54
			$s .= ']';
55
		} elseif ( is_object( $value ) || is_array( $value ) ) {
56
			// Objects and associative arrays
57
			$s = '{';
58
			foreach ( (array)$value as $name => $elt ) {
59
				if ( $s != '{' ) {
60
					$s .= ', ';
61
				}
62
				$s .= '"' . Xml::encodeJsVar( $name ) . '": ' .
63
					self::encodeJsVar( $elt );
64
			}
65
			$s .= '}';
66
		} else {
67
			$s = '"' . Xml::encodeJsVar( $value ) . '"';
68
		}
69
		return $s;
70
	}
71
72
	/**
73
	 * This function returns the definitions for the parameters used by every map feature.
74
	 *
75
	 * @return array
76
	 */
77
	public static function getCommonParameters() {
78
		$params = [];
79
80
		$params['mappingservice'] = [
81
			'type' => 'string',
82
			'aliases' => 'service',
83
			'default' => $GLOBALS['egMapsDefaultService'],
84
			'values' => self::getAllPossibleServiceValues(),
85
		];
86
87
		$params['width'] = [
88
			'type' => 'dimension',
89
			'allowauto' => true,
90
			'units' => [ 'px', 'ex', 'em', '%', '' ],
91
			'default' => $GLOBALS['egMapsMapWidth'],
92
		];
93
94
		$params['height'] = [
95
			'type' => 'dimension',
96
			'units' => [ 'px', 'ex', 'em', '' ],
97
			'default' => $GLOBALS['egMapsMapHeight'],
98
		];
99
100
		$params['centre'] = [
101
			'type' => 'string',
102
			'aliases' => [ 'center' ],
103
			'default' => false,
104
			'manipulatedefault' => false,
105
		];
106
107
		// Give grep a chance to find the usages:
108
		// maps-par-mappingservice, maps-par-geoservice, maps-par-width,
109
		// maps-par-height, maps-par-centre
110
		foreach ( $params as $name => &$data ) {
111
			$data['name'] = $name;
112
			$data['message'] = 'maps-par-' . $name;
113
		}
114
115
		return array_merge( $params, self::getEvenMawrCommonParameters() );
116
	}
117
118
	private static function getAllPossibleServiceValues(): array {
119
		$allServiceValues = [];
120
121
		foreach ( $GLOBALS['egMapsAvailableServices'] as $availableService ) {
122
			$allServiceValues = array_merge(
123
				$allServiceValues,
124
				[ $availableService ],
125
				MappingServices::getServiceInstance( $availableService )->getAliases()
126
			);
127
		}
128
129
		return $allServiceValues;
130
	}
131
132
	private static function getEvenMawrCommonParameters() {
133
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
134
135
		$params = [];
136
137
		$params['title'] = [
138
			'name' => 'title',
139
			'default' => $egMapsDefaultTitle,
140
		];
141
142
		$params['label'] = [
143
			'default' => $egMapsDefaultLabel,
144
			'aliases' => 'text',
145
		];
146
147
		$params['icon'] = [
148
			'default' => '',
149
		];
150
151
		$params['visitedicon'] = [
152
			'default' => '',
153
		];
154
155
		$params['lines'] = [
156
			'type' => 'mapsline',
157
			'default' => [],
158
			'delimiter' => ';',
159
			'islist' => true,
160
		];
161
162
		$params['polygons'] = [
163
			'type' => 'mapspolygon',
164
			'default' => [],
165
			'delimiter' => ';',
166
			'islist' => true,
167
		];
168
169
		$params['circles'] = [
170
			'type' => 'mapscircle',
171
			'default' => [],
172
			'delimiter' => ';',
173
			'islist' => true,
174
		];
175
176
		$params['rectangles'] = [
177
			'type' => 'mapsrectangle',
178
			'default' => [],
179
			'delimiter' => ';',
180
			'islist' => true,
181
		];
182
183
		$params['wmsoverlay'] = [
184
			'type' => 'wmsoverlay',
185
			'default' => false,
186
			'delimiter' => ' ',
187
		];
188
189
		$params['maxzoom'] = [
190
			'type' => 'integer',
191
			'default' => false,
192
			'manipulatedefault' => false,
193
			'dependencies' => 'minzoom',
194
		];
195
196
		$params['minzoom'] = [
197
			'type' => 'integer',
198
			'default' => false,
199
			'manipulatedefault' => false,
200
			'lowerbound' => 0,
201
		];
202
203
		$params['copycoords'] = [
204
			'type' => 'boolean',
205
			'default' => false,
206
		];
207
208
		$params['static'] = [
209
			'type' => 'boolean',
210
			'default' => false,
211
		];
212
213
		// Give grep a chance to find the usages:
214
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
215
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
216
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
217
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
218
		// maps-displaymap-par-static
219
		foreach ( $params as $name => &$param ) {
220
			if ( !array_key_exists( 'message', $param ) ) {
221
				$param['message'] = 'maps-displaymap-par-' . $name;
222
			}
223
		}
224
225
		return $params;
226
	}
227
228
	/**
229
	 * Resolves the url of images provided as wiki page; leaves others alone.
230
	 *
231
	 * @since 1.0
232
	 * @deprecated
233
	 *
234
	 * @param string $file
235
	 *
236
	 * @return string
237
	 */
238
	public static function getFileUrl( $file ): string {
239
		$colonPosition = strpos( $file, ':' );
240
241
		$titleWithoutPrefix = $colonPosition === false ? $file : substr( $file, $colonPosition + 1 );
242
243
		$title = Title::makeTitle( NS_FILE, trim( $titleWithoutPrefix ) );
244
245
		if ( $title !== null && $title->exists() ) {
246
			$imagePage = new ImagePage( $title );
247
			return $imagePage->getDisplayedFile()->getURL();
248
		}
249
250
		return trim( $file );
251
	}
252
253
	/**
254
	 * Returns JS to init the vars to hold the map data when they are not there already.
255
	 *
256
	 * @since 1.0
257
	 * @deprecated
258
	 *
259
	 * @param string $serviceName
260
	 *
261
	 * @return string
262
	 */
263
	public static function getBaseMapJSON( $serviceName ) {
264
		static $baseInit = false;
265
		static $serviceInit = [];
266
267
		$json = '';
268
269
		if ( !$baseInit ) {
270
			$baseInit = true;
271
			$json .= 'var mwmaps={};';
272
		}
273
274
		if ( !in_array( $serviceName, $serviceInit ) ) {
275
			$serviceInit[] = $serviceName;
276
			$json .= "mwmaps.$serviceName={};";
277
		}
278
279
		return $json;
280
	}
281
282
}
283