inman-news-widget.php ➔ repro_inman_news_widget()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 17 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
 * Inman Feed Widget
8
 *
9
 * @package RE-PRO
10
 */
11
12
/**
13
 * InmanNewsWidget class.
14
 *
15
 * @extends WP_Widget
16
 */
17
class InmanNewsWidget 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...
18
19
20
	/**
21
	 * __construct function.
22
	 *
23
	 * @access public
24
	 * @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...
25
	 */
26
	public function __construct() {
27
28
		parent::__construct(
29
			'inman_news_widget',
30
			__( 'Inman News', 're-pro' ),
31
			array(
32
				'description' => __( 'Display the lastest news from Inman.', 're-pro' ),
33
				'classname'   => 're-pro re-pro-widget inman-widget inman-news-widget',
34
				'customize_selective_refresh' => true,
35
			)
36
		);
37
	}
38
39
	/**
40
	 * Widget function.
41
	 *
42
	 * @access public
43
	 * @param mixed $args Arguments.
44
	 * @param mixed $instance Instance.
45
	 * @return void
46
	 */
47
	public function widget( $args, $instance ) {
48
49
50
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
51
		$display_total = ! empty( $instance['display_total'] ) ? $instance['display_total'] : '3';
52
53
54
		echo $args['before_widget'];
55
56
		echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
57
58
59
		$rss =  fetch_feed( 'https://feeds.feedburner.com/inmannews' );
60
61
62
63
		if( ! is_wp_error( $rss ) ) {
64
65
			$max_items = $rss->get_item_quantity( $display_total );
0 ignored issues
show
Unused Code introduced by
$max_items 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...
66
			$rss_items = $rss->get_items( 0, $display_total );
67
68
			// echo $rss->get_title();
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...
69
			// echo '<small>'. $rss->get_description() . '.</small>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
70
71
			foreach( $rss_items as $item ) {
72
73
				echo '<div style="margin:20px 0;">';
74
				echo '<h4><a href="'. $item->get_link() .'" rel="nofollow" target="_blank">' . $item->get_title() . '</a></h4>';
75
				echo '</div>';
76
77
				echo $item->get_content();
78
79
			}
80
81
		} else {
82
			echo $rss->get_error_message();
83
		}
84
85
		echo $args['after_widget'];
86
	}
87
88
	/**
89
	 * Form function.
90
	 *
91
	 * @access public
92
	 * @param mixed $instance Instance.
93
	 * @return void
94
	 */
95
	public function form( $instance ) {
96
97
		// Set default values.
98
		$instance = wp_parse_args( (array) $instance, array(
99
			'title' => '',
100
101
		));
102
103
		// Retrieve an existing value from the database.
104
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
105
		$display_total = ! empty( $instance['display_total'] ) ? $instance['display_total'] : '';
0 ignored issues
show
Unused Code introduced by
$display_total 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...
106
107
		// Title.
108
		echo '<p>';
109
		echo '	<label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>';
110
		echo '	<input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title  . '" class="widefat">';
111
		echo '</p>';
112
113
		// TODO: Add field to select display total count, 1 - 20.
114
115
	}
116
117
	/**
118
	 * Update.
119
	 *
120
	 * @access public
121
	 * @param mixed $new_instance New Instance.
122
	 * @param mixed $old_instance Old Instance.
123
	 * @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...
124
	 */
125
	public function update( $new_instance, $old_instance ) {
126
127
		$instance = $old_instance;
128
129
		$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : '';
130
		$instance['display_total'] = ! empty( $new_instance['display_total'] ) ? strip_tags( $new_instance['display_total'] ) : '';
131
132
		return $instance;
133
	}
134
}
135
136
/**
137
 * Register Widget.
138
 *
139
 * @access public
140
 * @return void
141
 */
142
function repro_inman_news_widget() {
143
144
	register_widget( 'InmanNewsWidget' );
145
}
146
add_action( 'widgets_init', 'repro_inman_news_widget' );
147