Completed
Pull Request — master (#130)
by None
07:05
created

MapsDisplayMap::getNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 4
	protected function getName() {
22 4
		return 'display_map';
23
	}
24
25
	/**
26
	 * @see ParserHook::getNames()
27
	 *
28
	 * @since 2.0
29
	 *
30
	 * @return array
31
	 */
32 4
	protected function getNames() {
33 4
		return array( $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 5
	protected function getParameterInfo( $type ) {
45 5
		$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 5
		$params['mappingservice']['feature'] = 'display_map';
48
49 5
		$params['coordinates'] = array(
50 5
			'type' => 'mapslocation',
51 5
			'aliases' => array( 'coords', 'location', 'address', 'addresses', 'locations', 'points' ),
52 5
			'dependencies' => array( 'mappingservice', 'geoservice' ),
53 5
			'default' => array(),
54 5
			'islist' => true,
55 5
			'delimiter' => $type === ParserHook::TYPE_FUNCTION ? ';' : "\n",
56 5
			'message' => 'maps-displaymap-par-coordinates',
57
		);
58
59 5
		$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 5
		return $params;
62
	}
63
64
	/**
65
	 * Temporary hack. Do not rely upon.
66
	 * @since 3.0
67
	 * @deprecated
68
	 * @return array
69
	 */
70 5
	public static function getCommonMapParams() {
71 5
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
72
73 5
		$params['title'] = array(
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 5
			'name' => 'title',
75 5
			'default' => $egMapsDefaultTitle,
76
		);
77
78 5
		$params['label'] = array(
79 5
			'default' => $egMapsDefaultLabel,
80 5
			'aliases' => 'text',
81
		);
82
83 5
		$params['icon'] = array(
84 5
			'default' => '', // TODO: image param
85
		);
86
87 5
		$params['visitedicon'] = array(
88 5
			'default' => '', //TODO: image param
89
		);
90
91 5
		$params['lines'] = array(
92 5
			'type' => 'mapsline',
93 5
			'default' => array(),
94 5
			'delimiter' => ';',
95 5
			'islist' => true,
96
		);
97
98 5
		$params['polygons'] = array(
99 5
			'type' => 'mapspolygon',
100 5
			'default' => array(),
101 5
			'delimiter' => ';',
102 5
			'islist' => true,
103
		);
104
105 5
		$params['circles'] = array(
106 5
			'type' => 'mapscircle',
107 5
			'default' => array(),
108 5
			'delimiter' => ';',
109 5
			'islist' => true,
110
		);
111
112 5
		$params['rectangles'] = array(
113 5
			'type' => 'mapsrectangle',
114 5
			'default' => array(),
115 5
			'delimiter' => ';',
116 5
			'islist' => true,
117
		);
118
119 5
		$params['wmsoverlay'] = array(
120 5
			'type' => 'wmsoverlay',
121 5
			'default' => false,
122 5
			'delimiter' => ' ',
123
		);
124
125 5
		$params['maxzoom'] = array(
126 5
			'type' => 'integer',
127 5
			'default' => false,
128 5
			'manipulatedefault' => false,
129 5
			'dependencies' => 'minzoom',
130
		);
131
132 5
		$params['minzoom'] = array(
133 5
			'type' => 'integer',
134 5
			'default' => false,
135 5
			'manipulatedefault' => false,
136 5
			'lowerbound' => 0,
137
		);
138
139 5
		$params['copycoords'] = array(
140 5
			'type' => 'boolean',
141 5
			'default' => false,
142
		);
143
144 5
		$params['static'] = array(
145 5
			'type' => 'boolean',
146 5
			'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 5
		foreach ( $params as $name => &$param ) {
156 5
			if ( !array_key_exists( 'message', $param ) ) {
157 5
				$param['message'] = 'maps-displaymap-par-' . $name;
158 5
			}
159 5
		}
160
161 5
		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 4
	protected function getDefaultParameters( $type ) {
173 4
		return array( '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 4
	public function render( array $parameters ) {
187
		// Get the instance of the service class.
188 4
		$service = MapsMappingServices::getServiceInstance( $parameters['mappingservice'], $this->getName() );
0 ignored issues
show
Unused Code introduced by
The call to MapsMappingServices::getServiceInstance() has too many arguments starting with $this->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
189
		
190 4
		$mapClass = new MapsDisplayMapRenderer( $service );
191
192 4
		$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 4
		if ( array_key_exists( 'zoom', $fullParams ) && $fullParams['zoom']->wasSetToDefault() && count( $parameters['coordinates'] ) > 1 ) {
195
			$parameters['zoom'] = false;
196
		}
197
198 4
		$this->parser->addTrackingCategory( 'maps-tracking-category' );
199 4
		return $mapClass->renderMap( $parameters, $this->parser );
200 4
	}
201 4
	
202 4
	/**
203
	 * Returns the parser function options.
204
	 * @see ParserHook::getFunctionOptions
205
	 * 
206
	 * @since 0.7
207
	 * 
208
	 * @return array
209
	 */
210
	protected function getFunctionOptions() {
211
		return array(
212
			'noparse' => true,
213 4
			'isHTML' => true
214
		);
215 4
	}
216
217 4
	/**
218
	 * @see ParserHook::getMessage()
219
	 * 
220
	 * @since 1.0
221
	 */
222
	public function getMessage() {
223
		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...
224
	}		
225
	
226
}
227