Completed
Push — master ( 88c9eb...e2ac37 )
by Jeroen De
03:05
created

MapsFunctions::getCommonParameters()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 128

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 38
cts 38
cp 1
rs 8
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4

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