| Conditions | 8 |
| Paths | 42 |
| Total Lines | 64 |
| Code Lines | 37 |
| 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 |
||
| 151 | public function writableFoldersAction(array $userData) |
||
| 152 | { |
||
| 153 | $request = symfonyRequest::createFromGlobals(); |
||
| 154 | $requestMethod = $request->getMethod(); |
||
| 155 | $strErrorDesc = $responseData = $strErrorHeader = ''; |
||
| 156 | |||
| 157 | if (strtoupper($requestMethod) === 'GET') { |
||
| 158 | try { |
||
| 159 | $userFolders = !empty($userData['folders_list']) ? explode(',', $userData['folders_list']) : []; |
||
| 160 | $rows = DB::query( |
||
| 161 | 'SELECT nt.id AS folder_id, nt.title, nt.nlevel, nt.parent_id |
||
| 162 | FROM ' . prefixTable('nested_tree') . ' AS nt |
||
| 163 | LEFT JOIN ' . prefixTable('nested_tree') . ' AS personal |
||
| 164 | ON personal.personal_folder = 1 |
||
| 165 | AND personal.title = %s |
||
| 166 | WHERE nt.id IN %li |
||
| 167 | AND ( |
||
| 168 | nt.personal_folder = 0 |
||
| 169 | OR ( |
||
| 170 | personal.id IS NOT NULL |
||
| 171 | AND nt.nleft >= personal.nleft |
||
| 172 | AND nt.nright <= personal.nright |
||
| 173 | ) |
||
| 174 | ) |
||
| 175 | GROUP BY nt.id, nt.title, nt.nlevel, nt.parent_id |
||
| 176 | ORDER BY nt.nlevel ASC, nt.title ASC', |
||
| 177 | $userData['id'], |
||
| 178 | $userFolders |
||
| 179 | ); |
||
| 180 | |||
| 181 | $userId = (string) $userData['id']; |
||
| 182 | $username = $userData['username']; |
||
| 183 | $writableFolders = []; |
||
| 184 | foreach ($rows as $row) { |
||
| 185 | $writableFolders[] = [ |
||
| 186 | 'id' => (int) $row['folder_id'], |
||
| 187 | 'label' => $row['title'] === $userId ? $username : $row['title'], |
||
| 188 | 'level' => (int) $row['nlevel'], |
||
| 189 | 'parent_id' => (int) $row['parent_id'], |
||
| 190 | 'first_position' => $row['title'] === $userId ? 1 : 0, |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | |||
| 194 | $responseData = json_encode($writableFolders); |
||
| 195 | |||
| 196 | } catch (Error $e) { |
||
| 197 | $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.'; |
||
| 198 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 199 | } |
||
| 200 | } else { |
||
| 201 | $strErrorDesc = 'Method not supported'; |
||
| 202 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 203 | } |
||
| 204 | |||
| 205 | // send output |
||
| 206 | if (empty($strErrorDesc) === true) { |
||
| 207 | $this->sendOutput( |
||
| 208 | $responseData, |
||
| 209 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 210 | ); |
||
| 211 | } else { |
||
| 212 | $this->sendOutput( |
||
| 213 | json_encode(['error' => $strErrorDesc]), |
||
| 214 | ['Content-Type: application/json', $strErrorHeader] |
||
| 215 | ); |
||
| 220 |