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.
Completed
Push — master ( cb2cea...828c4e )
by Charles
10s
created

IPLoggerSecurityAdminExtension::updateEditForm()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 77
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 8.4456
c 0
b 0
f 0
cc 5
eloc 43
nc 2
nop 1

How to fix   Long Method   

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
class IPLoggerSecurityAdminExtension extends LeftAndMainExtension
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...
3
{
4
    private static $dependencies = array(
0 ignored issues
show
Unused Code introduced by
The property $dependencies 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...
5
        'loggerService' => '%$IPLoggerService',
6
        'loggerEntry'   => '%$IPLoggerEntry',
7
        'banEntry'      => '%$IPBanEntry'
8
    );
9
10
    public $loggerService;
11
    
12
    public $loggerEntry;
13
14
    public $banEntry;
15
    
16
    public function updateEditForm($form)
17
    {
18
        if (Permission::check('ADMIN')) {
19
            $fields = $form->Fields();
20
21
            $loggerTab = $fields->findOrMakeTab('Root.IPLogs', 'IP Logs');
22
        
23
            // List ban entries
24
            $banClass = get_class($this->banEntry);
25
        
26
            $banEntries = $banClass::get()->sort('Created DESC');
27
28
            $banGrid = GridField::create('BanGrid', 'Bans', $banEntries);
29
30
            $banGrid->setForm($form);
31
32
            $banGridConfig = GridFieldConfig_RecordEditor::create();
33
            $banGridConfig->removeComponentsByType('GridFieldAddNewButton');
34
35
            // Add a new sudo column to show whether or not a ban is active
36
            $dataColumns = $banGridConfig->getComponentByType('GridFieldDataColumns');
37
38
            $displayFields = $dataColumns->getDisplayFields($banGrid);
39
40
            $displayFields = array('Active' => '') + $displayFields;
41
42
            $dataColumns->setDisplayFields($displayFields);
43
44
            $loggerService = $this->loggerService;
45
            $dataColumns->setFieldFormatting(
46
                array(
47
                    'Active' => function ($value, $item) use ($loggerService) {
48
                        $banSecondsAgo = $item->obj('Created')->TimeDiffIn('seconds');
49
                        $banSeconds = $loggerService->getRule($item->Event)['bantime'];
50
51
                        $active = $banSeconds === 0 || $banSecondsAgo < $banSeconds;
52
53
                        switch ($active) {
54
                            case true:
55
                                $colour = 'rgb(76, 153, 0)';
56
                                $value = 'Active';
57
                                break;
58
                            case false:
59
                                $colour = 'rgb(255, 0, 0)';
60
                                $value = 'Expired';
61
                                break;
62
                        }
63
64
                        $html = "<strong style='color: {$colour};
0 ignored issues
show
Bug introduced by
The variable $colour 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...
65
font-size: 120%;'>{$value}</strong>";
66
67
                        return $html;
68
                    }
69
                )
70
            );
71
72
            $banGrid->setConfig($banGridConfig);
73
74
            $loggerTab->push($banGrid);
75
76
            // List log entries
77
            $entryClass = get_class($this->loggerEntry);
78
        
79
            $logEntries = $entryClass::get()->sort('Created DESC');
80
81
            $entryGrid = GridField::create('EntryGrid', 'Log Entries', $logEntries);
82
83
            $entryGrid->setForm($form);
84
85
            $entryGridConfig = GridFieldConfig_RecordEditor::create();
86
            $entryGridConfig->removeComponentsByType('GridFieldAddNewButton');
87
88
            $entryGrid->setConfig($entryGridConfig);
89
90
            $loggerTab->push($entryGrid);
91
        }
92
    }
93
}
94