StreetAdvisor_Reviews::update()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 16
nop 2
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
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_Reviews 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-reviews',
23
			__( 'Street Advisor - Location Reviews', 're-pro' ),
24
			array(
25
				'description' => __( 'Street Advisor Location Reviews', '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
$reviews = $streetadvisor->get_location_reviews( $latitude, $longitude, $level );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $reviews is correct as $streetadvisor->get_loca...de, $longitude, $level) (which targets StreetAdvisorAPI::get_location_reviews()) 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
// var_dump($reviews);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
/*
53
$name = $location['Location']['Name'];
54
$ranking_description = $location['Location']['RankingDescription'];
55
$score = $location['Location']['Score'];
56
$photo_url = esc_url( apply_filters( 'jetpack_photon_url', $location['Location']['PhotoUrl'] ) );
57
$streetadvisor_url = esc_url( $location['Location']['Url'] ); // Lets force HTTPS for this please !
58
59
$recommendations_greatfor = $location['Recommendations']['GreatFor'];
60
$recommendations_notgreatfor = $location['Recommendations']['NotGreatFor'];
61
$recommendations_wholiveshere = $location['Recommendations']['WhoLivesHere'];
62
*/
63
64
65
echo $args['before_widget'];
66
67
echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
68
69
foreach ($reviews['Items'] as $review) {
70
71
	// var_dump($review);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
73
	$title = $review['Title'];
74
	$content = $review['Content'];
75
	$date = $review['Date'];
76
	$rating = $review['Rating'];
77
	$legal = $review['Legal'];
0 ignored issues
show
Unused Code introduced by
$legal 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...
78
	$url = $review['Url'];
0 ignored issues
show
Unused Code introduced by
$url 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...
79
	$user_displayname = $review['User']['DisplayName'];
80
	$user_avatar_url = $review['User']['AvatarUrl'];
0 ignored issues
show
Unused Code introduced by
$user_avatar_url 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...
81
82
	echo '<div class="review" itemprop="review" itemscope itemtype="https://schema.org/Review">';
83
84
	// echo '<img src="'. $user_avatar_url .'">';
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
	echo '<span itemprop="reviewRating">'.$rating.'</span> stars - <strong>"<span itemprop="name">' . $title . '</span>"</strong>
86
  <br /> by <span itemprop="author">'.$user_displayname.'</span><br />
87
  Written on <meta itemprop="datePublished" content="' .$date .'">'.$date .'
88
  <br /><span itemprop="reviewBody">'.$content.'</span>';
89
90
  // echo '<small>'.$legal . '</small>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92
	echo '</div>';
93
94
95
96
97
}
98
99
100
echo $args['after_widget'];
101
102
	}
103
104
	/**
105
	 * form function.
106
	 *
107
	 * @access public
108
	 * @param mixed $instance
109
	 * @return void
110
	 */
111 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...
112
113
		// Set default values
114
		$instance = wp_parse_args( (array) $instance, array(
115
			'latitude' => '',
116
			'longitude' => '',
117
			'level' => '',
118
			'title' => '',
119
		) );
120
121
		// Retrieve an existing value from the database
122
		$latitude = !empty( $instance['latitude'] ) ? $instance['latitude'] : '';
123
		$longitude = !empty( $instance['longitude'] ) ? $instance['longitude'] : '';
124
		$level = !empty( $instance['level'] ) ? $instance['level'] : '';
125
		$title = !empty( $instance['title'] ) ? $instance['title'] : '';
126
127
		// Form fields
128
		echo '<p>';
129
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title_label">' . __( 'Title', 're-pro' ) . '</label>';
130
		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 ) . '">';
131
		echo '	<span class="description">' . __( 'Title', 're-pro' ) . '</span>';
132
		echo '</p>';
133
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 ) . '">';
150
		echo '	<span class="description">' . __( 'Level', 're-pro' ) . '</span>';
151
		echo '</p>';
152
153
154
	}
155
156
157
	/**
158
	 * update function.
159
	 *
160
	 * @access public
161
	 * @param mixed $new_instance
162
	 * @param mixed $old_instance
163
	 * @return void
164
	 */
165
	public function update( $new_instance, $old_instance ) {
166
167
		$instance = $old_instance;
168
169
		$instance['latitude'] = !empty( $new_instance['latitude'] ) ? strip_tags( $new_instance['latitude'] ) : '';
170
		$instance['longitude'] = !empty( $new_instance['longitude'] ) ? strip_tags( $new_instance['longitude'] ) : '';
171
		$instance['level'] = !empty( $new_instance['level'] ) ? strip_tags( $new_instance['level'] ) : '';
172
		$instance['title'] = !empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
173
174
		return $instance;
175
176
	}
177
178
}
179
180
/**
181
 * register_widgets function.
182
 *
183
 * @access public
184
 * @return void
185
 */
186
function streetadvisor_register_widgets() {
187
	register_widget( 'StreetAdvisor_Reviews' );
188
}
189
add_action( 'widgets_init', 'streetadvisor_register_widgets' );
190