ZillowNewestHomesWidget   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 106
loc 106
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
B widget() 20 20 6
B form() 27 27 3
B update() 11 11 5

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
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 Newest For Sale Homes Widget (http://www.zillow.com/webtools/widgets/NewestForSaleHomes.htm)
8
 *
9
 * @package RE-PRO
10
 */
11
12
/**
13
 * ZillowNewestHomesWidget class.
14
 *
15
 * @extends WP_Widget
16
 */
17 View Code Duplication
class ZillowNewestHomesWidget extends WP_Widget {
0 ignored issues
show
Duplication introduced by
This class 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...
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_newest_forsale_homes_widget',
30
			__( 'Zillow Newest For Sale Homes', 're-pro' ),
31
			array(
32
				'description' => __( 'Display the most recent for sale homes from Zillow.', 're-pro' ),
33
				'classname'   => 're-pro re-pro-widget zillow-widget zillow-widget-newest-homes',
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
		$type = ! empty( $instance['type'] ) ? $instance['type'] : '';
0 ignored issues
show
Unused Code introduced by
$type 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...
52
		$size = ! empty( $instance['size'] ) ? $instance['size'] : '';
0 ignored issues
show
Unused Code introduced by
$size 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...
53
		$location = ! empty( $instance['location'] ) ? $instance['location'] : '';
54
55
		echo $args['before_widget'];
56
57
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
58
59
60
		$zillow_widgets = new ZillowWidgets();
61
62
		$zillow_widgets->get_newest_forsale_homes_widget( $iframe_id, $location, 'iframe', 'wide' );
63
64
65
		echo $args['after_widget'];
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
			'type' => 'iframe',
81
			'size' => 'wide',
82
			'location' => 'El Segundo, CA',
83
		));
84
85
		// Retrieve an existing value from the database.
86
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
87
		$location = ! empty( $instance['location'] ) ? $instance['location'] : '';
88
89
		// Title.
90
		echo '<p>';
91
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
92
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
93
		echo '</p>';
94
95
		// Location.
96
		echo '<p>';
97
		echo '	<label for="' . $this->get_field_id( 'location' ) . '" class="title-label">' . __( 'Location:', 're-pro' ) . '</label>';
98
		echo '	<input id="' . $this->get_field_id( 'location' ) . '" placeholder="El Segundo, CA" name="' . $this->get_field_name( 'location' ) . '" value="' . $location  . '" class="widefat">';
99
		echo '</p>';
100
101
	}
102
103
	/**
104
	 * Update Widget Instance.
105
	 *
106
	 * @access public
107
	 * @param mixed $new_instance New Instance.
108
	 * @param mixed $old_instance Old Instance.
109
	 * @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...
110
	 */
111
	public function update( $new_instance, $old_instance ) {
112
113
		$instance = $old_instance;
114
115
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
116
		$instance['type'] = ! empty( $new_instance['type'] ) ? strip_tags( $new_instance['type'] ) : '';
117
		$instance['size'] = ! empty( $new_instance['size'] ) ? strip_tags( $new_instance['size'] ) : '';
118
		$instance['location'] = ! empty( $new_instance['location'] ) ? strip_tags( $new_instance['location'] ) : '';
119
120
		return $instance;
121
	}
122
}
123
124
/**
125
 * Register Zillow Affordability Calculator Widget.
126
 *
127
 * @access public
128
 * @return void
129
 */
130
function repro_zillow_newest_homes_widget() {
131
132
	register_widget( 'ZillowNewestHomesWidget' );
133
}
134
add_action( 'widgets_init', 'repro_zillow_newest_homes_widget' );
135