Completed
Push — rmds ( 34c84e )
by Jeroen De
04:50
created

MapsGeodistance   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
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 107
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

5 Methods

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