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.

FrontendWorkflowForm::httpSubmission()   F
last analyzed

Complexity

Conditions 25
Paths 1920

Size

Total Lines 135

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 0
c 0
b 0
f 0
cc 25
nc 1920
nop 1

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
namespace Symbiote\AdvancedWorkflow\Forms;
4
5
use SilverStripe\Forms\Form;
6
use FormResponse;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Core\Convert;
10
11
class FrontendWorkflowForm extends Form
12
{
13
    public function httpSubmission($request)
14
    {
15
        $vars = $request->requestVars();
16
        if (isset($funcName)) {
0 ignored issues
show
Bug introduced by
The variable $funcName seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
17
            Form::setFormAction($funcName);
18
        }
19
20
        // Populate the form
21
        $this->loadDataFrom($vars, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
22
23
        // Protection against CSRF attacks
24
        $token = $this->getSecurityToken();
25
        if (!$token->checkRequest($request)) {
26
            $this->httpError(400, _t(
0 ignored issues
show
Documentation Bug introduced by
The method httpError does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
27
                'AdvancedWorkflowFrontendForm.SECURITYTOKENCHECK',
28
                "Security token doesn't match, possible CSRF attack."
29
            ));
30
        }
31
32
        // Determine the action button clicked
33
        $funcName = null;
34
        foreach ($vars as $paramName => $paramVal) {
35
            if (substr($paramName, 0, 7) == 'action_') {
36
                // Added for frontend workflow form - get / set transitionID on controller,
37
                // unset action and replace with doFrontEndAction action
38
                if (substr($paramName, 0, 18) == 'action_transition_') {
39
                    $this->controller->transitionID = substr($paramName, strrpos($paramName, '_') +1);
40
                    unset($vars['action_transition_' . $this->controller->transitionID]);
41
                    $vars['action_doFrontEndAction'] = 'doFrontEndAction';
42
                    $paramName = 'action_doFrontEndAction';
43
                    $paramVal = 'doFrontEndAction';
0 ignored issues
show
Unused Code introduced by
$paramVal 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...
44
                }
45
46
                // Break off querystring arguments included in the action
47
                if (strpos($paramName, '?') !== false) {
48
                    list($paramName, $paramVars) = explode('?', $paramName, 2);
49
                    $newRequestParams = array();
50
                    parse_str($paramVars, $newRequestParams);
51
                    $vars = array_merge((array)$vars, (array)$newRequestParams);
52
                }
53
54
                // Cleanup action_, _x and _y from image fields
55
                $funcName = preg_replace(array('/^action_/','/_x$|_y$/'), '', $paramName);
56
                break;
57
            }
58
        }
59
60
        // If the action wasnt' set, choose the default on the form.
61
        if (!isset($funcName) && $defaultAction = $this->defaultAction()) {
62
            $funcName = $defaultAction->actionName();
63
        }
64
65
        if (isset($funcName)) {
66
            $this->setButtonClicked($funcName);
0 ignored issues
show
Documentation Bug introduced by
The method setButtonClicked does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
67
        }
68
69
        // Permission checks (first on controller, then falling back to form)
70
        if (// Ensure that the action is actually a button or method on the form,
71
            // and not just a method on the controller.
72
            $this->controller->hasMethod($funcName)
73
            && !$this->controller->checkAccessAction($funcName)
74
            // If a button exists, allow it on the controller
75
            && !$this->Actions()->fieldByName('action_' . $funcName)
76
        ) {
77
            return $this->httpError(
0 ignored issues
show
Documentation Bug introduced by
The method httpError does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
                403,
79
                sprintf(
80
                    _t(
81
                        'AdvancedWorkflowFrontendForm.ACTIONCONTROLLERCHECK',
82
                        'Action "%s" not allowed on controller (Class: %s)'
83
                    ),
84
                    $funcName,
85
                    get_class($this->controller)
86
                )
87
            );
88
        } elseif ($this->hasMethod($funcName)
89
            && !$this->checkAccessAction($funcName)
0 ignored issues
show
Documentation Bug introduced by
The method checkAccessAction does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
90
            // No checks for button existence or $allowed_actions is performed -
91
            // all form methods are callable (e.g. the legacy "callfieldmethod()")
92
        ) {
93
            return $this->httpError(
0 ignored issues
show
Documentation Bug introduced by
The method httpError does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
94
                403,
95
                sprintf(_t(
96
                    'AdvancedWorkflowFrontendForm.ACTIONFORMCHECK',
97
                    'Action "%s" not allowed on form (Name: "%s")'
98
                ), $funcName, $this->Name())
0 ignored issues
show
Documentation Bug introduced by
The method Name does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
            );
100
        }
101
102
        if ($wfTransition = $this->controller->getCurrentTransition()) {
103
            $wfTransType = $wfTransition->Type;
104
        } else {
105
            $wfTransType = null; //ie. when a custom Form Action is defined in WorkflowAction
106
        }
107
108
        // Validate the form
109
        if (!$this->validate() && $wfTransType == 'Active') {
0 ignored issues
show
Documentation Bug introduced by
The method validate does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
            if (Director::is_ajax()) {
111
                $acceptType = $request->getHeader('Accept');
112
                if (strpos($acceptType, 'application/json') !== false) {
113
                    // Send validation errors back as JSON with a flag at the start
114
                    $response = new HTTPResponse(json_encode($this->validator->getErrors()));
115
                    $response->addHeader('Content-Type', 'application/json');
116
                } else {
117
                    $this->setupFormErrors();
0 ignored issues
show
Documentation Bug introduced by
The method setupFormErrors does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
118
                    // Send the newly rendered form tag as HTML
119
                    $response = new HTTPResponse($this->forTemplate());
120
                    $response->addHeader('Content-Type', 'text/html');
121
                }
122
123
                return $response;
124
            }
125
126
            if ($this->getRedirectToFormOnValidationError()) {
127
                if ($pageURL = $request->getHeader('Referer')) {
128
                    if (Director::is_site_url($pageURL)) {
129
                        // Remove existing pragmas
130
                        $pageURL = preg_replace('/(#.*)/', '', $pageURL);
131
                        return Director::redirect($pageURL . '#' . $this->FormName());
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on SilverStripe\Control\Director. Did you maybe mean force_redirect()?

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...
132
                    }
133
                }
134
            }
135
            return $this->controller->redirectBack();
136
        }
137
138
        // First, try a handler method on the controller (has been checked for allowed_actions above already)
139
        if ($this->controller->hasMethod($funcName)) {
140
            return $this->controller->$funcName($vars, $this, $request);
141
        // Otherwise, try a handler method on the form object.
142
        } elseif ($this->hasMethod($funcName)) {
143
            return $this->$funcName($vars, $this, $request);
144
        }
145
146
        return $this->httpError(404);
0 ignored issues
show
Documentation Bug introduced by
The method httpError does not exist on object<Symbiote\Advanced...s\FrontendWorkflowForm>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
147
    }
148
}
149