Completed
Push — mv ( 2424da...13ee4c )
by Jeroen De
06:33 queued 01:32
created

GeocodeFunction::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Maps\MediaWiki\ParserHooks;
4
5
use Jeroen\SimpleGeocoder\Geocoder;
6
use Maps\MapsFactory;
7
use ParserHook;
8
9
/**
10
 * Class for the 'geocode' parser hooks, which can turn
11
 * human readable locations into sets of coordinates.
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GeocodeFunction 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...
17
18
	private $geocoder;
19
20 49
	public function __construct( Geocoder $geocoder = null ) {
21 49
		$this->geocoder = $geocoder ?? \Maps\MapsFactory::newDefault()->newGeocoder();
22 49
		parent::__construct();
23 49
	}
24
25
	/**
26
	 * Renders and returns the output.
27
	 *
28
	 * @see ParserHook::render
29
	 *
30
	 * @param array $parameters
31
	 *
32
	 * @return string
33
	 */
34 3
	public function render( array $parameters ) {
35 3
		$coordinates = $this->geocoder->geocode( $parameters['location'] );
36
37 3
		if ( $coordinates === null ) {
38 1
			return 'Geocoding failed'; // TODO: i18n
39
		}
40
41 2
		return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
42 2
			$coordinates,
43 2
			$parameters['format'],
44 2
			$parameters['directional']
45
		);
46
	}
47
48
	/**
49
	 * @see ParserHook::getMessage()
50
	 */
51
	public function getMessage() {
52
		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...
53
	}
54
55
	/**
56
	 * Gets the name of the parser hook.
57
	 *
58
	 * @see ParserHook::getName
59
	 *
60
	 * @return string
61
	 */
62 48
	protected function getName() {
63 48
		return 'geocode';
64
	}
65
66
	/**
67
	 * Returns an array containing the parameter info.
68
	 *
69
	 * @see ParserHook::getParameterInfo
70
	 *
71
	 * @return array
72
	 */
73 4
	protected function getParameterInfo( $type ) {
74 4
		global $egMapsAvailableCoordNotations;
75 4
		global $egMapsCoordinateNotation;
76 4
		global $egMapsCoordinateDirectional;
77
78 4
		$params = [];
79
80 4
		$params['location'] = [
81
			'type' => 'string',
82
			'message' => 'maps-geocode-par-location',
83
		];
84
85 4
		$params['format'] = [
86 4
			'default' => $egMapsCoordinateNotation,
87 4
			'values' => $egMapsAvailableCoordNotations,
88 4
			'aliases' => 'notation',
89
			'tolower' => true,
90 4
			'message' => 'maps-geocode-par-format',
91
		];
92
93 4
		$params['directional'] = [
94 4
			'type' => 'boolean',
95 4
			'default' => $egMapsCoordinateDirectional,
96 4
			'message' => 'maps-geocode-par-directional',
97
		];
98
99 4
		return $params;
100
	}
101
102
	/**
103
	 * Returns the list of default parameters.
104
	 *
105
	 * @see ParserHook::getDefaultParameters
106
	 *
107
	 * @return array
108
	 */
109 3
	protected function getDefaultParameters( $type ) {
110 3
		return [ 'location' ];
111
	}
112
113
}
114