Completed
Push — merge-sm ( 44b830...c70fa4 )
by Jeroen De
10:03 queued 07:12
created

MapsMapsDoc::msg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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
	 * Gets the name of the parser hook.
27
	 * @see ParserHook::getName
28
	 *
29
	 * @since 1.0
30
	 *
31
	 * @return string
32
	 */
33 1
	protected function getName() {
34 1
		return 'mapsdoc';
35
	}
36
37
	/**
38
	 * Returns an array containing the parameter info.
39
	 * @see ParserHook::getParameterInfo
40
	 *
41
	 * @since 1.0
42
	 *
43
	 * @return array
44
	 */
45 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...
46 3
		$params = [];
47
48 3
		$params['service'] = [
49 3
			'values' => $GLOBALS['egMapsAvailableServices'],
50 3
			'tolower' => true,
51
		];
52
53 3
		$params['language'] = [
54 3
			'default' => $GLOBALS['wgLanguageCode'],
55
		];
56
57
		// Give grep a chance to find the usages:
58
		// maps-geocode-par-service, maps-geocode-par-language
59 3
		foreach ( $params as $name => &$param ) {
60 3
			$param['message'] = 'maps-geocode-par-' . $name;
61 3
		}
62
63 3
		return $params;
64
	}
65
66
	/**
67
	 * Returns the list of default parameters.
68
	 * @see ParserHook::getDefaultParameters
69
	 *
70
	 * @since 1.0
71
	 *
72
	 * @return array
73
	 */
74 1
	protected function getDefaultParameters( $type ) {
75 1
		return [ 'service', 'language' ];
76
	}
77
78
	/**
79
	 * Renders and returns the output.
80
	 * @see ParserHook::render
81
	 *
82
	 * @since 1.0
83
	 *
84
	 * @param array $parameters
85
	 *
86
	 * @return string
87
	 */
88
	public function render( array $parameters ) {
89
		$this->language = $parameters['language'];
90
91
		$params = $this->getServiceParameters( $parameters['service'] );
92
93
		return $this->getParameterTable( $params );
94
	}
95
96
	/**
97
	 * Message function that takes into account the language parameter.
98
	 *
99
	 * @since 1.0.1
100
	 *
101
	 * @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...
102
	 * @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...
103
	 *
104
	 * @return string
105
	 */
106
	protected function msg() {
107
		$args = func_get_args();
108
		$key = array_shift( $args );
109
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
110
	}
111
112
	/**
113
	 * Returns the wikitext for a table listing the provided parameters.
114
	 *
115
	 * @since 1.0
116
	 *
117
	 * @param array $parameters
118
	 *
119
	 * @return string
120
	 */
121
	protected function getParameterTable( array $parameters ) {
122
		$tableRows = [];
123
124
		$parameters = ParamDefinition::getCleanDefinitions( $parameters );
125
126
		foreach ( $parameters as $parameter ) {
127
			$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...
128
		}
129
130
		$table = '';
131
132
		if ( count( $tableRows ) > 0 ) {
133
			$tableRows = array_merge( [
134
			'!' . $this->msg( 'validator-describe-header-parameter' ) ."\n" .
135
			//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
136
			'!' . $this->msg( 'validator-describe-header-type' ) ."\n" .
137
			'!' . $this->msg( 'validator-describe-header-default' ) ."\n" .
138
			'!' . $this->msg( 'validator-describe-header-description' )
139
			], $tableRows );
140
141
			$table = implode( "\n|-\n", $tableRows );
142
143
			$table =
144
					'{| class="wikitable sortable"' . "\n" .
145
					$table .
146
					"\n|}";
147
		}
148
149
		return $table;
150
	}
151
152
	/**
153
	 * Returns the wikitext for a table row describing a single parameter.
154
	 *
155
	 * @since 1.0
156
	 *
157
	 * @param ParamDefinition $parameter
158
	 *
159
	 * @return string
160
	 */
161
	protected function getDescriptionRow( ParamDefinition $parameter ) {
162
		$description = $this->msg( $parameter->getMessage() );
163
164
		$type = $parameter->getTypeMessage();
165
166
		$default = $parameter->isRequired() ? "''" . $this->msg( 'validator-describe-required' ) . "''" : $parameter->getDefault();
167
		if ( is_array( $default ) ) {
168
			$default = implode( ', ', $default );
169
		}
170
		elseif ( is_bool( $default ) ) {
171
			$default = $default ? 'yes' : 'no';
172
		}
173
174
		if ( $default === '' ) $default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
175
176
		return <<<EOT
177
| {$parameter->getName()}
178
| {$type}
179
| {$default}
180
| {$description}
181
EOT;
182
	}
183
184
	protected function getServiceParameters( $service ) {
185
		$service = MapsMappingServices::getServiceInstance( $service );
186
187
		$params = [];
188
189
		$params['zoom'] = [
190
			'type' => 'integer',
191
			'message' => 'maps-par-zoom',
192
		];
193
194
		$service->addParameterInfo( $params );
195
196
		return $params;
197
	}
198
199
	/**
200
	 * @see ParserHook::getDescription()
201
	 *
202
	 * @since 1.0
203
	 */
204
	public function getMessage() {
205
		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...
206
	}
207
208
}
209