StreetAdvisor_Details   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 169
Duplicated Lines 26.04 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 44
loc 169
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
C widget() 0 65 8
B form() 44 44 5
B update() 0 12 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 11 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
 * StreetAdvisor_Details class.
8
 *
9
 * @extends WP_Widget
10
 */
11
class StreetAdvisor_Details 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...
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
			'streetadvisor-details',
23
			__( 'Street Advisor - Location Details', 're-pro' ),
24
			array(
25
				'description' => __( 'Street Advisor Location Details', 're-pro' ),
26
			)
27
		);
28
29
	}
30
31
	/**
32
	 * widget function.
33
	 *
34
	 * @access public
35
	 * @param mixed $args
36
	 * @param mixed $instance
37
	 * @return void
38
	 */
39
	public function widget( $args, $instance ) {
40
41
		// Retrieve an existing value from the database
42
		$latitude = !empty( $instance['latitude'] ) ? $instance['latitude'] : '';
43
		$longitude = !empty( $instance['longitude'] ) ? $instance['longitude'] : '';
44
		$level = !empty( $instance['level'] ) ? $instance['level'] : '';
45
		$title = !empty( $instance['title'] ) ? $instance['title'] : '';
46
47
		$streetadvisor = new StreetAdvisorAPI( 'b08f6473-8dee-41c3-9a3e-b32f335e9d2d' );
48
49
		$location = $streetadvisor->get_location_data( $latitude, $longitude, $level );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $location is correct as $streetadvisor->get_loca...de, $longitude, $level) (which targets StreetAdvisorAPI::get_location_data()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
51
		$name = $location['Location']['Name'];
52
		$ranking_description = $location['Location']['RankingDescription'];
53
		$score = $location['Location']['Score'];
54
		$photo_url = esc_url( apply_filters( 'jetpack_photon_url', $location['Location']['PhotoUrl'] ) );
55
		$streetadvisor_url = esc_url( $location['Location']['Url'] ); // Lets force HTTPS for this please !
56
57
		$recommendations_greatfor = $location['Recommendations']['GreatFor'];
58
		$recommendations_notgreatfor = $location['Recommendations']['NotGreatFor'];
59
		$recommendations_wholiveshere = $location['Recommendations']['WhoLivesHere'];
60
61
62
		echo $args['before_widget'];
63
64
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
65
66
		echo '<style>.sa-column{width:100%;float:left;display:inline-block;}.streetadvisor-list{margin: 10px 0;}.streetadvisor-list li {list-style-type:circle !important;margin:0;padding:0;list-style-position: inside;}</style>';
67
68
		echo '<img id="" class="" src="'.$photo_url.'" srcset="" sizes="" alt="'. $name .'" height="" width="">';
69
70
71
		echo '<p>' . $name . ' has a score of <strong>' . $score . ' out of 10</strong> on <a href="'. $streetadvisor_url .'" rel="nofollow">StreetAdvisor</a>. '. $ranking_description .'</p>';
72
73
		echo '<div class="sa-column">';
74
75
		echo '<strong>Great For:</strong>';
76
		echo '<ul class="re-pro streetadvisor-list streetadvisor-great-for">';
77
		foreach($recommendations_greatfor as $greatfor_item) {
78
			echo '<li>' . $greatfor_item . '</li>';
79
		}
80
		echo '</ul>';
81
		echo '</div>';
82
83
		echo '<div class="sa-column">';
84
		echo '<strong>Not Great For:</strong>';
85
		echo '<ul class="re-pro streetadvisor-list streetadvisor-not-great-for">';
86
		foreach($recommendations_notgreatfor as $notgreatfor_item) {
87
			echo '<li>' . $notgreatfor_item . '</li>';
88
		}
89
		echo '</ul>';
90
		echo '</div>';
91
92
		echo '<div class="sa-column">';
93
		echo '<strong>Who Lives Here:</strong>';
94
		echo '<ul class="re-pro streetadvisor-list streetadvisor-who-lives-here">';
95
		foreach($recommendations_wholiveshere as $wholiveshere_item) {
96
			echo '<li>' . $wholiveshere_item . '</li>';
97
		}
98
		echo '</ul>';
99
		echo '</div>';
100
101
		echo $args['after_widget'];
102
103
	}
104
105
	/**
106
	 * form function.
107
	 *
108
	 * @access public
109
	 * @param mixed $instance
110
	 * @return void
111
	 */
112 View Code Duplication
	public function form( $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...
113
114
		// Set default values
115
		$instance = wp_parse_args( (array) $instance, array(
116
			'latitude' => '',
117
			'longitude' => '',
118
			'level' => '',
119
			'title' => '',
120
		) );
121
122
		// Retrieve an existing value from the database
123
		$latitude = !empty( $instance['latitude'] ) ? $instance['latitude'] : '';
124
		$longitude = !empty( $instance['longitude'] ) ? $instance['longitude'] : '';
125
		$level = !empty( $instance['level'] ) ? $instance['level'] : '';
126
		$title = !empty( $instance['title'] ) ? $instance['title'] : '';
127
128
		// Form fields
129
		echo '<p>';
130
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title_label">' . __( 'Title', 're-pro' ) . '</label>';
131
		echo '	<input type="text" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $title ) . '">';
132
		echo '	<span class="description">' . __( 'Title', 're-pro' ) . '</span>';
133
		echo '</p>';
134
135
		echo '<p>';
136
		echo '	<label for="' . $this->get_field_id( 'latitude' ) . '" class="latitude_label">' . __( 'Latitude', 're-pro' ) . '</label>';
137
		echo '	<input type="text" id="' . $this->get_field_id( 'latitude' ) . '" name="' . $this->get_field_name( 'latitude' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $latitude ) . '">';
138
		echo '	<span class="description">' . __( 'Latitude', 're-pro' ) . '</span>';
139
		echo '</p>';
140
141
		echo '<p>';
142
		echo '	<label for="' . $this->get_field_id( 'longitude' ) . '" class="longitude_label">' . __( 'Longitude', 're-pro' ) . '</label>';
143
		echo '	<input type="text" id="' . $this->get_field_id( 'longitude' ) . '" name="' . $this->get_field_name( 'longitude' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $longitude ) . '">';
144
		echo '	<span class="description">' . __( 'Longitude', 're-pro' ) . '</span>';
145
		echo '</p>';
146
147
		echo '<p>';
148
		echo '	<label for="' . $this->get_field_id( 'level' ) . '" class="level_label">' . __( 'Level', 're-pro' ) . '</label>';
149
		echo '	<input type="number" id="' . $this->get_field_id( 'level' ) . '" name="' . $this->get_field_name( 'level' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $level ) . '" step="1" min="0" max="50">';
150
		echo '	<span class="description">' . __( 'Level', 're-pro' ) . '</span>';
151
		echo '</p>';
152
153
154
155
	}
156
157
158
	/**
159
	 * update function.
160
	 *
161
	 * @access public
162
	 * @param mixed $new_instance
163
	 * @param mixed $old_instance
164
	 * @return void
165
	 */
166
	public function update( $new_instance, $old_instance ) {
167
168
		$instance = $old_instance;
169
170
		$instance['latitude'] = !empty( $new_instance['latitude'] ) ? strip_tags( $new_instance['latitude'] ) : '';
171
		$instance['longitude'] = !empty( $new_instance['longitude'] ) ? strip_tags( $new_instance['longitude'] ) : '';
172
		$instance['level'] = !empty( $new_instance['level'] ) ? strip_tags( $new_instance['level'] ) : '';
173
		$instance['title'] = !empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
174
175
		return $instance;
176
177
	}
178
179
}
180
181
/**
182
 * register_widgets function.
183
 *
184
 * @access public
185
 * @return void
186
 */
187
function register_widgets() {
188
	register_widget( 'StreetAdvisor_Details' );
189
}
190
add_action( 'widgets_init', 'register_widgets' );
191