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

CoordinatesFunction::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Maps\MediaWiki\ParserHooks;
4
5
use Maps\MapsFactory;
6
use ParserHook;
7
8
/**
9
 * Class for the 'coordinates' parser hooks,
10
 * which can transform the notation of a set of coordinates.
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class CoordinatesFunction 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...
16
17
	/**
18
	 * Renders and returns the output.
19
	 *
20
	 * @see ParserHook::render
21
	 *
22
	 * @param array $parameters
23
	 *
24
	 * @return string
25
	 */
26 14
	public function render( array $parameters ) {
27 14
		return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
28 14
			$parameters['location'],
29 14
			$parameters['format'],
30 14
			$parameters['directional']
31
		);
32
	}
33
34
	/**
35
	 * @see ParserHook::getMessage()
36
	 */
37
	public function getMessage() {
38
		return 'maps-coordinates-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-coordinates-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...
39
	}
40
41
	/**
42
	 * Gets the name of the parser hook.
43
	 *
44
	 * @see ParserHook::getName
45
	 *
46
	 * @return string
47
	 */
48 48
	protected function getName() {
49 48
		return 'coordinates';
50
	}
51
52
	/**
53
	 * Returns an array containing the parameter info.
54
	 *
55
	 * @see ParserHook::getParameterInfo
56
	 *
57
	 * @return array
58
	 */
59 19
	protected function getParameterInfo( $type ) {
60 19
		global $egMapsAvailableCoordNotations;
61 19
		global $egMapsCoordinateNotation;
62 19
		global $egMapsCoordinateDirectional;
63
64 19
		$params = [];
65
66 19
		$params['location'] = [
67
			'type' => 'coordinate',
68
		];
69
70 19
		$params['format'] = [
71 19
			'default' => $egMapsCoordinateNotation,
72 19
			'values' => $egMapsAvailableCoordNotations,
73 19
			'aliases' => 'notation',
74
			'tolower' => true,
75
		];
76
77 19
		$params['directional'] = [
78 19
			'type' => 'boolean',
79 19
			'default' => $egMapsCoordinateDirectional,
80
		];
81
82
		// Give grep a chance to find the usages:
83
		// maps-coordinates-par-location, maps-coordinates-par-format, maps-coordinates-par-directional
84 19
		foreach ( $params as $name => &$param ) {
85 19
			$param['message'] = 'maps-coordinates-par-' . $name;
86
		}
87
88 19
		return $params;
89
	}
90
91
	/**
92
	 * Returns the list of default parameters.
93
	 *
94
	 * @see ParserHook::getDefaultParameters
95
	 *
96
	 * @return array
97
	 */
98 16
	protected function getDefaultParameters( $type ) {
99 16
		return [ 'location', 'format', 'directional' ];
100
	}
101
102
}