| Conditions | 5 |
| Paths | 12 |
| Total Lines | 57 |
| Code Lines | 42 |
| 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 |
||
| 80 | public function createAction(array $userData) |
||
| 81 | { |
||
| 82 | $request = Request::createFromGlobals(); |
||
| 83 | $requestMethod = $request->getMethod(); |
||
| 84 | //$superGlobal = new SuperGlobal(); |
||
| 85 | $strErrorDesc = $responseData = $strErrorHeader = ''; |
||
| 86 | //$requestMethod = $superGlobal->get('REQUEST_METHOD', 'SERVER'); |
||
| 87 | |||
| 88 | if (strtoupper($requestMethod) === 'POST') { |
||
| 89 | if (empty($userData['folders_list'])) { |
||
| 90 | $this->sendOutput("", ['HTTP/1.1 204 No Content']); |
||
| 91 | } else { |
||
| 92 | // get parameters |
||
| 93 | $arrQueryStringParams = $this->getQueryStringParams(); |
||
| 94 | |||
| 95 | try { |
||
| 96 | $folderModel = new FolderModel(); |
||
| 97 | $arrFolder = $folderModel->createFolder( |
||
| 98 | (string) $arrQueryStringParams['title'], |
||
| 99 | (int) $arrQueryStringParams['parent_id'], |
||
| 100 | (int) $arrQueryStringParams['complexity'], |
||
| 101 | (int) $arrQueryStringParams['duration'], |
||
| 102 | (int) $arrQueryStringParams['create_auth_without'], |
||
| 103 | (int) $arrQueryStringParams['edit_auth_without'], |
||
| 104 | (string) $arrQueryStringParams['icon'], |
||
| 105 | (string) $arrQueryStringParams['icon_selected'], |
||
| 106 | (string) $arrQueryStringParams['access_rights'], |
||
| 107 | (int) $userData['is_admin'], |
||
| 108 | (array) explode(',', $userData['folders_list']), |
||
| 109 | (int) $userData['is_manager'], |
||
| 110 | (int) $userData['user_can_create_root_folder'], |
||
| 111 | (int) $userData['user_can_manage_all_users'], |
||
| 112 | (int) $userData['id'], |
||
| 113 | (string) $userData['roles'], |
||
| 114 | ); |
||
| 115 | |||
| 116 | $responseData = json_encode($arrFolder); |
||
| 117 | } catch (Error $e) { |
||
| 118 | $strErrorDesc = $e->getMessage() . ' Something went wrong! Please contact support.1'; |
||
| 119 | $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | $strErrorDesc = 'Method not supported'; |
||
| 124 | $strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; |
||
| 125 | } |
||
| 126 | |||
| 127 | // send output |
||
| 128 | if (empty($strErrorDesc) === true) { |
||
| 129 | $this->sendOutput( |
||
| 130 | $responseData, |
||
| 131 | ['Content-Type: application/json', 'HTTP/1.1 200 OK'] |
||
| 132 | ); |
||
| 133 | } else { |
||
| 134 | $this->sendOutput( |
||
| 135 | json_encode(['error' => $strErrorDesc]), |
||
| 136 | ['Content-Type: application/json', $strErrorHeader] |
||
| 137 | ); |
||
| 142 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: