Completed
Push — php4 ( 24a287 )
by Jeroen De
06:55 queued 02:49
created

MapsDocFunction   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 22.03%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 184
ccs 13
cts 59
cp 0.2203
rs 10
c 0
b 0
f 0

9 Methods

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