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

MapsFinddestination::getParameterInfo()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 1
1
<?php
2
3
use DataValues\Geo\Formatters\GeoCoordinateFormatter;
4
5
/**
6
 * Class for the 'finddestination' parser hooks, which can find a
7
 * destination given a starting point, an initial bearing and a distance.
8
 *
9
 * @since 0.7
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class MapsFinddestination 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...
15
16
	/**
17
	 * Renders and returns the output.
18
	 *
19
	 * @see ParserHook::render
20
	 *
21
	 * @since 0.7
22
	 *
23
	 * @param array $parameters
24
	 *
25
	 * @return string
26
	 */
27
	public function render( array $parameters ) {
28
		$destination = MapsGeoFunctions::findDestination(
29
			$parameters['location']->getCoordinates(),
30
			$parameters['bearing'],
31
			$parameters['distance']
32
		);
33
34
		$options = new \ValueFormatters\FormatterOptions(
35
			[
36
				GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
37
				GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
38
				GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
39
			]
40
		);
41
42
		$formatter = new GeoCoordinateFormatter( $options );
0 ignored issues
show
Deprecated Code introduced by
The class DataValues\Geo\Formatters\GeoCoordinateFormatter has been deprecated with message: since 2.0, use the base class instead.

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...
43
44
		$geoCoords = new \DataValues\LatLongValue( $destination['lat'], $destination['lon'] );
0 ignored issues
show
Deprecated Code introduced by
The class DataValues\LatLongValue has been deprecated with message: since 1.0, use the base class instead.

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...
45
		$output = $formatter->format( $geoCoords );
46
47
		return $output;
48
	}
49
50
	/**
51
	 * @see ParserHook::getMessage()
52
	 *
53
	 * @since 1.0
54
	 */
55
	public function getMessage() {
56
		return 'maps-finddestination-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-finddestination-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...
57
	}
58
59
	/**
60
	 * Gets the name of the parser hook.
61
	 *
62
	 * @see ParserHook::getName
63
	 *
64
	 * @since 0.7
65
	 *
66
	 * @return string
67
	 */
68
	protected function getName() {
69
		return 'finddestination';
70
	}
71
72
	/**
73
	 * Returns an array containing the parameter info.
74
	 *
75
	 * @see ParserHook::getParameterInfo
76
	 *
77
	 * @since 0.7
78
	 *
79
	 * @return array
80
	 */
81
	protected function getParameterInfo( $type ) {
82
		global $egMapsAvailableCoordNotations;
83
		global $egMapsCoordinateNotation, $egMapsCoordinateDirectional;
84
85
		$params = [];
86
87
		$params['location'] = [
88
			'type' => 'mapslocation',
89
		];
90
91
		$params['format'] = [
92
			'default' => $egMapsCoordinateNotation,
93
			'values' => $egMapsAvailableCoordNotations,
94
			'aliases' => 'notation',
95
			'tolower' => true,
96
		];
97
98
		$params['directional'] = [
99
			'type' => 'boolean',
100
			'default' => $egMapsCoordinateDirectional,
101
		];
102
103
		$params['bearing'] = [
104
			'type' => 'float',
105
		];
106
107
		$params['distance'] = [
108
			'type' => 'distance',
109
		];
110
111
		// Give grep a chance to find the usages:
112
		// maps-finddestination-par-location, maps-finddestination-par-format,
113
		// maps-finddestination-par-directional, maps-finddestination-par-bearing,
114
		// maps-finddestination-par-distance, maps-finddestination-par-mappingservice,
115
		// maps-finddestination-par-geoservice, maps-finddestination-par-allowcoordinates
116
		foreach ( $params as $name => &$param ) {
117
			$param['message'] = 'maps-finddestination-par-' . $name;
118
		}
119
120
		return $params;
121
	}
122
123
	/**
124
	 * Returns the list of default parameters.
125
	 *
126
	 * @see ParserHook::getDefaultParameters
127
	 *
128
	 * @since 0.7
129
	 *
130
	 * @return array
131
	 */
132
	protected function getDefaultParameters( $type ) {
133
		return [ 'location', 'bearing', 'distance' ];
134
	}
135
136
}