WP_API_MAPS_WIDGET::form()   F
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 80
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 58
nc 256
nop 1
dl 0
loc 80
rs 3.7752
c 0
b 0
f 0

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
 * Google maps API library.
4
 *
5
 * @package WP-API-Libraries
6
 **/
7
8
/**
9
 * WP_API_MAPS_WIDGET class.
10
 *
11
 * @extends WP_Widget
12
 */
13
class WP_API_MAPS_WIDGET extends WP_Widget {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
	/**
16
	 * Widget constructor.
17
	 *
18
	 * @access public
19
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
	 */
21
	public function __construct() {
22
23
		parent::__construct(
24
			'wp-api-maps',
25
			__( 'Google Maps' ),
26
			array(
27
				'description' => __( 'Display a location on google maps' ),
28
				'classname'   => 'wp-api-libraries',
29
			)
30
		);
31
	}
32
33
	/**
34
	 * Widget method.
35
	 *
36
	 * @access public
37
	 * @param mixed $args Arguments.
38
	 * @param mixed $instance Instance.
39
	 * @return void
40
	 */
41
	public function widget( $args, $instance ) {
42
		$instance = $this->parse_args( $instance );
43
44
		// Display widget title.
45
		if ( isset( $instance['title'] ) ) {
46
			echo $args['before_title'];
47
			echo esc_attr( $instance['title'] );
48
			echo $args['after_title'];
49
		}
50
51
		WPAPI_GOOGLE_MAPS::print_map( $instance );
52
	}
53
54
	/**
55
	 * Form function.
56
	 *
57
	 * @access public
58
	 * @param mixed $instance Instance.
59
	 * @return void
60
	 */
61
	public function form( $instance ) {
62
		// Set default values.
63
		$instance = $this->parse_args( $instance );
64
65
		// Retrieve an existing value from the database.
66
		$title['val']  = ! empty( $instance['title'] ) ? $instance['title'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$title was never initialized. Although not strictly required by PHP, it is generally a good practice to add $title = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
67
		$lat['val']    = ! empty( $instance['lat'] ) ? $instance['lat'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lat was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lat = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
68
		$lng['val']    = ! empty( $instance['lng'] ) ? $instance['lng'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lng was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lng = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
69
		$info['val'] 	 = ! empty( $instance['info'] ) ? $instance['info'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $info = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
70
		$width['val']  = ! empty( $instance['width'] ) ? $instance['width'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$width was never initialized. Although not strictly required by PHP, it is generally a good practice to add $width = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
71
		$height['val'] = ! empty( $instance['height'] ) ? $instance['height'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$height was never initialized. Although not strictly required by PHP, it is generally a good practice to add $height = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
72
		$zoom['val'] 	 = ! empty( $instance['zoom'] ) ? $instance['zoom'] : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$zoom was never initialized. Although not strictly required by PHP, it is generally a good practice to add $zoom = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
73
		$scrollwheel['val'] 	 = ! empty( $instance['scrollwheel'] ) ? $instance['scrollwheel'] : 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$scrollwheel was never initialized. Although not strictly required by PHP, it is generally a good practice to add $scrollwheel = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
75
		$title['id'] 	= $this->get_field_id( 'title' );
76
		$lat['id'] 		= $this->get_field_id( 'lat' );
77
		$lng['id']		= $this->get_field_id( 'lng' );
78
		$info['id']		= $this->get_field_id( 'info' );
79
		$width['id'] 	= $this->get_field_id( 'width' );
80
		$height['id'] = $this->get_field_id( 'height' );
81
		$zoom['id'] 	= $this->get_field_id( 'zoom' );
82
		$scrollwheel['id'] 	= $this->get_field_id( 'scrollwheel' );
83
84
		$title['name'] 	= $this->get_field_name( 'title' );
85
		$lat['name'] 	 	= $this->get_field_name( 'lat' );
86
		$lng['name']	 	= $this->get_field_name( 'lng' );
87
		$info['name']  	= $this->get_field_name( 'info' );
88
		$width['name'] 	= $this->get_field_name( 'width' );
89
		$height['name'] = $this->get_field_name( 'height' );
90
		$zoom['name'] 	= $this->get_field_name( 'zoom' );
91
		$scrollwheel['name'] 	= $this->get_field_name( 'scrollwheel' );
92
93
		// Widget title.
94
		echo '<p>';
95
		echo '	<label for="' . esc_attr( $title['id'] ) . '" class="wp-api-maps_title_label">' . esc_attr( 'Title:' ) . '</label>';
96
		echo '	<input type="text" id="' . esc_attr( $title['id'] ) . '" name="' . esc_attr( $title['name'] ) . '" class="widefat" value="' . esc_attr( $title['val'] ) . '">';
97
		echo '</p>';
98
99
		// Widget width.
100
		echo '<p>';
101
		echo '	<label for="' . esc_attr( $width['id'] ) . '" class="wp-api-maps_width_label">' . esc_attr( 'Width:' ) . '</label>';
102
		echo '	<input type="text" id="' . esc_attr( $width['id'] ) . '" name="' . esc_attr( $width['name'] ) . '" class="widefat" value="' . esc_attr( $width['val'] ) . '">';
103
		echo '</p>';
104
105
		// Widget height.
106
		echo '<p>';
107
		echo '	<label for="' . esc_attr( $height['id'] ) . '" class="wp-api-maps_height_label">' . esc_attr( 'Height:' ) . '</label>';
108
		echo '	<input type="text" id="' . esc_attr( $height['id'] ) . '" name="' . esc_attr( $height['name'] ) . '" class="widefat" value="' . esc_attr( $height['val'] ) . '">';
109
		echo '</p>';
110
111
		// Latitude input.
112
		echo '<p>';
113
		echo '	<label for="' . esc_attr( $lat['id'] ) . '" class="wp-api-maps_lat_label">' . esc_attr( 'Latitude:' ) . '</label>';
114
		echo '	<input type="text" id="' . esc_attr( $lat['id'] ) . '" name="' . esc_attr( $lat['name'] ) . '" class="widefat" value="' . esc_attr( $lat['val'] ) . '">';
115
		echo '</p>';
116
117
		// Longitude input.
118
		echo '<p>';
119
		echo '	<label for="' . esc_attr( $lng['id'] ) . '" class="wp-api-maps_lng_label">' . esc_attr( 'Longitude:' ) . '</label>';
120
		echo '	<input type="text" id="' . esc_attr( $lng['id'] ) . '" name="' . esc_attr( $lng['name'] ) . '" class="widefat" value="' . esc_attr( $lng['val'] ) . '">';
121
		echo '</p>';
122
123
		// Info content input.
124
		echo '<p>';
125
		echo '	<label for="' . esc_attr( $info['id'] ) . '" class="wp-api-maps_info_label">' . esc_attr( 'Info window content:' ) . '</label>';
126
		echo '	<input type="text" id="' . esc_attr( $info['id'] ) . '" name="' . esc_attr( $info['name'] ) . '" class="widefat" value="' . esc_attr( $info['val'] ) . '">';
127
		echo '</p>';
128
129
		// Zoom input.
130
		echo '<p>';
131
		echo '	<label for="' . esc_attr( $zoom['id'] ) . '" class="wp-api-maps_zoom_label">' . esc_attr( 'Zoom:' ) . '</label>';
132
		echo '	<input type="text" id="' . esc_attr( $zoom['id'] ) . '" name="' . esc_attr( $zoom['name'] ) . '" class="widefat" value="' . esc_attr( $zoom['val'] ) . '">';
133
		echo '</p>';
134
135
		// ScrollWheel option.
136
		echo '<p>';
137
		echo '	<input value="1" type="checkbox"' . checked( esc_attr( $scrollwheel['val'] ), 1, false ) . 'id="' . esc_attr( $scrollwheel['id'] ) . '" name="' . esc_attr( $scrollwheel['name'] ) . '" />';
138
		echo '	<label for="' .  esc_attr( $scrollwheel['id'] ) . '">Disable Scroll Zoom</label>';
139
		echo '</p>';
140
	}
141
142
	/**
143
	 * Update function.
144
	 *
145
	 * @access public
146
	 * @param mixed $new_instance New Instance.
147
	 * @param mixed $old_instance Old Instance.
148
	 * @return $instance Instance.
0 ignored issues
show
Documentation introduced by
The doc-type $instance could not be parsed: Unknown type name "$instance" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
149
	 */
150
	public function update( $new_instance, $old_instance ) {
151
152
		$instance = $old_instance;
153
154
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
155
		$instance['width'] = ! empty( $new_instance['width'] ) ? strip_tags( $new_instance['width'] ) : '';
156
		$instance['height'] = ! empty( $new_instance['height'] ) ? strip_tags( $new_instance['height'] ) : '';
157
		$instance['lat'] = ! empty( $new_instance['lat'] ) ? strip_tags( $new_instance['lat'] ) : '';
158
		$instance['lng'] = ! empty( $new_instance['lng'] ) ? strip_tags( $new_instance['lng'] ) : '';
159
		$instance['info'] = ! empty( $new_instance['info'] ) ? strip_tags( $new_instance['info'] ) : '';
160
		$instance['zoom'] = ! empty( $new_instance['zoom'] ) ? strip_tags( $new_instance['zoom'] ) : null;
161
		$instance['scrollwheel'] = ! empty( $new_instance['scrollwheel'] ) ? strip_tags( $new_instance['scrollwheel'] ) : 0;
162
163
		return $instance;
164
	}
165
166
	/**
167
	 * Parse default arguments.
168
	 *
169
	 * @param  [Array] $args : Array of arguments to parse.
0 ignored issues
show
Documentation introduced by
The doc-type [Array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
170
	 * @return [Array]       : Parsed arguments.
0 ignored issues
show
Documentation introduced by
The doc-type [Array] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
171
	 */
172
	private function parse_args( $args ) {
173
		// Set default values.
174
		$args = wp_parse_args( $args, array(
175
			'title' => '',
176
			'width'	 => '300px',
177
			'height' => '300px',
178
			'lat'		 => '-17.7134',
179
			'lng'		 => '178.0650',
180
			'info'	 => '',
181
			'zoom'	 => 14,
182
			'scrollwheel' => 0,
183
		) );
184
185
		return $args;
186
	}
187
}
188