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

DisplayMapFunction::getParameterDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
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\MappingService;
7
use Maps\MapsFactory;
8
use Maps\MappingServices;
9
use Maps\Presentation\ParameterExtractor;
10
use MWException;
11
use ParamProcessor\ProcessedParam;
12
use ParamProcessor\Processor;
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 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
			[],
56 19
			self::getHookDefinition( ';' )->getDefaultParameters()
57
		);
58
59 19
		$processor->setParameterDefinitions(
60 19
			$this->getAllParameterDefinitions( $service, ';' )
61
		);
62
63 19
		return $this->getMapHtmlFromProcessor( $parser, $processor );
64
	}
65
66
	/**
67
	 * @param Parser $parser
68
	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys.
69
	 * Both keys and values have not been normalized.
70
	 *
71
	 * @return string
72
	 * @throws MWException
73
	 */
74 2
	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
75 2
		$processor = new Processor( new \ParamProcessor\Options() );
76
77 2
		$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) );
78
79 2
		$this->renderer->service = $service;
80
81 2
		$processor->setParameters( $parameters );
82 2
		$processor->setParameterDefinitions(
83 2
			$this->getAllParameterDefinitions( $service, "\n" )
84
		);
85
86 2
		return $this->getMapHtmlFromProcessor( $parser, $processor );
87
	}
88
89 21
	private function getAllParameterDefinitions( MappingService $service, string $delimiter ) {
90 21
		$definitionArrays = array_merge(
91 21
			self::getHookDefinition( $delimiter )->getParameters(),
92 21
			$service->getParameterInfo()
93
		);
94
95 21
		$factory = MapsFactory::globalInstance()->getParamDefinitionFactory();
96 21
		$definitions = [];
97
98 21
		foreach ( $definitionArrays as $name => $definitionArray ) {
99 21
			if ( !array_key_exists( 'name', $definitionArray ) && is_string( $name ) ) {
100 21
				$definitionArray['name'] = $name;
101
			}
102
103 21
			$definitions[$name] = $factory->newDefinitionFromArray( $definitionArray );
104
		}
105
106 21
		return $definitions;
107
	}
108
109 21
	private function getMapHtmlFromProcessor( Parser $parser, Processor $processor ) {
110 21
		$params = $processor->processParameters()->getParameters();
111
112 21
		$this->defaultMapZoom( $params );
113
114 21
		$this->trackMap( $parser );
115
116 21
		return $this->renderer->renderMap(
117 21
			$this->processedParametersToKeyValueArray( $params ),
118
			$parser
119
		);
120
	}
121
122 21
	private function extractServiceName( array $parameters ): string {
123 21
		$service = ( new ParameterExtractor() )->extract(
124 21
			[ 'mappingservice', 'service' ],
125
			$parameters
126
		);
127
128 21
		return $service ?? '';
129
	}
130
131 21
	private function processedParametersToKeyValueArray( array $params ): array {
132 21
		$parameters = [];
133
134 21
		foreach ( $params as $parameter ) {
135 21
			$parameters[$parameter->getName()] = $parameter->getValue();
136
		}
137
138 21
		return $parameters;
139
	}
140
141 21
	public static function getHookDefinition( string $locationDelimiter ): HookDefinition {
142 21
		$params = [];
143
144 21
		$params['mappingservice'] = [
145 21
			'type' => 'string',
146 21
			'aliases' => 'service',
147 21
			'default' => $GLOBALS['egMapsDefaultService'],
148 21
			'values' => MapsFactory::globalInstance()->getMappingServices()->getAllNames(),
149 21
			'message' => 'maps-par-mappingservice'
150
		];
151
152 21
		$params['coordinates'] = [
153 21
			'type' => 'string',
154
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
155
			'default' => [],
156
			'islist' => true,
157 21
			'delimiter' => $locationDelimiter,
158 21
			'message' => 'maps-displaymap-par-coordinates',
159
		];
160
161 21
		return new HookDefinition(
162 21
			[ 'display_map', 'display_point', 'display_points', 'display_line' ],
163
			$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...
164 21
			[ 'coordinates' ]
165
		);
166
	}
167
168
	/**
169
	 * @param ProcessedParam[] $parameters
170
	 */
171 21
	private function defaultMapZoom( array &$parameters ) {
172 21
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
173 20
				$parameters['coordinates']->getValue()
174 21
			) > 1 ) {
175 2
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
176
		}
177 21
	}
178
179 2
	private function getParameterWithValue( ProcessedParam $param, $value ) {
180 2
		return new ProcessedParam(
181 2
			$param->getName(),
182
			$value,
183 2
			$param->wasSetToDefault(),
184 2
			$param->getOriginalName(),
185 2
			$param->getOriginalValue()
186
		);
187
	}
188
189 21
	private function trackMap( Parser $parser ) {
190 21
		if ( $GLOBALS['egMapsEnableCategory'] ) {
191
			$parser->addTrackingCategory( 'maps-tracking-category' );
192
		}
193 21
	}
194
195
}
196