Completed
Push — cs ( 148090 )
by Jeroen De
03:24
created

MapsMapsDoc::getParameterInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
use ParamProcessor\ParamDefinition;
4
5
/**
6
 * Class for the 'mapsdoc' parser hooks,
7
 * which displays documentation for a specified mapping service.
8
 *
9
 * @since 1.0
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class MapsMapsDoc 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...
15
16
	/**
17
	 * Field to store the value of the language parameter.
18
	 *
19
	 * @since 1.0.1
20
	 *
21
	 * @var string
22
	 */
23
	protected $language;
24
25
	/**
26
	 * Renders and returns the output.
27
	 *
28
	 * @see ParserHook::render
29
	 *
30
	 * @since 1.0
31
	 *
32
	 * @param array $parameters
33
	 *
34
	 * @return string
35
	 */
36
	public function render( array $parameters ) {
37
		$this->language = $parameters['language'];
38
39
		$params = $this->getServiceParameters( $parameters['service'] );
40
41
		return $this->getParameterTable( $params );
42
	}
43
44
	private function getServiceParameters( $service ) {
45
		$service = MapsMappingServices::getServiceInstance( $service );
46
47
		$params = [];
48
49
		$params['zoom'] = [
50
			'type' => 'integer',
51
			'message' => 'maps-par-zoom',
52
		];
53
54
		$service->addParameterInfo( $params );
55
56
		return $params;
57
	}
58
59
	/**
60
	 * Returns the wikitext for a table listing the provided parameters.
61
	 *
62
	 * @since 1.0
63
	 *
64
	 * @param array $parameters
65
	 *
66
	 * @return string
67
	 */
68
	private function getParameterTable( array $parameters ) {
69
		$tableRows = [];
70
71
		$parameters = ParamDefinition::getCleanDefinitions( $parameters );
72
73
		foreach ( $parameters as $parameter ) {
74
			$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...
75
		}
76
77
		$table = '';
78
79
		if ( count( $tableRows ) > 0 ) {
80
			$tableRows = array_merge(
81
				[
82
					'!' . $this->msg( 'validator-describe-header-parameter' ) . "\n" .
83
					//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
84
					'!' . $this->msg( 'validator-describe-header-type' ) . "\n" .
85
					'!' . $this->msg( 'validator-describe-header-default' ) . "\n" .
86
					'!' . $this->msg( 'validator-describe-header-description' )
87
				],
88
				$tableRows
89
			);
90
91
			$table = implode( "\n|-\n", $tableRows );
92
93
			$table =
94
				'{| class="wikitable sortable"' . "\n" .
95
				$table .
96
				"\n|}";
97
		}
98
99
		return $table;
100
	}
101
102
	/**
103
	 * Returns the wikitext for a table row describing a single parameter.
104
	 *
105
	 * @param ParamDefinition $parameter
106
	 *
107
	 * @return string
108
	 */
109
	private function getDescriptionRow( ParamDefinition $parameter ) {
110
		$description = $this->msg( $parameter->getMessage() );
111
112
		$type = $this->msg( $parameter->getTypeMessage() );
113
114
		$default = $parameter->isRequired() ? "''" . $this->msg(
115
				'validator-describe-required'
116
			) . "''" : $parameter->getDefault();
117
		if ( is_array( $default ) ) {
118
			$default = implode( ', ', $default );
119
		}
120
		elseif ( is_bool( $default ) ) {
121
			$default = $default ? 'yes' : 'no';
122
		}
123
124
		if ( $default === '' ) {
125
			$default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
126
		}
127
128
		return <<<EOT
129
| {$parameter->getName()}
130
| {$type}
131
| {$default}
132
| {$description}
133
EOT;
134
	}
135
136
	/**
137
	 * Message function that takes into account the language parameter.
138
	 *
139
	 * @since 1.0.1
140
	 *
141
	 * @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...
142
	 * @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...
143
	 *
144
	 * @return string
145
	 */
146
	private function msg() {
147
		$args = func_get_args();
148
		$key = array_shift( $args );
149
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
150
	}
151
152
	/**
153
	 * @see ParserHook::getDescription()
154
	 *
155
	 * @since 1.0
156
	 */
157
	public function getMessage() {
158
		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...
159
	}
160
161
	/**
162
	 * Gets the name of the parser hook.
163
	 *
164
	 * @see ParserHook::getName
165
	 *
166
	 * @since 1.0
167
	 *
168
	 * @return string
169
	 */
170 1
	protected function getName() {
171 1
		return 'mapsdoc';
172
	}
173
174
	/**
175
	 * Returns an array containing the parameter info.
176
	 *
177
	 * @see ParserHook::getParameterInfo
178
	 *
179
	 * @since 1.0
180
	 *
181
	 * @return array
182
	 */
183 3
	protected function getParameterInfo( $type ) {
0 ignored issues
show
Coding Style introduced by
getParameterInfo uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
184 3
		$params = [];
185
186 3
		$params['service'] = [
187 3
			'values' => $GLOBALS['egMapsAvailableServices'],
188
			'tolower' => true,
189
		];
190
191 3
		$params['language'] = [
192 3
			'default' => $GLOBALS['wgLanguageCode'],
193
		];
194
195
		// Give grep a chance to find the usages:
196
		// maps-geocode-par-service, maps-geocode-par-language
197 3
		foreach ( $params as $name => &$param ) {
198 3
			$param['message'] = 'maps-geocode-par-' . $name;
199
		}
200
201 3
		return $params;
202
	}
203
204
	/**
205
	 * Returns the list of default parameters.
206
	 *
207
	 * @see ParserHook::getDefaultParameters
208
	 *
209
	 * @since 1.0
210
	 *
211
	 * @return array
212
	 */
213 1
	protected function getDefaultParameters( $type ) {
214 1
		return [ 'service', 'language' ];
215
	}
216
217
}
218