Completed
Push — master ( cc1fa7...46bd0e )
by Jeroen De
01:23
created

DisplayMapFunction::getHookDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 15
cp 1
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Maps\MediaWiki\ParserHooks;
4
5
use Maps;
6
use Maps\MapsFactory;
7
use Maps\MapsFunctions;
8
use Maps\MappingServices;
9
use Maps\Presentation\ParameterExtractor;
10
use MWException;
11
use ParamProcessor;
12
use ParamProcessor\ProcessedParam;
13
use Parser;
14
use ParserHooks\HookDefinition;
15
16
/**
17
 * Class for the 'display_map' parser hooks.
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class DisplayMapFunction {
23
24
	private $services;
25
26
	private $renderer;
27
28 21
	public function __construct( MappingServices $services ) {
29 21
		$this->services = $services;
30
31 21
		$this->renderer = new DisplayMapRenderer();
32 21
	}
33
34
	/**
35
	 * @param Parser $parser
36
	 * @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed.
37
	 * They are not normalized, so can be "key =  value "
38
	 *
39
	 * @return string
40
	 * @throws MWException
41
	 */
42 19
	public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string {
43 19
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
44
45 19
		$service = $this->services->getServiceOrDefault(
46 19
			$this->extractServiceName(
47 19
				Maps\Presentation\ParameterExtractor::extractFromKeyValueStrings( $parameters )
48
			)
49
		);
50
51 19
		$this->renderer->service = $service;
52
53 19
		$processor->setFunctionParams(
54 19
			$parameters,
55 19
			array_merge(
56 19
				self::getHookDefinition( ';' )->getParameters(),
57 19
				$service->getParameterInfo()
58
			),
59 19
			self::getHookDefinition( ';' )->getDefaultParameters()
60
		);
61
62 19
		return $this->getMapHtmlFromProcessor( $parser, $processor );
63
	}
64
65
	/**
66
	 * @param Parser $parser
67
	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys.
68
	 * Both keys and values have not been normalized.
69
	 *
70
	 * @return string
71
	 * @throws MWException
72
	 */
73 2
	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
74 2
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
75
76 2
		$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) );
77
78 2
		$this->renderer->service = $service;
79
80 2
		$processor->setParameters(
81 2
			$parameters,
82 2
			array_merge(
83 2
				self::getHookDefinition( "\n" )->getParameters(),
84 2
				$service->getParameterInfo()
85
			)
86
		);
87
88 2
		return $this->getMapHtmlFromProcessor( $parser, $processor );
89
	}
90
91 21
	private function getMapHtmlFromProcessor( Parser $parser, ParamProcessor\Processor $processor ) {
92 21
		$params = $processor->processParameters()->getParameters();
93
94 21
		$this->defaultMapZoom( $params );
95
96 21
		$this->trackMap( $parser );
97
98 21
		return $this->renderer->renderMap(
99 21
			$this->processedParametersToKeyValueArray( $params ),
100
			$parser
101
		);
102
	}
103
104 21
	private function extractServiceName( array $parameters ): string {
105 21
		$service = ( new ParameterExtractor() )->extract(
106 21
			[ 'mappingservice', 'service' ],
107
			$parameters
108
		);
109
110 21
		return $service ?? '';
111
	}
112
113 21
	private function processedParametersToKeyValueArray( array $params ): array {
114 21
		$parameters = [];
115
116 21
		foreach ( $params as $parameter ) {
117 21
			$parameters[$parameter->getName()] = $parameter->getValue();
118
		}
119
120 21
		return $parameters;
121
	}
122
123 21
	public static function getHookDefinition( string $locationDelimiter ): HookDefinition {
124 21
		$params = [];
125
126 21
		$params['mappingservice'] = [
127 21
			'type' => 'string',
128 21
			'aliases' => 'service',
129 21
			'default' => $GLOBALS['egMapsDefaultService'],
130 21
			'values' => MapsFactory::globalInstance()->getMappingServices()->getAllNames(),
131 21
			'message' => 'maps-par-mappingservice'
132
		];
133
134 21
		$params['coordinates'] = [
135 21
			'type' => 'string',
136
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
137
			'default' => [],
138
			'islist' => true,
139 21
			'delimiter' => $locationDelimiter,
140 21
			'message' => 'maps-displaymap-par-coordinates',
141
		];
142
143 21
		return new HookDefinition(
144 21
			[ 'display_map', 'display_point', 'display_points', 'display_line' ],
145
			$params,
0 ignored issues
show
Documentation introduced by
$params is of type array<string,array<strin...ssage\":\"string\"}>"}>, but the function expects a array<integer,object<Par...essor\ParamDefinition>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146 21
			[ 'coordinates' ]
147
		);
148
	}
149
150
	/**
151
	 * @param ProcessedParam[] $parameters
152
	 */
153 21
	private function defaultMapZoom( array &$parameters ) {
154 21
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
155 20
				$parameters['coordinates']->getValue()
156 21
			) > 1 ) {
157 2
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
158
		}
159 21
	}
160
161 2
	private function getParameterWithValue( ProcessedParam $param, $value ) {
162 2
		return new ProcessedParam(
163 2
			$param->getName(),
164
			$value,
165 2
			$param->wasSetToDefault(),
166 2
			$param->getOriginalName(),
167 2
			$param->getOriginalValue()
168
		);
169
	}
170
171 21
	private function trackMap( Parser $parser ) {
172 21
		if ( $GLOBALS['egMapsEnableCategory'] ) {
173
			$parser->addTrackingCategory( 'maps-tracking-category' );
174
		}
175 21
	}
176
177
}
178