Conditions | 25 |
Paths | 1920 |
Total Lines | 135 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
13 | public function httpSubmission($request) |
||
14 | { |
||
15 | $vars = $request->requestVars(); |
||
16 | if (isset($funcName)) { |
||
|
|||
17 | Form::setFormAction($funcName); |
||
18 | } |
||
19 | |||
20 | // Populate the form |
||
21 | $this->loadDataFrom($vars, true); |
||
22 | |||
23 | // Protection against CSRF attacks |
||
24 | $token = $this->getSecurityToken(); |
||
25 | if (!$token->checkRequest($request)) { |
||
26 | $this->httpError(400, _t( |
||
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'; |
||
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); |
||
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( |
||
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) |
||
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( |
||
94 | 403, |
||
95 | sprintf(_t( |
||
96 | 'AdvancedWorkflowFrontendForm.ACTIONFORMCHECK', |
||
97 | 'Action "%s" not allowed on form (Name: "%s")' |
||
98 | ), $funcName, $this->Name()) |
||
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') { |
||
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(); |
||
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()); |
||
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); |
||
147 | } |
||
148 | } |
||
149 |
This check marks calls to
isset(...)
orempty(...)
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.