ZillowMortgageRateWidget::form()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 98
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 83
nc 32
nop 1
dl 0
loc 98
rs 8.1707
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 17 and the first side effect is on line 4.

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
/* Exit if accessed directly. */
4
if ( ! defined( 'ABSPATH' ) ) { exit; }
5
6
/**
7
 * Zillow Mortgage Rate Widget (https://www.zillow.com/webtools/widgets/MortgageRateWidget.htm)
8
 *
9
 * @package RE-PRO
10
 */
11
12
/**
13
 * ZillowMortgageRateWidget class.
14
 *
15
 * @extends WP_Widget
16
 */
17
class ZillowMortgageRateWidget 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...
18
19
20
	/**
21
	 * __construct function.
22
	 *
23
	 * @access public
24
	 * @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...
25
	 */
26
	public function __construct() {
27
28
		parent::__construct(
29
			'zillow_mortgage_rate_widget',
30
			__( 'Zillow Mortgage Rate Table', 're-pro' ),
31
			array(
32
				'description' => __( 'Display a Mortgage Rate Table from Zillow.', 're-pro' ),
33
				'classname'   => 're-pro re-pro-widget zillow-widget zillow-widget-mortgage-rate-table',
34
				'customize_selective_refresh' => true,
35
			)
36
		);
37
	}
38
39
	/**
40
	 * Widget function.
41
	 *
42
	 * @access public
43
	 * @param mixed $args Arguments.
44
	 * @param mixed $instance Instance.
45
	 * @return void
46
	 */
47
	public function widget( $args, $instance ) {
48
49
		$iframe_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : '';
50
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
51
		$screenname = ! empty( $instance['screenname'] ) ? $instance['screenname'] : '';
52
		$region = ! empty( $instance['region'] ) ? $instance['region'] : '';
53
		$textcolor = ! empty( $instance['textcolor'] ) ? $instance['textcolor'] : '';
54
55
		echo $args['before_widget'];
56
57
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
58
59
60
61
		$zillow_widgets = new ZillowWidgets();
62
63
		return $zillow_widgets->get_mortgage_rate_table_widget( $iframe_id, $textcolor, $screenname, $region );
64
65
		echo $args['after_widget'];
0 ignored issues
show
Unused Code introduced by
echo $args['after_widget']; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
66
	}
67
68
	/**
69
	 * Form function.
70
	 *
71
	 * @access public
72
	 * @param mixed $instance Instance.
73
	 * @return void
74
	 */
75
	public function form( $instance ) {
76
77
		// Set default values.
78
		$instance = wp_parse_args( (array) $instance, array(
79
			'title' => '',
80
			'screenname' => '',
81
			'region' => '',
82
			'textcolor' => '000000',
83
			'zuid' => '',
84
		));
85
86
		// Retrieve an existing value from the database.
87
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
88
		$screenname = ! empty( $instance['screenname'] ) ? $instance['screenname'] : '';
89
		$textcolor = ! empty( $instance['textcolor'] ) ? $instance['textcolor'] : '';
90
		$region = ! empty( $instance['region'] ) ? $instance['region'] : '';
91
		$zuid = ! empty( $instance['zuid'] ) ? $instance['zuid'] : '';
0 ignored issues
show
Unused Code introduced by
$zuid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
93
		// Title.
94
		echo '<p>';
95
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
96
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
97
		echo '</p>';
98
99
		// Zillow Screenname.
100
		echo '<p>';
101
		echo '	<label for="' . $this->get_field_id( 'screenname' ) . '" class="title-label">' . __( 'Zillow Screenname:', 're-pro' ) . '</label>';
102
		echo '	<input id="' . $this->get_field_id( 'screenname' ) . '" name="' . $this->get_field_name( 'screenname' ) . '" value="' . $screenname  . '" class="widefat">';
103
		echo '</p>';
104
105
		// Select a State.
106
		echo '<p>';
107
		echo '	<label for="' . $this->get_field_id( 'region' ) . '" class="title-label">' . __( 'Region:', 're-pro' ) . '</label>';
108
		echo '<select id="' . $this->get_field_id( 'region' ) . '" name="' . $this->get_field_name( 'region' ) . '" class="widefat">';
109
	    echo '<option value="102001"'. selected( $region, 102001 ) .'>'. __( 'United States', 're-pro' ) .'</option>';
110
	    echo '<option value="3"'. selected( $region, 3 ) .'>Alaska</option>';
111
	    echo '<option value="4"'. selected( $region, 4 ) .'>Alabama</option>';
112
	    echo '<option value="6"'. selected( $region, 6 ) .'>Arkansas</option>';
113
	    echo '<option value="8"'. selected( $region, 8 ) .'>Arizona</option>';
114
	    echo '<option value="9"'. selected( $region, 9 ) .'>California</option>';
115
	    echo '<option value="10"'. selected( $region, 10 ) .'>Colorado</option>';
116
	    echo '<option value="11"'. selected( $region, 11 ) .'>Connecticut</option>';
117
	    echo '<option value="13"'. selected( $region, 13 ) .'>Delaware</option>';
118
	    echo '<option value="14"'. selected( $region, 14 ) .'>Florida</option>';
119
	    echo '<option value="16"'. selected( $region, 16 ) .'>Georgia</option>';
120
	    echo '<option value="18"'. selected( $region, 18 ) .'>Hawaii</option>';
121
	    echo '<option value="19"'. selected( $region, 19 ) .'>Iowa</option>';
122
	    echo '<option value="20"'. selected( $region, 20 ) .'>Idaho</option>';
123
	    echo '<option value="21"'. selected( $region, 21 ) .'>Illinois</option>';
124
	    echo '<option value="22"'. selected( $region, 22 ) .'>Indiana</option>';
125
	    echo '<option value="23"'. selected( $region, 23 ) .'>Kansas</option>';
126
	    echo '<option value="24"'. selected( $region, 24 ) .'>Kentucky</option>';
127
	    echo '<option value="25"'. selected( $region, 25 ) .'>Louisiana</option>';
128
	    echo '<option value="26"'. selected( $region, 26 ) .'>Massachusetts</option>';
129
	    echo '<option value="27"'. selected( $region, 27 ) .'>Maryland</option>';
130
	    echo '<option value="28"'. selected( $region, 28 ) .'>Maine</option>';
131
	    echo '<option value="30"'. selected( $region, 30 ) .'>Michigan</option>';
132
	    echo '<option value="31"'. selected( $region, 31 ) .'>Minnesota</option>';
133
	    echo '<option value="32"'. selected( $region, 32 ) .'>Missouri</option>';
134
	    echo '<option value="34"'. selected( $region, 34 ) .'>Mississippi</option>';
135
	    echo '<option value="35"'. selected( $region, 35 ) .'>Montana</option>';
136
	    echo '<option value="36"'. selected( $region, 36 ) .'>North Carolina</option>';
137
	    echo '<option value="37"'. selected( $region, 37 ) .'>North Dakota</option>';
138
	    echo '<option value="38"'. selected( $region, 38 ) .'>Nebraska</option>';
139
	    echo '<option value="39"'. selected( $region, 39 ) .'>New Hampshire</option>';
140
	    echo '<option value="40"'. selected( $region, 40 ) .'>New Jersey</option>';
141
	    echo '<option value="41"'. selected( $region, 41 ) .'>New Mexico</option>';
142
	    echo '<option value="42"'. selected( $region, 42 ) .'>Nevada</option>';
143
	    echo '<option value="43"'. selected( $region, 43 ) .'>New York</option>';
144
	    echo '<option value="44"'. selected( $region, 44 ) .'>Ohio</option>';
145
	    echo '<option value="45"'. selected( $region, 45 ) .'>Oklahoma</option>';
146
	    echo '<option value="46"'. selected( $region, 46 ) .'>Oregon</option>';
147
	    echo '<option value="47"'. selected( $region, 47 ) .'>Pennsylvania</option>';
148
	    echo '<option value="48"'. selected( $region, 48 ) .'>Puerto Rico</option>';
149
	    echo '<option value="50"'. selected( $region, 50 ) .'>Rhode Island</option>';
150
	    echo '<option value="51"'. selected( $region, 51 ) .'>South Carolina</option>';
151
	    echo '<option value="52"'. selected( $region, 52 ) .'>South Dakota</option>';
152
	    echo '<option value="53"'. selected( $region, 53 ) .'>Tennessee</option>';
153
	    echo '<option value="54"'. selected( $region, 54 ) .'>Texas</option>';
154
	    echo '<option value="55"'. selected( $region, 55 ) .'>Utah</option>';
155
	    echo '<option value="56"'. selected( $region, 56 ) .'>Virginia</option>';
156
	    echo '<option value="57"'. selected( $region, 57 ) .'>Virgin Islands</option>';
157
	    echo '<option value="58"'. selected( $region, 58 ) .'>Vermont</option>';
158
	    echo '<option value="59"'. selected( $region, 59 ) .'>Washington</option>';
159
	    echo '<option value="12"'. selected( $region, 12 ) .'>Washington, DC</option>';
160
	    echo '<option value="60"'. selected( $region, 60 ) .'>Wisconsin</option>';
161
	    echo '<option value="61"'. selected( $region, 61 ) .'>West Virginia</option>';
162
	    echo '<option value="62"'. selected( $region, 62 ) .'>Wyoming</option>';
163
		echo '</select>';
164
		echo '</p>';
165
166
		// Text Color
167
		echo '<p>';
168
		echo '	<label for="' . $this->get_field_id( 'textcolor' ) . '" class="title-label">' . __( 'Text Color:', 're-pro' ) . '</label>';
169
		echo '	<input id="' . $this->get_field_id( 'textcolor' ) . '" name="' . $this->get_field_name( 'textcolor' ) . '" value="' . $textcolor  . '" class="widefat">';
170
		echo '</p>';
171
172
	}
173
174
	/**
175
	 * Update Widget Instance.
176
	 *
177
	 * @access public
178
	 * @param mixed $new_instance New Instance.
179
	 * @param mixed $old_instance Old Instance.
180
	 * @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...
181
	 */
182
	public function update( $new_instance, $old_instance ) {
183
184
		$instance = $old_instance;
185
186
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
187
		$instance['screenname'] = ! empty( $new_instance['screenname'] ) ? strip_tags( $new_instance['screenname'] ) : '';
188
		$instance['region'] = ! empty( $new_instance['region'] ) ? strip_tags( $new_instance['region'] ) : '';
189
		$instance['textcolor'] = ! empty( $new_instance['textcolor'] ) ? strip_tags( $new_instance['textcolor'] ) : '';
190
191
		return $instance;
192
	}
193
}
194
195
/**
196
 * Register Zillow Review Widget.
197
 *
198
 * @access public
199
 * @return void
200
 */
201
/*function repro_zillow_mortgage_rate_widget() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
203
	register_widget( 'ZillowMortgageRateWidget' );
204
}
205
add_action( 'widgets_init', 'repro_zillow_mortgage_rate_widget' );*/
206