greatschools_getschools   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B widget() 0 41 6
B form() 0 34 4
A update() 0 11 4
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 3 and the first side effect is on line 132.

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
class greatschools_getschools 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...
4
5
6
	/**
7
	 * __construct function.
8
	 *
9
	 * @access public
10
	 * @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...
11
	 */
12
	public function __construct() {
13
14
		parent::__construct(
15
			'getschools',
16
			__( 'GreatSchools - Get List of Schools', 're-pro' ),
17
			array(
18
				'description' => __( 'Get Schools', 're-pro' ),
19
			)
20
		);
21
22
	}
23
24
	/**
25
	 * widget function.
26
	 *
27
	 * @access public
28
	 * @param mixed $args
29
	 * @param mixed $instance
30
	 * @return void
31
	 */
32
	public function widget( $args, $instance ) {
33
34
	$greatschools_title = !empty( $instance['greatschools_title'] ) ? $instance['greatschools_title'] : '';
35
	$greatschools_state = !empty( $instance['greatschools_state'] ) ? $instance['greatschools_state'] : '';
36
	$greatschools_city = !empty( $instance['greatschools_city'] ) ? $instance['greatschools_city'] : '';
37
38
	// Call our API.
39
	$repro_settings = get_option( 'repro_settings' );
40
	$greatschools_apikey = $repro_settings['greatschools_apikey'];
41
42
	$greatschools = new GreatSchoolsAPI( $greatschools_apikey );
43
	$schools = $greatschools->get_schools( $greatschools_state,$greatschools_city );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $schools is correct as $greatschools->get_schoo...te, $greatschools_city) (which targets GreatSchoolsAPI::get_schools()) 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...
44
45
46
47
		echo $args['before_widget'];
48
49
		echo $args['before_title'] . esc_attr( $greatschools_title ) . $args['after_title'];
50
51
				// var_dump($schools);
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...
52
53
				foreach($schools as $school) {
0 ignored issues
show
Bug introduced by
The expression $schools of type null is not traversable.
Loading history...
54
55
					foreach($school as $school_item) {
56
57
						$name = $school_item['name'];
58
						$type =$school_item['type'];
0 ignored issues
show
Unused Code introduced by
$type 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...
59
						$grade_range = $school_item['gradeRange'];
0 ignored issues
show
Unused Code introduced by
$grade_range 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...
60
						$state = $school_item['state'];
0 ignored issues
show
Unused Code introduced by
$state 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...
61
						$city = $school_item['city'];
0 ignored issues
show
Unused Code introduced by
$city 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...
62
						$overview_link = $school_item['overviewLink'];
63
64
						echo '<li><a href="'.$overview_link.'" rel="nofollow">' . $name . '</a></li>';
65
					}
66
67
68
				}
69
70
		echo $args['after_widget'];
71
72
	}
73
74
	public function form( $instance ) {
75
76
		// Set default values
77
		$instance = wp_parse_args( (array) $instance, array(
78
			'greatschools_title' => '',
79
			'greatschools_state' => '',
80
			'greatschools_city' => '',
81
		) );
82
83
		// Retrieve an existing value from the database
84
		$greatschools_title = !empty( $instance['greatschools_title'] ) ? $instance['greatschools_title'] : '';
85
		$greatschools_state = !empty( $instance['greatschools_state'] ) ? $instance['greatschools_state'] : '';
86
		$greatschools_city = !empty( $instance['greatschools_city'] ) ? $instance['greatschools_city'] : '';
87
88
		// Form fields
89
		echo '<p>';
90
		echo '	<label for="' . $this->get_field_id( 'greatschools_title' ) . '" class="greatschools_title_label">' . __( 'Title', 're-pro' ) . '</label>';
91
		echo '	<input type="text" id="' . $this->get_field_id( 'greatschools_title' ) . '" name="' . $this->get_field_name( 'greatschools_title' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_title ) . '">';
92
		echo '	<span class="description">' . __( 'Title', 're-pro' ) . '</span>';
93
		echo '</p>';
94
95
		echo '<p>';
96
		echo '	<label for="' . $this->get_field_id( 'greatschools_state' ) . '" class="greatschools_state_label">' . __( 'State', 're-pro' ) . '</label>';
97
		echo '	<input type="text" id="' . $this->get_field_id( 'greatschools_state' ) . '" name="' . $this->get_field_name( 'greatschools_state' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_state ) . '">';
98
		echo '	<span class="description">' . __( 'State', 're-pro' ) . '</span>';
99
		echo '</p>';
100
101
		echo '<p>';
102
		echo '	<label for="' . $this->get_field_id( 'greatschools_city' ) . '" class="greatschools_city_label">' . __( 'City', 're-pro' ) . '</label>';
103
		echo '	<input type="text" id="' . $this->get_field_id( 'greatschools_city' ) . '" name="' . $this->get_field_name( 'greatschools_city' ) . '" class="widefat" placeholder="' . esc_attr__( '', 're-pro' ) . '" value="' . esc_attr( $greatschools_city ) . '">';
104
		echo '	<span class="description">' . __( 'City', 're-pro' ) . '</span>';
105
		echo '</p>';
106
107
	}
108
109
	public function update( $new_instance, $old_instance ) {
110
111
		$instance = $old_instance;
112
113
		$instance['greatschools_title'] = !empty( $new_instance['greatschools_title'] ) ? strip_tags( $new_instance['greatschools_title'] ) : '';
114
		$instance['greatschools_state'] = !empty( $new_instance['greatschools_state'] ) ? strip_tags( $new_instance['greatschools_state'] ) : '';
115
		$instance['greatschools_city'] = !empty( $new_instance['greatschools_city'] ) ? strip_tags( $new_instance['greatschools_city'] ) : '';
116
117
		return $instance;
118
119
	}
120
121
}
122
123
function greatschools_register_widgets() {
124
125
	$repro_settings = get_option( 'repro_settings' );
126
	$greatschools_apikey = $repro_settings['greatschools_apikey'];
127
128
	if ( ! empty( $greatschools_apikey) ) {
129
	register_widget( 'greatschools_getschools' );
130
	}
131
}
132
add_action( 'widgets_init', 'greatschools_register_widgets' );
133