Completed
Push — cs ( 148090 )
by Jeroen De
03:24
created

MapsDisplayMap::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Class for the 'display_map' parser hooks.
5
 *
6
 * @since 0.7
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 */
11
class MapsDisplayMap 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...
12
13
	/**
14
	 * Renders and returns the output.
15
	 *
16
	 * @see ParserHook::render
17
	 *
18
	 * @since 0.7
19
	 *
20
	 * @param array $parameters
21
	 *
22
	 * @return string
23
	 */
24 4
	public function render( array $parameters ) {
25 4
		$this->defaultMapZoom( $parameters );
26 4
		$this->trackMap();
27
28 4
		$renderer = new MapsDisplayMapRenderer(
29 4
			MapsMappingServices::getServiceInstance( $parameters['mappingservice'] )
30
		);
31
32 4
		return $renderer->renderMap( $parameters, $this->parser );
33
	}
34
35 4
	private function defaultMapZoom( &$parameters ) {
36 4
		$fullParams = $this->validator->getParameters();
0 ignored issues
show
Deprecated Code introduced by
The method ParamProcessor\Processor::getParameters() has been deprecated with message: since 1.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
37
38 4
		if ( array_key_exists( 'zoom', $fullParams ) && $fullParams['zoom']->wasSetToDefault() && count(
39 4
				$parameters['coordinates']
40 4
			) > 1 ) {
41
			$parameters['zoom'] = false;
42
		}
43 4
	}
44
45 4
	private function trackMap() {
0 ignored issues
show
Coding Style introduced by
trackMap uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46 4
		if ( $GLOBALS['egMapsEnableCategory'] ) {
47
			$this->parser->addTrackingCategory( 'maps-tracking-category' );
48
		}
49 4
	}
50
51
	/**
52
	 * @see ParserHook::getMessage()
53
	 *
54
	 * @since 1.0
55
	 */
56
	public function getMessage() {
57
		return 'maps-displaymap-description';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return 'maps-displaymap-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...
58
	}
59
60
	/**
61
	 * @see ParserHook::getNames()
62
	 *
63
	 * @since 2.0
64
	 *
65
	 * @return array
66
	 */
67 4
	protected function getNames() {
68 4
		return [ $this->getName(), 'display_point', 'display_points', 'display_line' ];
69
	}
70
71
	/**
72
	 * Gets the name of the parser hook.
73
	 *
74
	 * @see ParserHook::getName
75
	 *
76
	 * @since 0.7
77
	 *
78
	 * @return string
79
	 */
80 4
	protected function getName() {
81 4
		return 'display_map';
82
	}
83
84
	/**
85
	 * Returns an array containing the parameter info.
86
	 *
87
	 * @see ParserHook::getParameterInfo
88
	 *
89
	 * @since 0.7
90
	 *
91
	 * @return array
92
	 */
93 5
	protected function getParameterInfo( $type ) {
94 5
		$params = MapsMapper::getCommonParameters();
95
96 5
		$params['mappingservice']['feature'] = 'display_map';
97
98 5
		$params['coordinates'] = [
99 5
			'type' => 'string',
100
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
101
			'default' => [],
102
			'islist' => true,
103 5
			'delimiter' => $type === ParserHook::TYPE_FUNCTION ? ';' : "\n",
104 5
			'message' => 'maps-displaymap-par-coordinates',
105
		];
106
107 5
		return $params;
108
	}
109
110
	/**
111
	 * Returns the list of default parameters.
112
	 *
113
	 * @see ParserHook::getDefaultParameters
114
	 *
115
	 * @since 0.7
116
	 *
117
	 * @return array
118
	 */
119 4
	protected function getDefaultParameters( $type ) {
120 4
		return [ 'coordinates' ];
121
	}
122
123
	/**
124
	 * Returns the parser function options.
125
	 *
126
	 * @see ParserHook::getFunctionOptions
127
	 *
128
	 * @since 0.7
129
	 *
130
	 * @return array
131
	 */
132 4
	protected function getFunctionOptions() {
133
		return [
134 4
			'noparse' => true,
135
			'isHTML' => true
136
		];
137
	}
138
139
}
140