HomesHomeValuesWidget::form()   C
last analyzed

Complexity

Conditions 9
Paths 128

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 42
nc 128
nop 1
dl 0
loc 60
rs 6.3008
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Homes.com Home Values Widget (http://www.homes.com/widget/home-values/)
4
 *
5
 * @package RE-PRO
6
 */
7
8
	/* Exit if accessed directly. */
9
if ( ! defined( 'ABSPATH' ) ) { exit; }
10
11
/**
12
 * HomesHomeValuesWidget class.
13
 *
14
 * @extends WP_Widget
15
 */
16
class HomesHomeValuesWidget 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...
17
18
	/**
19
	 * __construct function.
20
	 *
21
	 * @access public
22
	 * @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...
23
	 */
24
	public function __construct() {
25
26
		parent::__construct(
27
			'homes_home_values',
28
			__( 'Homes Home Values', 're-pro' ),
29
			array(
30
				'description' => __( 'Display the average home value in a neighborhood from Homes.com', 're-pro' ),
31
				'classname'   => 're-pro re-pro-widget homes-widget homes-home-values',
32
				'customize_selective_refresh' => true,
33
			)
34
		);
35
	}
36
37
	/**
38
	 * Widget function.
39
	 *
40
	 * @access public
41
	 * @param mixed $args Arguments.
42
	 * @param mixed $instance Instance.
43
	 */
44
	public function widget( $args, $instance ) {
45
46
		$iframe_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : '';
47
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
48
		$location = ! empty( $instance['location'] ) ? $instance['location'] : '';
49
		$firstColor = ! empty( $instance['firstColor'] ) ? $instance['firstColor'] : '';
50
		$secondColor = ! empty( $instance['secondColor'] ) ? $instance['secondColor'] : '';
51
		$average = ! empty( $instance['average'] ) ? $instance['average'] : '';
52
		$median = ! empty( $instance['median'] ) ? $instance['median'] : '';
53
54
		echo $args['before_widget'];
55
56
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
57
58
		$homes_widgets = new HomesWidgets();
59
60
		$homes_widgets->get_home_values( $iframe_id, $location, $firstColor, $secondColor, $average, $median );
61
62
		echo $args['after_widget'];
63
	}
64
65
	/**
66
	 * Form function.
67
	 *
68
	 * @access public
69
	 * @param mixed $instance Instance.
70
	 * @return void
71
	 */
72
	public function form( $instance ) {
73
74
		// Set default values.
75
		$instance = wp_parse_args( (array) $instance, array(
76
			'title' => '',
77
			'location' => '',
78
			'firstColor' => '0054a0',
79
			'secondColor' => 'f7841b',
80
			'average' => 1,
81
			'median' => 1,
82
		) );
83
84
		// Retrieve an existing value from the database.
85
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
86
		$location = ! empty( $instance['location'] ) ? $instance['location'] : '';
87
		$firstColor = ! empty( $instance['firstColor'] ) ? $instance['firstColor'] : '';
88
		$secondColor = ! empty( $instance['secondColor'] ) ? $instance['secondColor'] : '';
89
		$average = ! empty( $instance['average'] ) ? $instance['average'] : '';
90
		$median = ! empty( $instance['median'] ) ? $instance['median'] : '';
91
92
		if ( empty( $average ) && empty( $median ) ) {
93
			$average = 1;
94
			$median = 1;
95
		}
96
97
		// Title.
98
		echo '<p>';
99
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
100
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
101
		echo '</p>';
102
103
		// Location.
104
		echo '<p>';
105
		echo '	<label for="' . $this->get_field_id( 'location' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
106
		echo '	<input id="' . $this->get_field_id( 'location' ) . '" name="' . $this->get_field_name( 'location' ) . '" value="' . $location . '" class="widefat">';
107
		echo '</p>';
108
109
		// Average Color.
110
		echo '<p>';
111
		echo '	<label for="' . $this->get_field_id( 'firstColor' ) . '" class="title-label">' . __( 'Color 1:', 're-pro' ) . '</label>';
112
		echo '	<input id="' . $this->get_field_id( 'firstColor' ) . '" name="' . $this->get_field_name( 'firstColor' ) . '" value="' . $firstColor  . '" class="widefat">';
113
		echo '</p>';
114
115
		// Median Color.
116
		echo '<p>';
117
		echo '	<label for="' . $this->get_field_id( 'secondColor' ) . '" class="title-label">' . __( 'Color 2:', 're-pro' ) . '</label>';
118
		echo '	<input id="' . $this->get_field_id( 'secondColor' ) . '" name="' . $this->get_field_name( 'secondColor' ) . '" value="' . $secondColor  . '" class="widefat">';
119
		echo '</p>';
120
121
		// Home Value Types.
122
		echo '<p>';
123
		echo '<label for="home-values-type" class="homes_value_type_label">' . __( 'Home Value Types:', 're-pro' ) . '</label>';
124
		echo '<br />';
125
		echo '<input value="1" type="checkbox"' . checked( $average, 1, false ) . 'id="' . $this->get_field_id( 'average' ) . '" name="' . $this->get_field_name( 'average' )  . '" />';
126
		echo '<label for="' . $this->get_field_id( 'average' ) . '">Average Home Value</label>';
127
		echo '<br />';
128
		echo '<input value="1" type="checkbox"' . checked( $median, 1, false ) . 'id="' . $this->get_field_id( 'median' ) . '" name="' . $this->get_field_name( 'median' ) . '" />';
129
		echo '<label for="' . $this->get_field_id( 'median' ) . '">Median Home Value</label>';
130
		echo '</p>';
131
	}
132
133
	/**
134
	 * Update Widget Instance.
135
	 *
136
	 * @access public
137
	 * @param mixed $new_instance New Instance.
138
	 * @param mixed $old_instance Old Instance.
139
	 * @return $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...
140
	 */
141
	public function update( $new_instance, $old_instance ) {
142
143
		$instance = $old_instance;
144
145
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
146
		$instance['location'] = ! empty( $new_instance['location'] ) ? strip_tags( $new_instance['location'] ) : '';
147
		$instance['firstColor'] = ! empty( $new_instance['firstColor'] ) ? strip_tags( $new_instance['firstColor'] ) : '';
148
		$instance['secondColor'] = ! empty( $new_instance['secondColor'] ) ? strip_tags( $new_instance['secondColor'] ) : '';
149
		$instance['average'] = ! empty( $new_instance['average'] ) ? strip_tags( $new_instance['average'] ) : '';
150
		$instance['median'] = ! empty( $new_instance['median'] ) ? strip_tags( $new_instance['median'] ) : '';
151
152
		return $instance;
153
	}
154
}
155
156
/**
157
 * Register Homes.com Featured Listings Widget.
158
 *
159
 * @access public
160
 * @return void
161
 */
162
function repro_homes_com_home_values() {
163
	if ( ! is_ssl() ) {
164
		register_widget( 'HomesHomeValuesWidget' );
165
	}
166
}
167
add_action( 'widgets_init', 'repro_homes_com_home_values' );
168