Completed
Push — mv ( 2424da...13ee4c )
by Jeroen De
06:33 queued 01:32
created

MapsDocFunction::getDescriptionRow()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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