Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/parserhooks/Maps_Geodistance.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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