Completed
Push — leaflet-attribution ( 0121c0 )
by Peter
04:35
created

MapsGeodistance::getParameterInfo()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 1
1
<?php
2
3
/**
4
 * Class for the 'geodistance' parser hooks, which can
5
 * calculate the geographical distance between two points.
6
 *
7
 * @since 0.7
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class MapsGeodistance 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...
13
14
	/**
15
	 * Renders and returns the output.
16
	 *
17
	 * @see ParserHook::render
18
	 *
19
	 * @since 0.7
20
	 *
21
	 * @param array $parameters
22
	 *
23
	 * @return string
24
	 * @throws MWException
25
	 */
26
	public function render( array $parameters ) {
27
		/**
28
		 * @var \DataValues\LatLongValue $coordinates1
29
		 * @var \DataValues\LatLongValue $coordinates2
30
		 */
31
		$coordinates1 = $parameters['location1']->getCoordinates();
32
		$coordinates2 = $parameters['location2']->getCoordinates();
33
34
		$distance = MapsGeoFunctions::calculateDistance( $coordinates1, $coordinates2 );
35
		$output = MapsDistanceParser::formatDistance( $distance, $parameters['unit'], $parameters['decimals'] );
36
37
		return $output;
38
	}
39
40
	/**
41
	 * @see ParserHook::getMessage
42
	 *
43
	 * @since 1.0
44
	 */
45
	public function getMessage() {
46
		return 'maps-geodistance-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-geodistance-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...
47
	}
48
49
	/**
50
	 * Gets the name of the parser hook.
51
	 *
52
	 * @see ParserHook::getName
53
	 *
54
	 * @since 0.7
55
	 *
56
	 * @return string
57
	 */
58
	protected function getName() {
59
		return 'geodistance';
60
	}
61
62
	/**
63
	 * Returns an array containing the parameter info.
64
	 *
65
	 * @see ParserHook::getParameterInfo
66
	 *
67
	 * @since 0.7
68
	 *
69
	 * @return array
70
	 */
71
	protected function getParameterInfo( $type ) {
72
		global $egMapsDistanceUnit, $egMapsDistanceDecimals;
73
74
		$params = [];
75
76
		$params['unit'] = [
77
			'default' => $egMapsDistanceUnit,
78
			'values' => MapsDistanceParser::getUnits(),
79
		];
80
81
		$params['decimals'] = [
82
			'type' => 'integer',
83
			'default' => $egMapsDistanceDecimals,
84
		];
85
86
		$params['location1'] = [
87
			'type' => 'mapslocation', // FIXME: geoservice is not used
88
			'aliases' => 'from',
89
		];
90
91
		$params['location2'] = [
92
			'type' => 'mapslocation', // FIXME: geoservice is not used
93
			'aliases' => 'to',
94
		];
95
96
		// Give grep a chance to find the usages:
97
		// maps-geodistance-par-mappingservice, maps-geodistance-par-geoservice,
98
		// maps-geodistance-par-unit, maps-geodistance-par-decimals,
99
		// maps-geodistance-par-location1, maps-geodistance-par-location2
100
		foreach ( $params as $name => &$param ) {
101
			$param['message'] = 'maps-geodistance-par-' . $name;
102
		}
103
104
		return $params;
105
	}
106
107
	/**
108
	 * Returns the list of default parameters.
109
	 *
110
	 * @see ParserHook::getDefaultParameters
111
	 *
112
	 * @since 0.7
113
	 *
114
	 * @param $type
115
	 *
116
	 * @return array
117
	 */
118
	protected function getDefaultParameters( $type ) {
119
		return [ 'location1', 'location2', 'unit', 'decimals' ];
120
	}
121
122
}