Completed
Push — fix/widgets/flickr-more-sizes ( e0163b )
by
unknown
10:52
created

Jetpack_Flickr_Widget::widget()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 36
nc 24
nop 2
dl 0
loc 51
rs 5.6668
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Disable direct access/execution to/of the widget code.
4
 */
5
if ( ! defined( 'ABSPATH' ) ) {
6
	exit;
7
}
8
9
if ( ! class_exists( 'Jetpack_Flickr_Widget' ) ) {
10
	/**
11
	 * Flickr Widget
12
	 *
13
	 * Display your recent Flickr photos.
14
	 */
15
	class Jetpack_Flickr_Widget extends WP_Widget {
16
		/**
17
		 * Constructor.
18
		 */
19 View Code Duplication
		function __construct() {
20
			parent::__construct(
21
				'flickr',
22
				/** This filter is documented in modules/widgets/facebook-likebox.php */
23
				apply_filters( 'jetpack_widget_name', esc_html__( 'Flickr', 'jetpack' ) ),
24
				array(
25
					'description' => esc_html__( 'Display your recent Flickr photos.', 'jetpack' ),
26
					'customize_selective_refresh' => true,
27
				),
28
				array()
29
			);
30
31
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
32
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
33
			}
34
		}
35
36
		/**
37
		 * Enqueue style.
38
		 */
39
		function enqueue_style() {
40
			wp_enqueue_style( 'flickr-widget-style', plugins_url( 'flickr/style.css', __FILE__ ), array(), '20170405' );
41
		}
42
43
		/**
44
		 * Return an associative array of default values.
45
		 *
46
		 * These values are used in new widgets.
47
		 *
48
		 * @return array Default values for the widget options.
49
		 */
50
		public function defaults() {
51
			return array(
52
				'title'             => esc_html__( 'Flickr Photos', 'jetpack' ),
53
				'items'             => 3,
54
				'flickr_image_size' => 'thumbnail',
55
				'flickr_rss_url'    => ''
56
			);
57
		}
58
59
		/**
60
		 * Front-end display of the widget.
61
		 *
62
		 * @param array $args     Widget arguments.
63
		 * @param array $instance Saved values from database.
64
		 */
65
		public function widget( $args, $instance ) {
66
			$instance = wp_parse_args( $instance, $this->defaults() );
67
68
			$image_size_string = 'small' == $instance['flickr_image_size'] ? '_m.jpg' : '_t.jpg';
69
70
			$rss_url = ( ! isset( $instance['flickr_rss_url'] ) || empty( $instance['flickr_rss_url'] ) )
71
				? 'https://api.flickr.com/services/feeds/photos_interesting.gne?format=rss_200'
72
				: htmlspecialchars_decode( $instance['flickr_rss_url'] );
73
74
			// We want to use the HTTPS version so the URLs in the API response are also HTTPS and we avoid mixed-content warnings.
75
			$rss_url = preg_replace( '!^http://api.flickr.com/!i', 'https://api.flickr.com/', $rss_url );
76
77
			$rss = fetch_feed( $rss_url );
78
79
			$photos = '';
80
			if ( ! is_wp_error( $rss ) ) {
81
				foreach ( $rss->get_items( 0, $instance['items'] ) as $photo ) {
82
					switch ( $instance['flickr_image_size'] ) {
83
						case 'thumbnail':
84
							$src = $photo->get_enclosure()->get_thumbnail();
85
							break;
86
						case 'small':
87
							$src = preg_match( '/src="(.*?)"/i', $photo->get_description(), $p );
0 ignored issues
show
Unused Code introduced by
$src 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...
88
							$src = $p[1];
89
							break;
90
						case 'large':
91
							$src = $photo->get_enclosure()->get_link();
92
							break;
93
					}
94
95
					$photos .= '<a href="' . esc_url( $photo->get_permalink(), array( 'http', 'https' ) ) . '">';
96
					$photos .= '<img src="' . esc_url( $src, array( 'http', 'https' ) ) . '" ';
0 ignored issues
show
Bug introduced by
The variable $src does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
97
					$photos .= 'alt="' . esc_attr( $photo->get_title() ) . '" ';
98
					$photos .= 'border="0" ';
99
					$photos .= 'title="' . esc_attr( $photo->get_title() ) . '" ';
100
					$photos .= ' /></a><br /><br />';
101
				}
102
				if ( ! empty( $photos ) && class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
103
					$photos = Jetpack_Photon::filter_the_content( $photos );
104
				}
105
106
				$flickr_home = $rss->get_link();
107
			}
108
109
			echo $args['before_widget'];
110
			echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
111
			require( dirname( __FILE__ ) . '/flickr/widget.php' );
112
			echo $args['after_widget'];
113
			/** This action is already documented in modules/widgets/gravatar-profile.php */
114
			do_action( 'jetpack_stats_extra', 'widget_view', 'flickr' );
115
		}
116
117
		/**
118
		 * Back-end widget form.
119
		 *
120
		 * @param array $instance Previously saved values from database.
121
		 */
122
		public function form( $instance ) {
123
			$instance = wp_parse_args( $instance, $this->defaults() );
124
			require( dirname( __FILE__ ) . '/flickr/form.php' );
125
		}
126
127
		/**
128
		 * Sanitize widget form values as they are saved.
129
		 *
130
		 * @param  array $new_instance Values just sent to be saved.
131
		 * @param  array $old_instance Previously saved values from database.
132
		 * @return array Updated safe values to be saved.
133
		 */
134
		public function update( $new_instance, $old_instance ) {
135
			$instance = array();
136
			$defaults = $this->defaults();
0 ignored issues
show
Unused Code introduced by
$defaults 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...
137
138
			if ( isset( $new_instance['title'] ) ) {
139
				$instance['title'] = wp_kses( $new_instance['title'], array() );
140
			}
141
142
			if ( isset( $new_instance['items'] ) ) {
143
				$instance['items'] = intval( $new_instance['items'] );
144
			}
145
146
			if (
147
				isset( $new_instance['flickr_image_size'] ) &&
148
				in_array( $new_instance['flickr_image_size'], array( 'thumbnail', 'small', 'large' ) )
149
			) {
150
				$instance['flickr_image_size'] = $new_instance['flickr_image_size'];
151
			} else {
152
				$instance['flickr_image_size'] = 'thumbnail';
153
			}
154
155
			if ( isset( $new_instance['flickr_rss_url'] ) ) {
156
				$instance['flickr_rss_url'] = esc_url( $new_instance['flickr_rss_url'], array( 'http', 'https' ) );
157
158
				if ( strlen( $instance['flickr_rss_url'] ) < 10 ) {
159
					$instance['flickr_rss_url'] = '';
160
				}
161
			}
162
163
			return $instance;
164
		}
165
	}
166
167
	// Register Jetpack_Flickr_Widget widget.
168
	function jetpack_register_flickr_widget() {
169
		register_widget( 'Jetpack_Flickr_Widget' );
170
	}
171
	add_action( 'widgets_init', 'jetpack_register_flickr_widget' );
172
}
173