Completed
Push — master ( 8344d3...1077d8 )
by Jeroen De
17s
created

MapsGeocode::getParameterInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 29
cts 29
cp 1
rs 8.8571
cc 1
eloc 29
nc 1
nop 1
crap 1
1
<?php
2
3
use DataValues\Geo\Formatters\GeoCoordinateFormatter;
4
use Maps\Geocoders;
5
6
/**
7
 * Class for the 'geocode' parser hooks, which can turn
8
 * human readable locations into sets of coordinates.
9
 * 
10
 * @since 0.7
11
 * 
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class MapsGeocode 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...
16
17
	/**
18
	 * Gets the name of the parser hook.
19
	 * @see ParserHook::getName
20
	 * 
21
	 * @since 0.7
22
	 * 
23
	 * @return string
24
	 */
25 1
	protected function getName() {
26 1
		return 'geocode';
27
	}
28
	
29
	/**
30
	 * Returns an array containing the parameter info.
31
	 * @see ParserHook::getParameterInfo
32
	 * 
33
	 * @since 0.7
34
	 * 
35
	 * @return array
36
	 */
37 2
	protected function getParameterInfo( $type ) {
38 2
		global $egMapsAvailableGeoServices, $egMapsAvailableCoordNotations;
39 2
		global $egMapsDefaultGeoService, $egMapsCoordinateNotation;
40 2
		global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
41
		
42 2
		$params = [];
43
44 2
		$params['location'] = [
45 2
			'type' => 'string',
46 2
			'message' => 'maps-geocode-par-location',
47
		];
48
49 2
		$params['geoservice'] = [
50 2
			'default' => $egMapsDefaultGeoService,
51 2
			'aliases' => 'service',
52 2
			'values' => $egMapsAvailableGeoServices,
53 2
			'tolower' => true,
54 2
			'message' => 'maps-geocode-par-geoservice',
55
		];
56
57 2
		$params['allowcoordinates'] = [
58 2
			'type' => 'boolean',
59 2
			'default' => $egMapsAllowCoordsGeocoding,
60 2
			'message' => 'maps-geocode-par-allowcoordinates',
61
		];
62
63 2
		$params['format'] = [
64 2
			'default' => $egMapsCoordinateNotation,
65 2
			'values' => $egMapsAvailableCoordNotations,
66 2
			'aliases' => 'notation',
67 2
			'tolower' => true,
68 2
			'message' => 'maps-geocode-par-format',
69
		];
70
71 2
		$params['directional'] = [
72 2
			'type' => 'boolean',
73 2
			'default' => $egMapsCoordinateDirectional,
74 2
			'message' => 'maps-geocode-par-directional',
75
		];
76
77 2
		return $params;
78
	}
79
	
80
	/**
81
	 * Returns the list of default parameters.
82
	 * @see ParserHook::getDefaultParameters
83
	 * 
84
	 * @since 0.7
85
	 * 
86
	 * @return array
87
	 */
88 1
	protected function getDefaultParameters( $type ) {
89 1
		return [ 'location', 'geoservice' ];
90
	}	
91
	
92
	/**
93
	 * Renders and returns the output.
94
	 * @see ParserHook::render
95
	 * 
96
	 * @since 0.7
97
	 * 
98
	 * @param array $parameters
99
	 * 
100
	 * @return string
101
	 */
102 1
	public function render( array $parameters ) {
103 1
		if ( !Geocoders::canGeocode() ) {
104
			return 'No geocoders available';
105
		}
106
107 1
		$coordinates = Geocoders::attemptToGeocode(
108 1
			$parameters['location'],
109 1
			$parameters['geoservice'],
110 1
			$parameters['allowcoordinates']
111 1
		);
112
113 1
		if ( $coordinates === false ) {
114
			return 'Geocoding failed';
115
		}
116
117 1
		$options = new \ValueFormatters\FormatterOptions( [
118 1
			GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
119 1
			GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
120 1
			GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
121 1
		] );
122
123 1
		$formatter = new GeoCoordinateFormatter( $options );
124
125 1
		return $formatter->format( $coordinates );
126
	}
127
128
	/**
129
	 * @see ParserHook::getMessage()
130
	 * 
131
	 * @since 1.0
132
	 */
133
	public function getMessage() {
134
		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...
135
	}		
136
	
137
}