GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

FooGallery_Widget::widget()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 2
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Widget to insert a FooGallery
4
 */
5
6
if ( ! class_exists( 'FooGallery_Widget_Init' ) ) {
7
    class FooGallery_Widget_Init
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...
8
    {
9
        public function __construct()
10
        {
11
            add_action('widgets_init', array($this, 'register_widget'));
12
        }
13
14
        public function register_widget()
15
        {
16
            register_widget('FooGallery_Widget');
17
        }
18
    }
19
}
20
21
if ( ! class_exists( 'FooGallery_Widget' ) ) {
22
    class FooGallery_Widget extends WP_Widget
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
23
    {
24
25
        /**
26
         * Sets up the widgets name etc
27
         */
28
        public function __construct()
29
        {
30
            $widget_ops = array(
31
                'classname' => 'foogallery_widget',
32
                'description' => __('Insert a FooGallery', 'foogallery'),
33
            );
34
35
            parent::__construct('foogallery_widget', 'FooGallery', $widget_ops);
36
        }
37
38
39
        /**
40
         * Outputs the content of the widget
41
         *
42
         * @param array $args
43
         * @param array $instance
44
         */
45
        public function widget($args, $instance)
46
        {
47
            // outputs the content of the widget
48
            echo $args['before_widget'];
49
            if (!empty($instance['title'])) {
50
                echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
51
            }
52
            //output the gallery here
53
            $foogallery_id = isset( $instance['foogallery_id'] ) ? intval( $instance['foogallery_id'] ) : 0;
54
            if ( $foogallery_id > 0 ) {
55
                foogallery_render_gallery( $foogallery_id );
56
            }
57
58
            echo $args['after_widget'];
59
        }
60
61
62
        /**
63
         * Outputs the options form on admin
64
         *
65
         * @param array $instance The widget options
66
         * @return string|void
67
         */
68
        public function form($instance)
69
        {
70
            // outputs the options form on admin
71
            $title = !empty($instance['title']) ? $instance['title'] : __('New title', 'foogallery');
72
            $foogallery_id = !empty($instance['foogallery_id'])  ? intval($instance['foogallery_id']) : 0;
73
            $galleries = foogallery_get_all_galleries();
74
            ?>
75
            <p>
76
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
77
                <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"
78
                       name="<?php echo $this->get_field_name('title'); ?>" type="text"
79
                       value="<?php echo esc_attr($title); ?>">
80
            </p>
81
            <p>
82
                <label for="<?php echo $this->get_field_id('foogallery_id'); ?>"><?php _e('Select Gallery:', 'foogallery'); ?></label>
83
                <select class="widefat" id="<?php echo $this->get_field_id('foogallery_id'); ?>"
84
                       name="<?php echo $this->get_field_name('foogallery_id'); ?>"
85
                       value="<?php echo esc_attr($title); ?>">
86
                    <?php foreach ( $galleries as $gallery ) {?>
87
                        <option <?php echo $gallery->ID == $foogallery_id ? 'selected="selected"' : ''; ?> value="<?php echo $gallery->ID; ?>"><?php echo $gallery->name . ' [' . $gallery->ID . ']'; ?></option>
88
                    <?php } ?>
89
                </select>
90
            </p>
91
            <?php
92
        }
93
94
95
        /**
96
         * Processing widget options on save
97
         *
98
         * @param array $new_instance The new options
99
         * @param array $old_instance The previous options
100
         * @return array|mixed
101
         */
102
        public function update($new_instance, $old_instance)
103
        {
104
            // processes widget options to be saved
105
            foreach ($new_instance as $key => $value) {
106
                $updated_instance[$key] = sanitize_text_field($value);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$updated_instance was never initialized. Although not strictly required by PHP, it is generally a good practice to add $updated_instance = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
            }
108
109
            return $updated_instance;
0 ignored issues
show
Bug introduced by
The variable $updated_instance 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...
110
        }
111
    }
112
}