Completed
Pull Request — master (#394)
by Peter
04:13
created

MapsLeaflet::addParameterInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 0
cts 28
cp 0
rs 8.9667
c 0
b 0
f 0
cc 1
eloc 53
nc 1
nop 1
crap 2

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 holding information and functionality specific to Leaflet.
5
 * This information and features can be used by any mapping feature.
6
 *
7
 * @licence GNU GPL v2+
8
 * @author Pavel Astakhov < [email protected] >
9
 */
10
class MapsLeaflet extends MapsMappingService {
11
12
	public function __construct( $serviceName ) {
13
		parent::__construct(
14
			$serviceName,
15
			[ 'leafletmaps', 'leaflet' ]
16
		);
17
	}
18
19
	/**
20
	 * @see MapsMappingService::addParameterInfo
21
	 *
22
	 * @since 3.0
23
	 */
24
	public function addParameterInfo( array &$params ) {
0 ignored issues
show
Coding Style introduced by
addParameterInfo 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...
25
		global $GLOBALS;
26
27
		$params['zoom'] = [
28
			'type' => 'integer',
29
			'range' => [ 0, 20 ],
30
			'default' => false,
31
			'message' => 'maps-par-zoom'
32
		];
33
34
		$params['defzoom'] = [
35
			'type' => 'integer',
36
			'range' => [ 0, 20 ],
37
			'default' => self::getDefaultZoom(),
38
			'message' => 'maps-leaflet-par-defzoom'
39
		];
40
41
		$params['layers'] = [
42
			'aliases' => 'layer',
43
			'type' => 'string',
44
			'values' => array_keys( $GLOBALS['egMapsLeafletAvailableLayers'], true, true ),
45
			'default' => $GLOBALS['egMapsLeafletLayers'],
46
			'message' => 'maps-leaflet-par-layer',
47
			'islist' => true,
48
		];
49
50
		$params['overlaylayers'] = [
51
			'type' => 'string',
52
			'values' => array_keys( $GLOBALS['egMapsLeafletAvailableOverlayLayers'], true, true ),
53
			'default' => $GLOBALS['egMapsLeafletOverlayLayers'],
54
			'message' => 'maps-leaflet-par-overlaylayers',
55
			'islist' => true,
56
		];
57
58
		$params['resizable'] = [
59
			'type' => 'boolean',
60
			'default' => $GLOBALS['egMapsResizableByDefault'],
61
			'message' => 'maps-par-resizable'
62
		];
63
64
		$params['enablefullscreen'] = [
65
			'type' => 'boolean',
66
			'default' => false,
67
			'message' => 'maps-par-enable-fullscreen',
68
		];
69
70
		$params['markercluster'] = [
71
			'type' => 'boolean',
72
			'default' => false,
73
			'message' => 'maps-par-markercluster',
74
		];
75
76
		$params['clustermaxzoom'] = [
77
			'type' => 'integer',
78
			'default' => 20,
79
			'message' => 'maps-par-clustermaxzoom',
80
		];
81
82
		$params['clusterzoomonclick'] = [
83
			'type' => 'boolean',
84
			'default' => true,
85
			'message' => 'maps-par-clusterzoomonclick',
86
		];
87
88
		$params['clustermaxradius'] = [
89
			'type' => 'integer',
90
			'default' => 80,
91
			'message' => 'maps-par-maxclusterradius',
92
		];
93
94
		$params['clusterspiderfy'] = [
95
			'type' => 'boolean',
96
			'default' => true,
97
			'message' => 'maps-leaflet-par-clusterspiderfy',
98
		];
99
	}
100
101
	/**
102
	 * @since 3.0
103
	 */
104
	public function getDefaultZoom() {
0 ignored issues
show
Coding Style introduced by
getDefaultZoom 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...
105
		return $GLOBALS['egMapsLeafletZoom'];
106
	}
107
108
	/**
109
	 * @see MapsMappingService::getMapId
110
	 *
111
	 * @since 3.0
112
	 */
113
	public function getMapId( $increment = true ) {
114
		static $mapsOnThisPage = 0;
115
116
		if ( $increment ) {
117
			$mapsOnThisPage++;
118
		}
119
120
		return 'map_leaflet_' . $mapsOnThisPage;
121
	}
122
123
	/**
124
	 * @see MapsMappingService::getResourceModules
125
	 *
126
	 * @since 3.0
127
	 *
128
	 * @return array of string
129
	 */
130
	public function getResourceModules() {
131
		return array_merge(
132
			parent::getResourceModules(),
133
			[ 'ext.maps.leaflet' ]
134
		);
135
	}
136
137
	protected function getDependencies() {
0 ignored issues
show
Coding Style introduced by
getDependencies 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...
138
		$leafletPath = $GLOBALS['wgScriptPath'] . '/extensions/Maps/includes/services/Leaflet/leaflet';
139
		return [
140
			Html::linkedStyle( "$leafletPath/leaflet.css" ),
141
			Html::linkedScript( "$leafletPath/leaflet.js" ),
142
		];
143
	}
144
145
}
146