Completed
Push — newparam ( 72433c...c5fad3 )
by Jeroen De
01:21
created

MapsFunctions   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 56.67%

Importance

Changes 0
Metric Value
wmc 19
lcom 0
cbo 1
dl 0
loc 192
ccs 34
cts 60
cp 0.5667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C encodeJsVar() 0 35 15
B getCommonParameters() 0 116 3
A getFileUrl() 0 3 1
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 22
	public static function getCommonParameters() {
76 22
		$params = [];
77
78 22
		$params['width'] = [
79 22
			'type' => 'dimension',
80
			'allowauto' => true,
81
			'units' => [ 'px', 'ex', 'em', '%', '' ],
82 22
			'default' => $GLOBALS['egMapsMapWidth'],
83 22
			'message' => 'maps-par-width',
84
		];
85
86 22
		$params['height'] = [
87 22
			'type' => 'dimension',
88
			'units' => [ 'px', 'ex', 'em', '' ],
89 22
			'default' => $GLOBALS['egMapsMapHeight'],
90 22
			'message' => 'maps-par-height',
91
		];
92
93 22
		$params['centre'] = [
94
			'type' => 'string',
95
			'aliases' => [ 'center' ],
96
			'default' => false,
97
			'manipulatedefault' => false,
98
			'message' => 'maps-par-centre',
99
		];
100
101 22
		$params['title'] = [
102 22
			'name' => 'title',
103 22
			'default' => $GLOBALS['egMapsDefaultTitle'],
104
		];
105
106 22
		$params['label'] = [
107 22
			'default' => $GLOBALS['egMapsDefaultLabel'],
108 22
			'aliases' => 'text',
109
		];
110
111 22
		$params['icon'] = [
112
			'default' => '',
113
		];
114
115 22
		$params['visitedicon'] = [
116
			'default' => '',
117
		];
118
119 22
		$params['lines'] = [
120
			'type' => 'mapsline',
121
			'default' => [],
122
			'delimiter' => ';',
123
			'islist' => true,
124
		];
125
126 22
		$params['polygons'] = [
127
			'type' => 'mapspolygon',
128
			'default' => [],
129
			'delimiter' => ';',
130
			'islist' => true,
131
		];
132
133 22
		$params['circles'] = [
134
			'type' => 'mapscircle',
135
			'default' => [],
136
			'delimiter' => ';',
137
			'islist' => true,
138
		];
139
140 22
		$params['rectangles'] = [
141
			'type' => 'mapsrectangle',
142
			'default' => [],
143
			'delimiter' => ';',
144
			'islist' => true,
145
		];
146
147 22
		$params['wmsoverlay'] = [
148
			'type' => 'wmsoverlay',
149
			'default' => false,
150
			'delimiter' => ' ',
151
		];
152
153 22
		$params['maxzoom'] = [
154
			'type' => 'integer',
155
			'default' => false,
156
			'manipulatedefault' => false,
157
			'dependencies' => 'minzoom',
158
		];
159
160 22
		$params['minzoom'] = [
161
			'type' => 'integer',
162
			'default' => false,
163
			'manipulatedefault' => false,
164
			'lowerbound' => 0,
165
		];
166
167 22
		$params['copycoords'] = [
168
			'type' => 'boolean',
169
			'default' => false,
170
		];
171
172 22
		$params['static'] = [
173
			'type' => 'boolean',
174
			'default' => false,
175
		];
176
177
		// Give grep a chance to find the usages:
178
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
179
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
180
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
181
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
182
		// maps-displaymap-par-static
183 22
		foreach ( $params as $name => &$param ) {
184 22
			if ( !array_key_exists( 'message', $param ) ) {
185 22
				$param['message'] = 'maps-displaymap-par-' . $name;
186
			}
187
		}
188
189 22
		return $params;
190
	}
191
192
	/**
193
	 * Resolves the url of images provided as wiki page; leaves others alone.
194
	 *
195
	 * @since 1.0
196
	 * @deprecated
197
	 *
198
	 * @param string $file
199
	 *
200
	 * @return string
201
	 */
202 4
	public static function getFileUrl( $file ): string {
203 4
		return MapsFactory::globalInstance()->getFileUrlFinder()->getUrlForFileName( $file );
204
	}
205
206
}
207