Passed
Pull Request — master (#18)
by Daryl
01:45
created

Marker   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 185
Duplicated Lines 7.57 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
dl 14
loc 185
ccs 46
cts 46
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A location() 0 7 2
A label() 0 7 2
A marker_args() 0 12 1
A __construct() 0 19 2
A set_icon() 0 10 2
A latitude() 6 6 3
A _geocoder() 0 7 2
A position() 0 3 1
A longitude() 7 7 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Clubdeuce\WPGoogleMaps;
4
5
/**
6
 * Class Marker
7
 * @package Clubdeuce\WPGoogleMaps
8
 *
9
 * @method string      address()
10
 * @method array       extra_args()
11
 * @method Geocoder    geocoder()
12
 * @method array       icon()
13
 * @method Info_Window info_window()
14
 * @method string      title()
15
 */
16
class Marker extends Model_Base {
17
18
	/**
19
	 * @var string
20
	 */
21
	protected $_address;
22
23
	/**
24
	 * @var Geocoder
25
	 */
26
	protected $_geocoder;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $_icon;
32
33
	/**
34
	 * @var Info_Window
35
	 */
36
	protected $_info_window;
37
38
	/**
39
	 * @var Marker_Label
40
	 */
41
	protected $_label;
42
43
	/**
44
	 * @var double
45
	 */
46
	protected $_latitude;
47
48
	/**
49
	 * @var double
50
	 */
51
	protected $_longitude;
52
53
	/**
54
	 * @var Location|\WP_Error
55
	 */
56
	protected $_location;
57
58
	/**
59
	 * This appears as a tooltip for the marker.
60
	 *
61
	 * @var string
62
	 */
63
	protected $_title;
64
65
	/**
66
	 * @var array
67
	 */
68
	protected $_extra_args = array();
69
70
	/**
71
	 * Marker_Model constructor.
72
	 * @param array $args
73
	 */
74 2
	public function __construct( $args = array() ) {
75
76 2
		$args = wp_parse_args( $args, array(
77 2
			'address'     => '',
78
			'icon'        => null,
79 2
			'info_window' => '',
80 2
			'label'       => new Marker_Label(),
81 2
			'title'       => '',
82
			'latitude'    => null,
83
			'longitude'   => null,
84
		) );
85
86 2
		if ( empty( $args['info_window'] ) ) {
87 2
			$args['info_window'] = new Info_Window( array(
88 2
				'position' => array( 'lat' => $args['latitude'], 'lng' => $args['longitude'] ),
89
			) );
90
		}
91
92 2
		parent::__construct( $args );
93
94 2
	}
95
96
	/**
97
	 * @return Marker_Label
98
	 */
99 1
	public function label() {
100
101 1
		if ( is_string( $this->_label ) ) {
102 1
			$this->_label = new Marker_Label( array( 'text' => $this->_label ) );
103
		}
104
105 1
		return $this->_label;
106
107
	}
108
109
	/**
110
	 * @return double
111
	 */
112 1 View Code Duplication
	public function latitude() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
114 1
		if ( is_null( $this->_latitude ) && ! is_wp_error( $this->location() ) ) {
115 1
			$this->_latitude = $this->location()->latitude();
116
		}
117 1
		return doubleval( $this->_latitude );
118
	}
119
120
	/**
121
	 * @return Location|\WP_Error
122
	 */
123 2
	public function location() {
124
125 2
		if ( ! is_object( $this->_location ) ) {
126 2
			$this->_location = $this->_geocoder()->geocode( $this->_address );
127
		}
128
129 2
		return $this->_location;
130
131
	}
132
133
	/**
134
	 * @return double
135
	 */
136 1 View Code Duplication
	public function longitude() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
138 1
		if ( is_null( $this->_longitude ) && ! is_wp_error( $this->location() ) ) {
139 1
			$this->_longitude = doubleval( $this->location()->longitude() );
140
		}
141
142 1
		return doubleval( $this->_longitude );
143
	}
144
145
	/**
146
	 * Get the position of this marker. An array with key/value pairs of lat and lng.
147
	 *
148
	 * @return array
149
	 */
150 1
	public function position() {
151
152 1
		return array( 'lat' => $this->latitude(), 'lng' => $this->longitude() );
153
154
	}
155
156
	/**
157
	 * @param string|array $icon
158
	 */
159 1
	public function set_icon( $icon ) {
160
161 1
		if ( is_string( $icon ) ) {
162
			//assume $icon is an URL
163
			$icon = array(
164 1
				'url' => (string)$icon,
165
			);
166
		}
167
168 1
		$this->_icon = $icon;
169
170 1
	}
171
172
	/**
173
	 * @param  array $args
174
	 * @return array
175
	 */
176 1
	public function marker_args( $args = array() ) {
177
178 1
		$args = array_merge( $args, $this->_extra_args );
179
180 1
		$args = wp_parse_args( $args, array(
181 1
			'position'  => $this->position(),
182 1
			'icon'      => $this->icon(),
183 1
			'label'     => $this->label()->options(),
184 1
			'title'     => $this->title(),
185
		) );
186
187 1
		return array_filter( $args );
188
189
	}
190
191
	/**
192
	 * @return Geocoder
193
	 */
194 2
	protected function _geocoder() {
195
196 2
		if (! is_a( $this->_geocoder, Geocoder::class ) ) {
197 1
			$this->_geocoder = new Geocoder();
198
		}
199
200 2
		return $this->_geocoder;
201
202
	}
203
204
}
205