Completed
Push — master ( 264305...76f9d5 )
by Jeroen De
03:12 queued 03:06
created

MapsDisplayMap::trackMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2.2559
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 [ $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'] = [
50 5
			'type' => 'string',
51 5
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
52 5
			'default' => [],
53 5
			'islist' => true,
54 5
			'delimiter' => $type === ParserHook::TYPE_FUNCTION ? ';' : "\n",
55 5
			'message' => 'maps-displaymap-par-coordinates',
56
		];
57
58 5
		return 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...
59
	}
60
61
	/**
62
	 * Temporary hack. Do not rely upon.
63
	 * @since 3.0
64
	 * @deprecated
65
	 * @return array
66
	 */
67 5
	public static function getCommonMapParams() {
68 5
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
69
70 5
		$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...
71 5
			'name' => 'title',
72 5
			'default' => $egMapsDefaultTitle,
73
		];
74
75 5
		$params['label'] = [
76 5
			'default' => $egMapsDefaultLabel,
77 5
			'aliases' => 'text',
78
		];
79
80 5
		$params['icon'] = [
81 5
			'default' => '', // TODO: image param
82
		];
83
84 5
		$params['visitedicon'] = [
85 5
			'default' => '', //TODO: image param
86
		];
87
88 5
		$params['lines'] = [
89 5
			'type' => 'mapsline',
90 5
			'default' => [],
91 5
			'delimiter' => ';',
92 5
			'islist' => true,
93
		];
94
95 5
		$params['polygons'] = [
96 5
			'type' => 'mapspolygon',
97 5
			'default' => [],
98 5
			'delimiter' => ';',
99 5
			'islist' => true,
100
		];
101
102 5
		$params['circles'] = [
103 5
			'type' => 'mapscircle',
104 5
			'default' => [],
105 5
			'delimiter' => ';',
106 5
			'islist' => true,
107
		];
108
109 5
		$params['rectangles'] = [
110 5
			'type' => 'mapsrectangle',
111 5
			'default' => [],
112 5
			'delimiter' => ';',
113 5
			'islist' => true,
114
		];
115
116 5
		$params['wmsoverlay'] = [
117 5
			'type' => 'wmsoverlay',
118 5
			'default' => false,
119 5
			'delimiter' => ' ',
120
		];
121
122 5
		$params['maxzoom'] = [
123 5
			'type' => 'integer',
124 5
			'default' => false,
125 5
			'manipulatedefault' => false,
126 5
			'dependencies' => 'minzoom',
127
		];
128
129 5
		$params['minzoom'] = [
130 5
			'type' => 'integer',
131 5
			'default' => false,
132 5
			'manipulatedefault' => false,
133 5
			'lowerbound' => 0,
134
		];
135
136 5
		$params['copycoords'] = [
137 5
			'type' => 'boolean',
138 5
			'default' => false,
139
		];
140
141 5
		$params['static'] = [
142 5
			'type' => 'boolean',
143 5
			'default' => false,
144
		];
145
146
		// Give grep a chance to find the usages:
147
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
148
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
149
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
150
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
151
		// maps-displaymap-par-static
152 5
		foreach ( $params as $name => &$param ) {
153 5
			if ( !array_key_exists( 'message', $param ) ) {
154 5
				$param['message'] = 'maps-displaymap-par-' . $name;
155 5
			}
156 5
		}
157
158 5
		return $params;
159
	}
160
	
161
	/**
162
	 * Returns the list of default parameters.
163
	 * @see ParserHook::getDefaultParameters
164
	 * 
165
	 * @since 0.7
166
	 * 
167
	 * @return array
168
	 */
169 4
	protected function getDefaultParameters( $type ) {
170 4
		return [ 'coordinates' ];
171
	}
172
	
173
	/**
174
	 * Renders and returns the output.
175
	 * @see ParserHook::render
176
	 * 
177
	 * @since 0.7
178
	 * 
179
	 * @param array $parameters
180
	 * 
181
	 * @return string
182
	 */
183 4
	public function render( array $parameters ) {
184 4
		$this->defaultMapZoom( $parameters );
185 4
		$this->trackMap();
186
187 4
		$renderer = new MapsDisplayMapRenderer( MapsMappingServices::getServiceInstance( $parameters['mappingservice'] ) );
0 ignored issues
show
Compatibility introduced by
\MapsMappingServices::ge...ters['mappingservice']) of type object<iMappingService> is not a sub-type of object<MapsMappingService>. It seems like you assume a concrete implementation of the interface iMappingService to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
188
189 4
		return $renderer->renderMap( $parameters, $this->parser );
190
	}
191
192 4
	private function defaultMapZoom( &$parameters ) {
193 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...
194
195 4
		if ( array_key_exists( 'zoom', $fullParams ) && $fullParams['zoom']->wasSetToDefault() && count( $parameters['coordinates'] ) > 1 ) {
196
			$parameters['zoom'] = false;
197
		}
198 4
	}
199
200 4
	private function trackMap() {
0 ignored issues
show
Coding Style introduced by
trackMap uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
201 4
		if ( $GLOBALS['egMapsEnableCategory'] ) {
202
			$this->parser->addTrackingCategory( 'maps-tracking-category' );
203
		}
204 4
	}
205
	
206
	/**
207
	 * Returns the parser function options.
208
	 * @see ParserHook::getFunctionOptions
209
	 * 
210
	 * @since 0.7
211
	 * 
212
	 * @return array
213
	 */
214 4
	protected function getFunctionOptions() {
215
		return [
216 4
			'noparse' => true,
217
			'isHTML' => true
218 4
		];
219
	}
220
221
	/**
222
	 * @see ParserHook::getMessage()
223
	 * 
224
	 * @since 1.0
225
	 */
226
	public function getMessage() {
227
		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...
228
	}		
229
	
230
}
231