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

includes/parserhooks/Maps_Geocode.php (1 issue)

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
use DataValues\Geo\Formatters\GeoCoordinateFormatter;
3
4
/**
5
 * Class for the 'geocode' parser hooks, which can turn
6
 * human readable locations into sets of coordinates.
7
 * 
8
 * @since 0.7
9
 * 
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class MapsGeocode extends ParserHook {
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 'geocode';
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 $egMapsAvailableGeoServices, $egMapsAvailableCoordNotations;
37
		global $egMapsDefaultGeoService, $egMapsCoordinateNotation;
38
		global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
39
		
40
		$params = [];
41
42
		$params['location'] = [
43
			'type' => 'mapslocation',
44
			'dependencies' => [ 'mappingservice', 'geoservice' ],
45
		];
46
47
		$params['mappingservice'] = [
48
			'default' => '',
49
			'values' => MapsMappingServices::getAllServiceValues(),
50
			'tolower' => true,
51
		];
52
53
		$params['geoservice'] = [
54
			'default' => $egMapsDefaultGeoService,
55
			'aliases' => 'service',
56
			'values' => $egMapsAvailableGeoServices,
57
			'tolower' => true,
58
		];
59
60
		$params['allowcoordinates'] = [
61
			'type' => 'boolean',
62
			'default' => $egMapsAllowCoordsGeocoding,
63
		];
64
65
		$params['format'] = [
66
			'default' => $egMapsCoordinateNotation,
67
			'values' => $egMapsAvailableCoordNotations,
68
			'aliases' => 'notation',
69
			'tolower' => true,
70
		];
71
72
		$params['directional'] = [
73
			'type' => 'boolean',
74
			'default' => $egMapsCoordinateDirectional,
75
		];
76
77
		// Give grep a chance to find the usages:
78
		// maps-geocode-par-location, maps-geocode-par-mappingservice, maps-geocode-par-geoservice,
79
		// maps-geocode-par-allowcoordinates, maps-geocode-par-format, maps-geocode-par-directional
80
		foreach ( $params as $name => &$param ) {
81
			$param['message'] = 'maps-geocode-par-' . $name;
82
		}
83
84
		return $params;
85
	}
86
	
87
	/**
88
	 * Returns the list of default parameters.
89
	 * @see ParserHook::getDefaultParameters
90
	 * 
91
	 * @since 0.7
92
	 * 
93
	 * @return array
94
	 */
95
	protected function getDefaultParameters( $type ) {
96
		return [ 'location', 'geoservice', 'mappingservice' ];
97
	}	
98
	
99
	/**
100
	 * Renders and returns the output.
101
	 * @see ParserHook::render
102
	 * 
103
	 * @since 0.7
104
	 * 
105
	 * @param array $parameters
106
	 * 
107
	 * @return string
108
	 */
109
	public function render( array $parameters ) {
110
		/**
111
		 * @var \DataValues\LatLongValue $coordinates
112
		 */
113
		$coordinates = $parameters['location']->getCoordinates();
114
115
		$options = new \ValueFormatters\FormatterOptions( [
116
			GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
117
			GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
118
			GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
119
		] );
120
121
		$formatter = new GeoCoordinateFormatter( $options );
122
123
		return $formatter->format( $coordinates );
124
	}
125
126
	/**
127
	 * @see ParserHook::getMessage()
128
	 * 
129
	 * @since 1.0
130
	 */
131
	public function getMessage() {
132
		return 'maps-geocode-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-geocode-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...
133
	}		
134
	
135
}