Completed
Push — master ( a7a34c...1e3be1 )
by Jeroen De
03:21
created

MapsFunctions::getAllPossibleServiceValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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

This method has been deprecated.

Loading history...
212
			);
213
		}
214
215 20
		return $allServiceValues;
216
	}
217
218
	/**
219
	 * Resolves the url of images provided as wiki page; leaves others alone.
220
	 *
221
	 * @since 1.0
222
	 * @deprecated
223
	 *
224
	 * @param string $file
225
	 *
226
	 * @return string
227
	 */
228 4
	public static function getFileUrl( $file ): string {
229 4
		return MapsFactory::globalInstance()->getFileUrlFinder()->getUrlForFileName( $file );
230
	}
231
232
}
233