RealtorLogoWidget   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 114
loc 114
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
A widget() 18 18 2
B form() 35 35 3
A update() 10 10 3

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 10 and the first side effect is on line 135.

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
4
5
/**
6
 * RealtorLogoWidget class.
7
 *
8
 * @extends WP_Widget
9
 */
10 View Code Duplication
class RealtorLogoWidget 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...
11
12
13
	/**
14
	 * __construct function.
15
	 *
16
	 * @access public
17
	 * @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...
18
	 */
19
	public function __construct() {
20
21
		parent::__construct(
22
			'realtor_logo',
23
			__( 'Realtor Logo', 're-pro' ),
24
			array(
25
				'description' => __( 'Display the Realtor Logo.', 're-pro' ),
26
				'classname'   => 're-pro realtor',
27
			)
28
		);
29
30
	}
31
32
33
	/**
34
	 * Display Realtor Logo Widget.
35
	 *
36
	 * @access public
37
	 * @param mixed $args
38
	 * @param mixed $instance
39
	 * @return void
40
	 */
41
	public function widget( $args, $instance ) {
42
43
		if ( ! empty( $instance['title'] ) ) {
44
			$title = $instance['title'];
45
		} else {
46
			$title = '';
47
		}
48
		$logo_size = $instance['logo_size'];
49
50
		echo $args['before_widget'];
51
52
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
53
54
		echo '<img src="'. esc_url( plugins_url( '../../assets/images/realtor/realtor.svg', __FILE__ ) ) .'" alt="'. __( 'Realtor', 're-pro' ) .'" height="' . $logo_size . '" width="' . $logo_size . '" class="re-pro realtor">';
55
56
		echo $args['after_widget'];
57
58
	}
59
60
61
	/**
62
	 * Widget Form.
63
	 *
64
	 * @access public
65
	 * @param mixed $instance
66
	 * @return void
67
	 */
68
	public function form( $instance ) {
69
70
		// Set default values
71
		$instance = wp_parse_args( (array) $instance, array(
72
			'logo-size' => '32',
73
			'title' => '',
74
		) );
75
76
		// Retrieve an existing value from the database
77
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
78
		$logo_size = ! empty( $instance['logo_size'] ) ? $instance['logo_size'] : '';
79
80
		// Form fields
81
		echo '<p>';
82
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile', 're-pro' ) . '</label>';
83
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
84
		echo '</p>';
85
86
		echo '<p>';
87
		echo '	<label for="' . $this->get_field_id( 'logo_size' ) . '" class="logo-size-label">' . __( 'Logo Size', 're-pro' ) . '</label>';
88
		echo '	<select id="' . $this->get_field_id( 'logo_size' ) . '" name="' . $this->get_field_name( 'logo_size' ) . '" class="widefat">';
89
		echo '		<option value="" ' . selected( $logo_size, '', false ) . '> ' . __( 'Choose', 're-pro' ) . '</option>';
90
		echo '		<option value="512" ' . selected( $logo_size, '512', false ) . '> ' . __( '512px', 're-pro' ) . '</option>';
91
		echo '		<option value="256" ' . selected( $logo_size, '256', false ) . '> ' . __( '256px', 're-pro' ) . '</option>';
92
		echo '		<option value="128" ' . selected( $logo_size, '128', false ) . '> ' . __( '128px', 're-pro' ) . '</option>';
93
		echo '		<option value="64" ' . selected( $logo_size, '64', false ) . '> ' . __( '64px', 're-pro' ) . '</option>';
94
		echo '		<option value="32" ' . selected( $logo_size, '32', false ) . '> ' . __( '32px', 're-pro' ) . '</option>';
95
		echo '		<option value="24" ' . selected( $logo_size, '24', false ) . '> ' . __( '24px', 're-pro' ) . '</option>';
96
		echo '		<option value="16" ' . selected( $logo_size, '16', false ) . '> ' . __( '16px', 're-pro' ) . '</option>';
97
		echo '		<option value="14" ' . selected( $logo_size, '14', false ) . '> ' . __( '14px', 're-pro' ) . '</option>';
98
		echo '	</select>';
99
		echo '	<span class="description">' . __( 'Choose the size of the logo you want to display.', 're-pro' ) . '</span>';
100
		echo '</p>';
101
102
	}
103
104
105
	/**
106
	 * Update Widget.
107
	 *
108
	 * @access public
109
	 * @param mixed $new_instance
110
	 * @param mixed $old_instance
111
	 * @return void
112
	 */
113
	public function update( $new_instance, $old_instance ) {
114
115
		$instance = $old_instance;
116
117
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
118
		$instance['logo_size'] = ! empty( $new_instance['logo_size'] ) ? strip_tags( $new_instance['logo_size'] ) : '';
119
120
		return $instance;
121
122
	}
123
}
124
125
126
/**
127
 * realtor Widget.
128
 *
129
 * @access public
130
 * @return void
131
 */
132
function repro_register_realtorlogo_widget() {
133
	register_widget( 'RealtorLogoWidget' );
134
}
135
add_action( 'widgets_init', 'repro_register_realtorlogo_widget' );
136