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.

BlocksContentControllerExtension::onAfterInit()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace SheaDawson\Blocks\Extensions;
4
5
use SilverStripe\View\Requirements;
6
use SilverStripe\Core\Extension;
7
8
class BlocksContentControllerExtension extends Extension
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
        'handleBlock',
15
    ];
16
17
    public function onAfterInit()
18
    {
19
        if ($this->owner->data()->canEdit() && $this->owner->getRequest()->getVar('block_preview') == 1) {
20
            Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js');
21
            Requirements::javascript(BLOCKS_DIR.'/javascript/block-preview.js');
22
            Requirements::css(BLOCKS_DIR.'/css/block-preview.css');
23
        }
24
    }
25
26
    /**
27
     * Handles blocks attached to a page
28
     * Assumes URLs in the following format: <URLSegment>/block/<block-ID>.
29
     *
30
     * @return RequestHandler
31
     */
32
    public function handleBlock()
33
    {
34
        if ($id = $this->owner->getRequest()->param('ID')) {
35
            $blocks = $this->owner->data()->getBlockList(null, true, true, true);
36
            if ($block = $blocks->find('ID', $id)) {
37
                return $block->getController();
38
            }
39
        }
40
41
        return $block->getController();
0 ignored issues
show
Bug introduced by
The variable $block 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...
42
    }
43
}
44