Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

MapsDisplayMap::getFunctionOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
1
<?php
2
3
/**
4
 * Class for the 'display_map' parser hooks.
5
 * 
6
 * @since 0.7
7
 * 
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 */
11
class MapsDisplayMap extends ParserHook {
0 ignored issues
show
Deprecated Code introduced by
The class ParserHook has been deprecated with message: since 1.0 in favour of the ParserHooks library

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
12
13
	/**
14
	 * Gets the name of the parser hook.
15
	 * @see ParserHook::getName
16
	 * 
17
	 * @since 0.7
18
	 * 
19
	 * @return string
20
	 */
21
	protected function getName() {
22
		return 'display_map';
23
	}
24
25
	/**
26
	 * @see ParserHook::getNames()
27
	 *
28
	 * @since 2.0
29
	 *
30
	 * @return array
31
	 */
32
	protected function getNames() {
33
		return [ $this->getName(), 'display_point', 'display_points', 'display_line' ];
34
	}
35
	
36
	/**
37
	 * Returns an array containing the parameter info.
38
	 * @see ParserHook::getParameterInfo
39
	 *
40
	 * @since 0.7
41
	 * 
42
	 * @return array
43
	 */
44
	protected function getParameterInfo( $type ) {
45
		$params = MapsMapper::getCommonParameters();
0 ignored issues
show
Deprecated Code introduced by
The method MapsMapper::getCommonParameters() has been deprecated.

This method has been deprecated.

Loading history...
46
47
		$params['mappingservice']['feature'] = 'display_map';
48
49
		$params['coordinates'] = [
50
			'type' => 'mapslocation',
51
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
52
			'dependencies' => [ 'mappingservice', 'geoservice' ],
53
			'default' => [],
54
			'islist' => true,
55
			'delimiter' => $type === ParserHook::TYPE_FUNCTION ? ';' : "\n",
56
			'message' => 'maps-displaymap-par-coordinates',
57
		];
58
59
		$params = array_merge( $params, self::getCommonMapParams() );
0 ignored issues
show
Deprecated Code introduced by
The method MapsDisplayMap::getCommonMapParams() has been deprecated.

This method has been deprecated.

Loading history...
60
		
61
		return $params;
62
	}
63
64
	/**
65
	 * Temporary hack. Do not rely upon.
66
	 * @since 3.0
67
	 * @deprecated
68
	 * @return array
69
	 */
70
	public static function getCommonMapParams() {
71
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
72
73
		$params['title'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
			'name' => 'title',
75
			'default' => $egMapsDefaultTitle,
76
		];
77
78
		$params['label'] = [
79
			'default' => $egMapsDefaultLabel,
80
			'aliases' => 'text',
81
		];
82
83
		$params['icon'] = [
84
			'default' => '', // TODO: image param
85
		];
86
87
		$params['visitedicon'] = [
88
			'default' => '', //TODO: image param
89
		];
90
91
		$params['lines'] = [
92
			'type' => 'mapsline',
93
			'default' => [],
94
			'delimiter' => ';',
95
			'islist' => true,
96
		];
97
98
		$params['polygons'] = [
99
			'type' => 'mapspolygon',
100
			'default' => [],
101
			'delimiter' => ';',
102
			'islist' => true,
103
		];
104
105
		$params['circles'] = [
106
			'type' => 'mapscircle',
107
			'default' => [],
108
			'delimiter' => ';',
109
			'islist' => true,
110
		];
111
112
		$params['rectangles'] = [
113
			'type' => 'mapsrectangle',
114
			'default' => [],
115
			'delimiter' => ';',
116
			'islist' => true,
117
		];
118
119
		$params['wmsoverlay'] = [
120
			'type' => 'wmsoverlay',
121
			'default' => false,
122
			'delimiter' => ' ',
123
		];
124
125
		$params['maxzoom'] = [
126
			'type' => 'integer',
127
			'default' => false,
128
			'manipulatedefault' => false,
129
			'dependencies' => 'minzoom',
130
		];
131
132
		$params['minzoom'] = [
133
			'type' => 'integer',
134
			'default' => false,
135
			'manipulatedefault' => false,
136
			'lowerbound' => 0,
137
		];
138
139
		$params['copycoords'] = [
140
			'type' => 'boolean',
141
			'default' => false,
142
		];
143
144
		$params['static'] = [
145
			'type' => 'boolean',
146
			'default' => false,
147
		];
148
149
		// Give grep a chance to find the usages:
150
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
151
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
152
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
153
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
154
		// maps-displaymap-par-static
155
		foreach ( $params as $name => &$param ) {
156
			if ( !array_key_exists( 'message', $param ) ) {
157
				$param['message'] = 'maps-displaymap-par-' . $name;
158
			}
159
		}
160
161
		return $params;
162
	}
163
	
164
	/**
165
	 * Returns the list of default parameters.
166
	 * @see ParserHook::getDefaultParameters
167
	 * 
168
	 * @since 0.7
169
	 * 
170
	 * @return array
171
	 */
172
	protected function getDefaultParameters( $type ) {
173
		return [ 'coordinates' ];
174
	}
175
	
176
	/**
177
	 * Renders and returns the output.
178
	 * @see ParserHook::render
179
	 * 
180
	 * @since 0.7
181
	 * 
182
	 * @param array $parameters
183
	 * 
184
	 * @return string
185
	 */
186
	public function render( array $parameters ) {
187
		// Get the instance of the service class.
188
		$service = MapsMappingServices::getServiceInstance( $parameters['mappingservice'] );
189
		
190
		$mapClass = new MapsDisplayMapRenderer( $service );
191
192
		$fullParams = $this->validator->getParameters();
0 ignored issues
show
Deprecated Code introduced by
The method ParamProcessor\Processor::getParameters() has been deprecated with message: since 1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
193
194
		if ( array_key_exists( 'zoom', $fullParams ) && $fullParams['zoom']->wasSetToDefault() && count( $parameters['coordinates'] ) > 1 ) {
195
			$parameters['zoom'] = false;
196
		}
197
198
		global $egMapsEnableCategory;
199
		if ($egMapsEnableCategory) {
200
			$this->parser->addTrackingCategory( 'maps-tracking-category' );
201
		}
202
		return $mapClass->renderMap( $parameters, $this->parser );
203
	}
204
	
205
	/**
206
	 * Returns the parser function options.
207
	 * @see ParserHook::getFunctionOptions
208
	 * 
209
	 * @since 0.7
210
	 * 
211
	 * @return array
212
	 */
213
	protected function getFunctionOptions() {
214
		return [
215
			'noparse' => true,
216
			'isHTML' => true
217
		];
218
	}
219
220
	/**
221
	 * @see ParserHook::getMessage()
222
	 * 
223
	 * @since 1.0
224
	 */
225
	public function getMessage() {
226
		return 'maps-displaymap-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-displaymap-description'; (string) is incompatible with the return type of the parent method ParserHook::getMessage of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
227
	}		
228
	
229
}
230