Completed
Push — zzzz ( 5a7928 )
by Jeroen De
03:06
created

MapsDisplayMap::getCommonMapParams()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 93
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 58
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 56
nc 3
nop 0
dl 0
loc 93
ccs 58
cts 58
cp 1
crap 3
rs 8.4642
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A MapsDisplayMap::getDefaultParameters() 0 3 1
A MapsDisplayMap::render() 0 8 1
A MapsDisplayMap::defaultMapZoom() 0 7 4
A MapsDisplayMap::trackMap() 0 5 2
A MapsDisplayMap::getFunctionOptions() 0 6 1
A MapsDisplayMap::getMessage() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	 * Gets the name of the parser hook.
15
	 * @see ParserHook::getName
16
	 * 
17
	 * @since 0.7
18
	 * 
19
	 * @return string
20
	 */
21 4
	protected function getName() {
22 4
		return 'display_map';
23
	}
24
25
	/**
26
	 * @see ParserHook::getNames()
27
	 *
28
	 * @since 2.0
29
	 *
30
	 * @return array
31
	 */
32 4
	protected function getNames() {
33 4
		return [ $this->getName(), 'display_point', 'display_points', 'display_line' ];
34
	}
35
	
36
	/**
37
	 * Returns an array containing the parameter info.
38
	 * @see ParserHook::getParameterInfo
39
	 *
40
	 * @since 0.7
41
	 * 
42
	 * @return array
43
	 */
44 5
	protected function getParameterInfo( $type ) {
45 5
		$params = MapsMapper::getCommonParameters();
46
47 5
		$params['mappingservice']['feature'] = 'display_map';
48
49 5
		$params['coordinates'] = [
50 5
			'type' => 'string',
51 5
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
52 5
			'default' => [],
53 5
			'islist' => true,
54 5
			'delimiter' => $type === ParserHook::TYPE_FUNCTION ? ';' : "\n",
55 5
			'message' => 'maps-displaymap-par-coordinates',
56
		];
57
58 5
		return $params;
59
	}
60
61
	/**
62
	 * Returns the list of default parameters.
63
	 * @see ParserHook::getDefaultParameters
64
	 * 
65
	 * @since 0.7
66
	 * 
67
	 * @return array
68
	 */
69 4
	protected function getDefaultParameters( $type ) {
70 4
		return [ 'coordinates' ];
71
	}
72
	
73
	/**
74
	 * Renders and returns the output.
75
	 * @see ParserHook::render
76
	 * 
77
	 * @since 0.7
78
	 * 
79
	 * @param array $parameters
80
	 * 
81
	 * @return string
82
	 */
83 4
	public function render( array $parameters ) {
84 4
		$this->defaultMapZoom( $parameters );
85 4
		$this->trackMap();
86
87 4
		$renderer = new MapsDisplayMapRenderer( MapsMappingServices::getServiceInstance( $parameters['mappingservice'] ) );
88
89 4
		return $renderer->renderMap( $parameters, $this->parser );
90
	}
91
92 4
	private function defaultMapZoom( &$parameters ) {
93 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...
94
95 4
		if ( array_key_exists( 'zoom', $fullParams ) && $fullParams['zoom']->wasSetToDefault() && count( $parameters['coordinates'] ) > 1 ) {
96
			$parameters['zoom'] = false;
97
		}
98 4
	}
99
100 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...
101 4
		if ( $GLOBALS['egMapsEnableCategory'] ) {
102
			$this->parser->addTrackingCategory( 'maps-tracking-category' );
103
		}
104 4
	}
105
	
106
	/**
107
	 * Returns the parser function options.
108
	 * @see ParserHook::getFunctionOptions
109
	 * 
110
	 * @since 0.7
111
	 * 
112
	 * @return array
113
	 */
114 4
	protected function getFunctionOptions() {
115
		return [
116 4
			'noparse' => true,
117
			'isHTML' => true
118 4
		];
119
	}
120
121
	/**
122
	 * @see ParserHook::getMessage()
123
	 * 
124
	 * @since 1.0
125
	 */
126
	public function getMessage() {
127
		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...
128
	}		
129
	
130
}
131