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

MapsFinddestination::getDefaultParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
	 * Gets the name of the parser hook.
18
	 * @see ParserHook::getName
19
	 * 
20
	 * @since 0.7
21
	 * 
22
	 * @return string
23
	 */
24 1
	protected function getName() {
25 1
		return 'finddestination';
26
	}
27
	
28
	/**
29
	 * Returns an array containing the parameter info.
30
	 * @see ParserHook::getParameterInfo
31
	 * 
32
	 * @since 0.7
33
	 * 
34
	 * @return array
35
	 */
36 51
	protected function getParameterInfo( $type ) {
37 51
		global $egMapsAvailableCoordNotations;
38 51
		global $egMapsCoordinateNotation, $egMapsCoordinateDirectional;
39
		
40 51
		$params = [];
41
42 51
		$params['location'] = [
43
			'type' => 'mapslocation',
44
		];
45
46 51
		$params['format'] = [
47 51
			'default' => $egMapsCoordinateNotation,
48 51
			'values' => $egMapsAvailableCoordNotations,
49 51
			'aliases' => 'notation',
50
			'tolower' => true,
51
		];
52
53 51
		$params['directional'] = [
54 51
			'type' => 'boolean',
55 51
			'default' => $egMapsCoordinateDirectional,
56
		];
57
58 51
		$params['bearing'] = [
59
			'type' => 'float',
60
		];
61
62 51
		$params['distance'] = [
63
			'type' => 'distance',
64
		];
65
66
		// Give grep a chance to find the usages:
67
		// maps-finddestination-par-location, maps-finddestination-par-format,
68
		// maps-finddestination-par-directional, maps-finddestination-par-bearing,
69
		// maps-finddestination-par-distance, maps-finddestination-par-mappingservice,
70
		// maps-finddestination-par-geoservice, maps-finddestination-par-allowcoordinates
71 51
		foreach ( $params as $name => &$param ) {
72 51
			$param['message'] = 'maps-finddestination-par-' . $name;
73
		}
74
75 51
		return $params;
76
	}
77
	
78
	/**
79
	 * Returns the list of default parameters.
80
	 * @see ParserHook::getDefaultParameters
81
	 * 
82
	 * @since 0.7
83
	 * 
84
	 * @return array
85
	 */
86 1
	protected function getDefaultParameters( $type ) {
87 1
		return [ 'location', 'bearing', 'distance' ];
88
	}
89
	
90
	/**
91
	 * Renders and returns the output.
92
	 * @see ParserHook::render
93
	 * 
94
	 * @since 0.7
95
	 * 
96
	 * @param array $parameters
97
	 * 
98
	 * @return string
99
	 */
100 1
	public function render( array $parameters ) {
101 1
		$destination = MapsGeoFunctions::findDestination(
102 1
			$parameters['location']->getCoordinates(),
103 1
			$parameters['bearing'],
104 1
			$parameters['distance']
105
		);
106
107 1
		$options = new \ValueFormatters\FormatterOptions( [
108 1
			GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
109 1
			GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
110
			GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
111
		] );
112
113 1
		$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...
114
115 1
		$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...
116 1
		$output = $formatter->format( $geoCoords );
117
118 1
		return $output;
119
	}
120
121
	/**
122
	 * @see ParserHook::getMessage()
123
	 * 
124
	 * @since 1.0
125
	 */
126
	public function getMessage() {
127
		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...
128
	}	
129
	
130
}