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 ( 28edb0...8667b8 )
by Robbie
9s
created

WorkflowField::sort()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 26
nc 16
nop 1
1
<?php
2
3
namespace Symbiote\AdvancedWorkflow\FormFields;
4
5
use ReflectionClass;
6
use SilverStripe\Control\HTTPResponse;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Manifest\ModuleLoader;
9
use SilverStripe\Forms\FormField;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\Security\SecurityToken;
12
use SilverStripe\View\ArrayData;
13
use SilverStripe\View\Requirements;
14
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition;
15
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction;
16
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowTransition;
17
use Symbiote\AdvancedWorkflow\FormFields\WorkflowField;
18
use Symbiote\AdvancedWorkflow\Services\WorkflowService;
19
20
/**
21
 * A form field that allows workflow actions and transitions to be edited,
22
 * while showing a visual overview of the flow.
23
 *
24
 * @package advancedworkflow
25
 */
26
class WorkflowField extends FormField
27
{
28
    private static $allowed_actions = array(
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...
29
        'action',
30
        'transition',
31
        'sort'
32
    );
33
34
    protected $definition;
35
36
    public function __construct($name, $title, WorkflowDefinition $definition)
37
    {
38
        $this->definition = $definition;
39
        $this->addExtraClass('workflow-field');
40
41
        parent::__construct($name, $title);
42
    }
43
44
    public function action()
45
    {
46
        return new WorkflowFieldActionController($this, 'action');
47
    }
48
49
    public function transition()
50
    {
51
        return new WorkflowFieldTransitionController($this, 'transition');
52
    }
53
54
    public function sort($request)
55
    {
56
        if (!SecurityToken::inst()->checkRequest($request)) {
57
            $this->httpError(404);
58
        }
59
60
        $class = $request->postVar('class');
61
        $ids   = $request->postVar('id');
62
63
        if ($class == WorkflowAction::class) {
64
            $objects = $this->Definition()->Actions();
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on Symbiote\AdvancedWorkflo...ects\WorkflowDefinition. Did you maybe mean updateAdminActions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
65
        } elseif ($class == WorkflowTransition::class) {
66
            $parent = $request->postVar('parent');
67
            $action = $this->Definition()->Actions()->byID($parent);
0 ignored issues
show
Bug introduced by
The method Actions() does not exist on Symbiote\AdvancedWorkflo...ects\WorkflowDefinition. Did you maybe mean updateAdminActions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
69
            if (!$action) {
70
                $this->httpError(400, _t(
71
                    'AdvancedWorkflowAdmin.INVALIDPARENTID',
72
                    'An invalid parent ID was specified.'
73
                ));
74
            }
75
76
            $objects = $action->Transitions();
77
        } else {
78
            $this->httpError(400, _t(
79
                'AdvancedWorkflowAdmin.INVALIDCLASSTOORDER',
80
                'An invalid class to order was specified.'
81
            ));
82
        }
83
84
        if (!$ids || array_diff($ids, $objects->column('ID'))) {
0 ignored issues
show
Bug introduced by
The variable $objects 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...
85
            $this->httpError(400, _t('AdvancedWorkflowAdmin.INVALIDIDLIST', 'An invalid list of IDs was provided.'));
86
        }
87
88
        singleton(WorkflowService::class)->reorder($objects, $ids);
89
90
        return new HTTPResponse(
91
            null,
92
            200,
93
            _t('AdvancedWorkflowAdmin.SORTORDERSAVED', 'The sort order has been saved.')
94
        );
95
    }
96
97
    public function getTemplate()
98
    {
99
        return __CLASS__;
100
    }
101
102
    public function FieldHolder($properties = array())
103
    {
104
        Requirements::javascript('symbiote/silverstripe-advancedworkflow:client/dist/js/advancedworkflow.js');
105
        Requirements::css('symbiote/silverstripe-advancedworkflow:client/dist/styles/advancedworkflow.css');
106
107
        return $this->Field($properties);
108
    }
109
110
    public function Definition()
111
    {
112
        return $this->definition;
113
    }
114
115
    public function ActionLink()
116
    {
117
        $parts = func_get_args();
118
        array_unshift($parts, 'action');
119
120
        return $this->Link(implode('/', $parts));
121
    }
122
123
    public function TransitionLink()
124
    {
125
        $parts = func_get_args();
126
        array_unshift($parts, 'transition');
127
128
        return $this->Link(implode('/', $parts));
129
    }
130
131
    public function CreateableActions()
132
    {
133
        $list    = ArrayList::create();
134
        $classes = ClassInfo::subclassesFor(WorkflowAction::class);
135
136
        array_shift($classes);
137
        sort($classes);
138
139
        foreach ($classes as $class) {
140
            $reflect = new ReflectionClass($class);
141
            $can     = singleton($class)->canCreate() && !$reflect->isAbstract();
142
143
            if ($can) {
144
                $list->push(ArrayData::create([
145
                    'Title' => singleton($class)->singular_name(),
146
                    'Class' => $this->sanitiseClassName($class),
147
                ]));
148
            }
149
        }
150
151
        return $list;
152
    }
153
154
    /**
155
     * Sanitise a model class' name for inclusion in a link
156
     *
157
     * @param string $class
158
     * @return string
159
     */
160
    protected function sanitiseClassName($class)
161
    {
162
        return str_replace('\\', '-', $class);
163
    }
164
}
165