Completed
Push — master ( f04bbc...eab9d5 )
by Jeroen De
07:09
created

MapsDistance::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class for the 'distance' parser hooks, 
5
 * which can transform the notation of a distance.
6
 * 
7
 * @since 0.7
8
 * 
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class MapsDistance 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...
13
14
	/**
15
	 * Gets the name of the parser hook.
16
	 * @see ParserHook::getName
17
	 * 
18
	 * @since 0.7
19
	 * 
20
	 * @return string
21
	 */
22 6
	protected function getName() {
23 6
		return 'distance';
24
	}
25
	
26
	/**
27
	 * Returns an array containing the parameter info.
28
	 * @see ParserHook::getParameterInfo
29
	 * 
30
	 * @since 0.7
31
	 * 
32
	 * @return array
33
	 */
34 14
	protected function getParameterInfo( $type ) {
35 14
		global $egMapsDistanceUnit, $egMapsDistanceDecimals; 
36
		
37 14
		$params = [];
38
39 14
		$params['distance'] = [
40 14
			'type' => 'distance',
41
		];
42
43 14
		$params['unit'] = [
44 14
			'default' => $egMapsDistanceUnit,
45 14
			'values' => MapsDistanceParser::getUnits(),
46
		];
47
48 14
		$params['decimals'] = [
49 14
			'type' => 'integer',
50 14
			'default' => $egMapsDistanceDecimals,
51
		];
52
53
		// Give grep a chance to find the usages:
54
		// maps-distance-par-distance, maps-distance-par-unit, maps-distance-par-decimals
55 14
		foreach ( $params as $name => &$param ) {
56 14
			$param['message'] = 'maps-distance-par-' . $name;
57 14
		}
58
59 14
		return $params;
60
	}
61
	
62
	/**
63
	 * Returns the list of default parameters.
64
	 * @see ParserHook::getDefaultParameters
65
	 * 
66
	 * @since 0.7
67
	 *
68
	 * @param $type
69
	 * 
70
	 * @return array
71
	 */
72 6
	protected function getDefaultParameters( $type ) {
73 6
		return [ 'distance', 'unit', 'decimals' ];
74
	}
75
	
76
	/**
77
	 * Renders and returns the output.
78
	 * @see ParserHook::render
79
	 * 
80
	 * @since 0.7
81
	 * 
82
	 * @param array $parameters
83
	 * 
84
	 * @return string
85
	 */
86 6
	public function render( array $parameters ) {
87 6
		return MapsDistanceParser::formatDistance(
88 6
			$parameters['distance'],
89 6
			$parameters['unit'],
90 6
			$parameters['decimals']
91 6
		);
92
	}
93
94
	/**
95
	 * @see ParserHook::getMessage()
96
	 * 
97
	 * @since 1.0
98
	 */
99
	public function getMessage() {
100
		return 'maps-distance-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-distance-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...
101
	}		
102
	
103
}