MapsDocFunction::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps\ParserHooks;
6
7
use Maps\MapsFactory;
8
use ParamProcessor\ParamDefinition;
9
use ParserHook;
10
11
/**
12
 * Class for the 'mapsdoc' parser hooks,
13
 * which displays documentation for a specified mapping service.
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class MapsDocFunction 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...
19
20
	/**
21
	 * Field to store the value of the language parameter.
22
	 *
23
	 * @var string
24
	 */
25
	protected $language;
26
27
	/**
28
	 * Renders and returns the output.
29
	 *
30
	 * @see ParserHook::render
31
	 *
32
	 * @param array $parameters
33
	 *
34
	 * @return string
35
	 */
36
	public function render( array $parameters ) {
37
		$this->language = $parameters['language'];
38
39
		$factory = MapsFactory::globalInstance();
40
41
		$params = $this->getServiceParameters( $factory, $parameters['service'] );
42
43
		return $this->getParameterTable( $factory, $params );
44
	}
45
46
	private function getServiceParameters( MapsFactory $factory, string $service ) {
47
		return array_merge(
48
			[
49
				'zoom' => [
50
					'type' => 'integer',
51
					'message' => 'maps-par-zoom',
52
				]
53
			],
54
			$factory->getMappingServices()->getService( $service )->getParameterInfo()
55
		);
56
	}
57
58
	/**
59
	 * Returns the wikitext for a table listing the provided parameters.
60
	 */
61
	private function getParameterTable( MapsFactory $factory, array $parameters ): string {
62
		$tableRows = [];
63
64
		$parameters = $factory->getParamDefinitionFactory()->newDefinitionsFromArrays( $parameters );
65
66
		foreach ( $parameters as $parameter ) {
67
			$tableRows[] = $this->getDescriptionRow( $parameter );
68
		}
69
70
		$table = '';
71
72
		if ( count( $tableRows ) > 0 ) {
73
			$tableRows = array_merge(
74
				[
75
					'!' . $this->msg( 'validator-describe-header-parameter' ) . "\n" .
76
					//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
77
					'!' . $this->msg( 'validator-describe-header-type' ) . "\n" .
78
					'!' . $this->msg( 'validator-describe-header-default' ) . "\n" .
79
					'!' . $this->msg( 'validator-describe-header-description' )
80
				],
81
				$tableRows
82
			);
83
84
			$table = implode( "\n|-\n", $tableRows );
85
86
			$table =
87
				'{| class="wikitable sortable"' . "\n" .
88
				$table .
89
				"\n|}";
90
		}
91
92
		return $table;
93
	}
94
95
	/**
96
	 * Returns the wikitext for a table row describing a single parameter.
97
	 *
98
	 * @param ParamDefinition $parameter
99
	 *
100
	 * @return string
101
	 */
102
	private function getDescriptionRow( ParamDefinition $parameter ) {
103
		$description = $this->msg( $parameter->getMessage() );
104
105
		$type = $this->msg( $parameter->getTypeMessage() );
106
107
		$default = $parameter->isRequired() ? "''" . $this->msg(
108
				'validator-describe-required'
109
			) . "''" : $parameter->getDefault();
110
		if ( is_array( $default ) ) {
111
			$default = implode( ', ', $default );
112
		} elseif ( is_bool( $default ) ) {
113
			$default = $default ? 'yes' : 'no';
114
		}
115
116
		if ( $default === '' ) {
117
			$default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
118
		}
119
120
		return <<<EOT
121
| {$parameter->getName()}
122
| {$type}
123
| {$default}
124
| {$description}
125
EOT;
126
	}
127
128
	/**
129
	 * Message function that takes into account the language parameter.
130
	 *
131
	 * @param string $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
132
	 * @param ... $args
0 ignored issues
show
Documentation introduced by
The doc-type ... could not be parsed: Unknown type name "..." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $args. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
133
	 *
134
	 * @return string
135
	 */
136
	private function msg() {
137
		$args = func_get_args();
138
		$key = array_shift( $args );
139
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
140
	}
141
142
	/**
143
	 * @see ParserHook::getDescription()
144
	 */
145
	public function getMessage() {
146
		return 'maps-mapsdoc-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-mapsdoc-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...
147
	}
148
149
	/**
150
	 * Gets the name of the parser hook.
151
	 *
152
	 * @see ParserHook::getName
153
	 *
154
	 * @return string
155
	 */
156 2
	protected function getName() {
157 2
		return 'mapsdoc';
158
	}
159
160
	/**
161
	 * Returns an array containing the parameter info.
162
	 *
163
	 * @see ParserHook::getParameterInfo
164
	 *
165
	 * @return array
166
	 */
167 3
	protected function getParameterInfo( $type ) {
168 3
		$params = [];
169
170 3
		$params['service'] = [
171 3
			'values' => $GLOBALS['egMapsAvailableServices'],
172
			'tolower' => true,
173
		];
174
175 3
		$params['language'] = [
176 3
			'default' => $GLOBALS['wgLanguageCode'],
177
		];
178
179
		// Give grep a chance to find the usages:
180
		// maps-geocode-par-service, maps-geocode-par-language
181 3
		foreach ( $params as $name => &$param ) {
182 3
			$param['message'] = 'maps-geocode-par-' . $name;
183
		}
184
185 3
		return $params;
186
	}
187
188
	/**
189
	 * Returns the list of default parameters.
190
	 *
191
	 * @see ParserHook::getDefaultParameters
192
	 *
193
	 * @return array
194
	 */
195 1
	protected function getDefaultParameters( $type ) {
196 1
		return [ 'service', 'language' ];
197
	}
198
199
}
200