HomeFinderAdvertiserDirectory   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 24.53 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A widget() 16 16 4
B form() 0 34 4
A update() 10 10 4

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 Advetiser Directory 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
 * HomeFinderAdvertiserDirectory class.
13
 *
14
 * @extends WP_Widget
15
 */
16
class HomeFinderAdvertiserDirectory 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
			'homefinder_advertiser_directory',
28
			__( 'HomeFinder Advertiser Directory', 're-pro' ),
29
			array(
30
				'description' => __( "Display your advertiser's contact information from HomeFinder.com", 're-pro' ),
31
				'classname'   => 're-pro re-pro-widget homefinder-widget homefinder-advertiser-directory',
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 View Code Duplication
	public function widget( $args, $instance ) {
0 ignored issues
show
Duplication introduced by
This method 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...
45
46
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
47
		$affiliate = ! empty( $instance['affiliate'] ) ? $instance['affiliate'] : '';
0 ignored issues
show
Unused Code introduced by
$affiliate 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...
48
		$count = ! empty( $instance['count'] ) ? $instance['count'] : '';
0 ignored issues
show
Unused Code introduced by
$count 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...
49
50
		echo $args['before_widget'];
51
52
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
53
54
		$homefinder_widgets = new HomeFinderWidgets();
55
56
		$homefinder_widgets->get_affiliates_widget( 'directory', $instance );
57
58
		echo $args['after_widget'];
59
	}
60
61
	/**
62
	 * Form function.
63
	 *
64
	 * @access public
65
	 * @param mixed $instance Instance.
66
	 * @return void
67
	 */
68
	public function form( $instance ) {
69
70
		// Set default values.
71
		$instance = wp_parse_args( (array) $instance, array(
72
			'title' => '',
73
			'affiliate' => '',
74
			'count' => '6',
75
		) );
76
77
		// Retrieve an existing value from the database.
78
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
79
		$affiliate = ! empty( $instance['affiliate'] ) ? $instance['affiliate'] : '';
80
		$count = ! empty( $instance['count'] ) ? $instance['count'] : '';
81
82
		// Title.
83
		echo '<p>';
84
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
85
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
86
		echo '</p>';
87
88
		// Affiliate Profile Name.
89
		echo '<p>';
90
		echo '	<label for="' . $this->get_field_id( 'affiliate' ) . '" class="title-label">' . __( 'Affiliate Profile Name:', 're-pro' ) . '</label>';
91
		echo '	<input id="' . $this->get_field_id( 'affiliate' ) . '" name="' . $this->get_field_name( 'affiliate' ) . '" value="' . $affiliate . '" class="widefat">';
92
		echo '</p>';
93
94
		// Max Count.
95
		echo '<p>';
96
		echo '	<label for="' . $this->get_field_id( 'count' ) . '" class="title-label">' . __( 'Display Count:', 're-pro' ) . '</label>';
97
		echo '	<input type="number" id="' . $this->get_field_id( 'count' ) . '" name="' . $this->get_field_name( 'count' ) . '" min="1" max="50" value="' . $count . '">';
98
		echo '	<span class="description">' . __( 'Max Count: 50', 're-pro' ) . '</span>';
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 View Code Duplication
	public function update( $new_instance, $old_instance ) {
0 ignored issues
show
Duplication introduced by
This method 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...
112
113
		$instance = $old_instance;
114
115
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
116
		$instance['affiliate'] = ! empty( $new_instance['affiliate'] ) ? strip_tags( $new_instance['affiliate'] ) : '';
117
		$instance['count'] = ! empty( $new_instance['count'] ) ? strip_tags( $new_instance['count'] ) : '';
118
119
		return $instance;
120
	}
121
}
122
123
/**
124
 * Register HomeFinder.com Homes for Sale Widget.
125
 *
126
 * @access public
127
 * @return void
128
 */
129
function repro_homefinder_advertiser_directory() {
130
	if ( ! is_ssl() ) {
131
		register_widget( 'HomeFinderAdvertiserDirectory' );
132
	}
133
}
134
add_action( 'widgets_init', 'repro_homefinder_advertiser_directory' );
135