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

MapsMapper::getEvenMawrCommonParameters()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 95
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 0
cts 59
cp 0
rs 8.4117
c 0
b 0
f 0
cc 3
eloc 57
nc 3
nop 0
crap 12

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
/**
4
 * A class that holds static helper functions for generic mapping-related functions.
5
 * 
6
 * @since 0.1
7
 * 
8
 * @deprecated
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
final class MapsMapper {
14
	
15
	/**
16
	 * Encode a variable of unknown type to JavaScript.
17
	 * Arrays are converted to JS arrays, objects are converted to JS associative
18
	 * arrays (objects). So cast your PHP associative arrays to objects before
19
	 * passing them to here.
20
	 *
21
	 * This is a copy of
22
	 * @see Xml::encodeJsVar
23
	 * which fixes incorrect behaviour with floats.
24
	 *
25
	 * @since 0.7.1
26
	 *
27
	 * @param mixed $value
28
	 *
29
	 * @return string
30
	 */
31
	public static function encodeJsVar( $value ) {
32
		if ( is_bool( $value ) ) {
33
			$s = $value ? 'true' : 'false';
34
		} elseif ( is_null( $value ) ) {
35
			$s = 'null';
36
		} elseif ( is_int( $value ) || is_float( $value ) ) {
37
			$s = $value;
38
		} elseif ( is_array( $value ) && // Make sure it's not associative.
39
					array_keys($value) === range( 0, count($value) - 1 ) ||
40
					count($value) == 0
41
				) {
42
			$s = '[';
43
			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...
44
				if ( $s != '[' ) {
45
					$s .= ', ';
46
				}
47
				$s .= self::encodeJsVar( $elt );
48
			}
49
			$s .= ']';
50
		} elseif ( is_object( $value ) || is_array( $value ) ) {
51
			// Objects and associative arrays
52
			$s = '{';
53
			foreach ( (array)$value as $name => $elt ) {
54
				if ( $s != '{' ) {
55
					$s .= ', ';
56
				}
57
				$s .= '"' . Xml::encodeJsVar( $name ) . '": ' .
58
					self::encodeJsVar( $elt );
59
			}
60
			$s .= '}';
61
		} else {
62
			$s = '"' . Xml::encodeJsVar( $value ) . '"';
63
		}
64
		return $s;
65
	}
66
67
	/**
68
	 * This function returns the definitions for the parameters used by every map feature.
69
	 *
70
	 * @return array
71
	 */
72
	public static function getCommonParameters() {
73
		global $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsMapWidth, $egMapsMapHeight, $egMapsDefaultService;
74
75
		$params = [];
76
77
		$params['mappingservice'] = [
78
			'type' => 'mappingservice',
79
			'aliases' => 'service',
80
			'default' => $egMapsDefaultService,
81
		];
82
83
		$params['geoservice'] = [
84
			'default' => $egMapsDefaultGeoService,
85
			'values' => $egMapsAvailableGeoServices,
86
			'dependencies' => 'mappingservice',
87
			// TODO 'manipulations' => new MapsParamGeoService( 'mappingservice' ),
88
		];
89
90
		$params['width'] = [
91
			'type' => 'dimension',
92
			'allowauto' => true,
93
			'units' => [ 'px', 'ex', 'em', '%', '' ],
94
			'default' => $egMapsMapWidth,
95
		];
96
97
		$params['height'] = [
98
			'type' => 'dimension',
99
			'units' => [ 'px', 'ex', 'em', '' ],
100
			'default' => $egMapsMapHeight,
101
		];
102
103
		// TODO$manipulation = new MapsParamLocation();
104
		// TODO$manipulation->toJSONObj = true;
105
106
		$params['centre'] = [
107
			'type' => 'mapslocation',
108
			'aliases' => [ 'center' ],
109
			'default' => false,
110
			'manipulatedefault' => false,
111
		];
112
113
		// Give grep a chance to find the usages:
114
		// maps-par-mappingservice, maps-par-geoservice, maps-par-width,
115
		// maps-par-height, maps-par-centre
116
		foreach ( $params as $name => &$data ) {
117
			$data['name'] = $name;
118
			$data['message'] = 'maps-par-' . $name;
119
		}
120
121
		return array_merge( $params, self::getEvenMawrCommonParameters() );
122
	}
123
124
	private static function getEvenMawrCommonParameters() {
125
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
126
127
		$params = [];
128
129
		$params['title'] = [
130
			'name' => 'title',
131
			'default' => $egMapsDefaultTitle,
132
		];
133
134
		$params['label'] = [
135
			'default' => $egMapsDefaultLabel,
136
			'aliases' => 'text',
137
		];
138
139
		$params['icon'] = [
140
			'default' => '', // TODO: image param
141
		];
142
143
		$params['visitedicon'] = [
144
			'default' => '', //TODO: image param
145
		];
146
147
		$params['lines'] = [
148
			'type' => 'mapsline',
149
			'default' => [],
150
			'delimiter' => ';',
151
			'islist' => true,
152
		];
153
154
		$params['polygons'] = [
155
			'type' => 'mapspolygon',
156
			'default' => [],
157
			'delimiter' => ';',
158
			'islist' => true,
159
		];
160
161
		$params['circles'] = [
162
			'type' => 'mapscircle',
163
			'default' => [],
164
			'delimiter' => ';',
165
			'islist' => true,
166
		];
167
168
		$params['rectangles'] = [
169
			'type' => 'mapsrectangle',
170
			'default' => [],
171
			'delimiter' => ';',
172
			'islist' => true,
173
		];
174
175
		$params['wmsoverlay'] = [
176
			'type' => 'wmsoverlay',
177
			'default' => false,
178
			'delimiter' => ' ',
179
		];
180
181
		$params['maxzoom'] = [
182
			'type' => 'integer',
183
			'default' => false,
184
			'manipulatedefault' => false,
185
			'dependencies' => 'minzoom',
186
		];
187
188
		$params['minzoom'] = [
189
			'type' => 'integer',
190
			'default' => false,
191
			'manipulatedefault' => false,
192
			'lowerbound' => 0,
193
		];
194
195
		$params['copycoords'] = [
196
			'type' => 'boolean',
197
			'default' => false,
198
		];
199
200
		$params['static'] = [
201
			'type' => 'boolean',
202
			'default' => false,
203
		];
204
205
		// Give grep a chance to find the usages:
206
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
207
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
208
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
209
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
210
		// maps-displaymap-par-static
211
		foreach ( $params as $name => &$param ) {
212
			if ( !array_key_exists( 'message', $param ) ) {
213
				$param['message'] = 'maps-displaymap-par-' . $name;
214
			}
215
		}
216
217
		return $params;
218
	}
219
	
220
	/**
221
	 * Resolves the url of images provided as wiki page; leaves others alone.
222
	 * 
223
	 * @since 1.0
224
	 * @deprecated
225
	 * 
226
	 * @param string $file
227
	 * 
228
	 * @return string
229
	 */
230 5
	public static function getFileUrl( $file ) {
231 5
		$title = Title::makeTitle( NS_FILE, $file );
232
233 5
		if( $title !==  null && $title->exists() ) {
234
			$imagePage = new ImagePage( $title );
235
			return $imagePage->getDisplayedFile()->getURL();
236
		}
237 5
		return $file;
238
	}
239
240
	/**
241
	 * Returns JS to init the vars to hold the map data when they are not there already.
242
	 * 
243
	 * @since 1.0
244
	 * @deprecated
245
	 * 
246
	 * @param string $serviceName
247
	 *
248
	 * @return string
249
	 */
250
	public static function getBaseMapJSON( $serviceName ) {
251
		static $baseInit = false;
252
		static $serviceInit = [];
253
254
		$json = '';
255
		
256
		if ( !$baseInit ) {
257
			$baseInit = true;
258
			$json .= 'var mwmaps={};';
259
		}
260
		
261
		if ( !in_array( $serviceName, $serviceInit ) ) {
262
			$serviceInit[] = $serviceName;
263
			$json .= "mwmaps.$serviceName={};";
264
		}
265
		
266
		return $json;
267
	}
268
	
269
}
270