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

GeoDistanceFunction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 101
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

5 Methods

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