Completed
Push — newparam ( 8a99e5...659dc5 )
by Jeroen De
01:32
created

MapsDocFunction::render()   A

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
namespace Maps\MediaWiki\ParserHooks;
4
5
use Maps\MapsFactory;
6
use ParamProcessor\ParamDefinition;
7
use ParserHook;
8
9
/**
10
 * Class for the 'mapsdoc' parser hooks,
11
 * which displays documentation for a specified mapping service.
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
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...
17
18
	/**
19
	 * Field to store the value of the language parameter.
20
	 *
21
	 * @var string
22
	 */
23
	protected $language;
24
25
	/**
26
	 * Renders and returns the output.
27
	 *
28
	 * @see ParserHook::render
29
	 *
30
	 * @param array $parameters
31
	 *
32
	 * @return string
33
	 */
34
	public function render( array $parameters ) {
35
		$this->language = $parameters['language'];
36
37
		$factory = MapsFactory::globalInstance();
38
39
		$params = $this->getServiceParameters( $factory, $parameters['service'] );
40
41
		return $this->getParameterTable( $factory, $params );
42
	}
43
44
	private function getServiceParameters( MapsFactory $factory, string $service ) {
45
		return array_merge(
46
			[
47
				'zoom' => [
48
					'type' => 'integer',
49
					'message' => 'maps-par-zoom',
50
				]
51
			],
52
			$factory->getMappingServices()->getService( $service )->getParameterInfo()
53
		);
54
	}
55
56
	/**
57
	 * Returns the wikitext for a table listing the provided parameters.
58
	 */
59
	private function getParameterTable( MapsFactory $factory, array $parameters ): string {
60
		$tableRows = [];
61
62
		$parameters = $factory->getParamDefinitionFactory()->newDefinitionsFromArrays( $parameters );
63
64
		foreach ( $parameters as $parameter ) {
65
			$tableRows[] = $this->getDescriptionRow( $parameter );
66
		}
67
68
		$table = '';
69
70
		if ( count( $tableRows ) > 0 ) {
71
			$tableRows = array_merge(
72
				[
73
					'!' . $this->msg( 'validator-describe-header-parameter' ) . "\n" .
74
					//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
75
					'!' . $this->msg( 'validator-describe-header-type' ) . "\n" .
76
					'!' . $this->msg( 'validator-describe-header-default' ) . "\n" .
77
					'!' . $this->msg( 'validator-describe-header-description' )
78
				],
79
				$tableRows
80
			);
81
82
			$table = implode( "\n|-\n", $tableRows );
83
84
			$table =
85
				'{| class="wikitable sortable"' . "\n" .
86
				$table .
87
				"\n|}";
88
		}
89
90
		return $table;
91
	}
92
93
	/**
94
	 * Returns the wikitext for a table row describing a single parameter.
95
	 *
96
	 * @param ParamDefinition $parameter
97
	 *
98
	 * @return string
99
	 */
100
	private function getDescriptionRow( ParamDefinition $parameter ) {
101
		$description = $this->msg( $parameter->getMessage() );
102
103
		$type = $this->msg( $parameter->getTypeMessage() );
104
105
		$default = $parameter->isRequired() ? "''" . $this->msg(
106
				'validator-describe-required'
107
			) . "''" : $parameter->getDefault();
108
		if ( is_array( $default ) ) {
109
			$default = implode( ', ', $default );
110
		} elseif ( is_bool( $default ) ) {
111
			$default = $default ? 'yes' : 'no';
112
		}
113
114
		if ( $default === '' ) {
115
			$default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
116
		}
117
118
		return <<<EOT
119
| {$parameter->getName()}
120
| {$type}
121
| {$default}
122
| {$description}
123
EOT;
124
	}
125
126
	/**
127
	 * Message function that takes into account the language parameter.
128
	 *
129
	 * @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...
130
	 * @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...
131
	 *
132
	 * @return string
133
	 */
134
	private function msg() {
135
		$args = func_get_args();
136
		$key = array_shift( $args );
137
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
138
	}
139
140
	/**
141
	 * @see ParserHook::getDescription()
142
	 */
143
	public function getMessage() {
144
		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...
145
	}
146
147
	/**
148
	 * Gets the name of the parser hook.
149
	 *
150
	 * @see ParserHook::getName
151
	 *
152
	 * @return string
153
	 */
154 50
	protected function getName() {
155 50
		return 'mapsdoc';
156
	}
157
158
	/**
159
	 * Returns an array containing the parameter info.
160
	 *
161
	 * @see ParserHook::getParameterInfo
162
	 *
163
	 * @return array
164
	 */
165 3
	protected function getParameterInfo( $type ) {
166 3
		$params = [];
167
168 3
		$params['service'] = [
169 3
			'values' => $GLOBALS['egMapsAvailableServices'],
170
			'tolower' => true,
171
		];
172
173 3
		$params['language'] = [
174 3
			'default' => $GLOBALS['wgLanguageCode'],
175
		];
176
177
		// Give grep a chance to find the usages:
178
		// maps-geocode-par-service, maps-geocode-par-language
179 3
		foreach ( $params as $name => &$param ) {
180 3
			$param['message'] = 'maps-geocode-par-' . $name;
181
		}
182
183 3
		return $params;
184
	}
185
186
	/**
187
	 * Returns the list of default parameters.
188
	 *
189
	 * @see ParserHook::getDefaultParameters
190
	 *
191
	 * @return array
192
	 */
193 1
	protected function getDefaultParameters( $type ) {
194 1
		return [ 'service', 'language' ];
195
	}
196
197
}
198