HomeFinderOpenHouse   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 86
loc 86
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
A widget() 15 15 3
A form() 17 17 2
A update() 8 8 2

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 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
 * HomeFinder Open House Widget (http://www.homefinder.com/widgets/)
4
 *
5
 * @package RE-PRO
6
 */
7
8
	/* Exit if accessed directly. */
9
if ( ! defined( 'ABSPATH' ) ) { exit; }
10
11
/**
12
 * HomeFinderOpenHouse class.
13
 *
14
 * @extends WP_Widget
15
 */
16 View Code Duplication
class HomeFinderOpenHouse 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...
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
			'homefinder_open_house',
28
			__( 'HomeFinder Open House', 're-pro' ),
29
			array(
30
				'description' => __( 'Display a open house search box from HomeFinder.com', 're-pro' ),
31
				'classname'   => 're-pro re-pro-widget homefinder-widget homefinder-open-house',
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
		$widget_id = ! empty( $args['widget_id'] ) ? $args['widget_id'] : '';
0 ignored issues
show
Unused Code introduced by
$widget_id 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...
47
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
48
49
		echo $args['before_widget'];
50
51
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
52
53
		$homefinder_widgets = new HomeFinderWidgets();
54
55
		$homefinder_widgets->get_open_house_widget();
56
57
		echo $args['after_widget'];
58
	}
59
60
	/**
61
	 * Form function.
62
	 *
63
	 * @access public
64
	 * @param mixed $instance Instance.
65
	 * @return void
66
	 */
67
	public function form( $instance ) {
68
69
		// Set default values.
70
		$instance = wp_parse_args( (array) $instance, array(
71
			'title' => '',
72
		) );
73
74
		// Retrieve an existing value from the database.
75
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
76
77
		// Title.
78
		echo '<p>';
79
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
80
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
81
		echo '</p>';
82
83
	}
84
85
	/**
86
	 * Update Widget Instance.
87
	 *
88
	 * @access public
89
	 * @param mixed $new_instance New Instance.
90
	 * @param mixed $old_instance Old Instance.
91
	 * @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...
92
	 */
93
	public function update( $new_instance, $old_instance ) {
94
95
		$instance = $old_instance;
96
97
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
98
99
		return $instance;
100
	}
101
}
102
103
/**
104
 * Register HomeFinder.com Open House Widget.
105
 *
106
 * @access public
107
 * @return void
108
 */
109
function repro_homefinder_open_house() {
110
	if ( ! is_ssl() ) {
111
		register_widget( 'HomeFinderOpenHouse' );
112
	}
113
}
114
add_action( 'widgets_init', 'repro_homefinder_open_house' );
115