| Conditions | 4 |
| Paths | 6 |
| Total Lines | 52 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 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 |
||
| 86 | public function tryMessenger(): ResponseInterface |
||
| 87 | { |
||
| 88 | $request = get_request(); |
||
| 89 | $message = []; |
||
| 90 | |||
| 91 | $getParams = $request->getQueryParams(); |
||
| 92 | $serverParams = $request->getServerParams(); |
||
| 93 | |||
| 94 | $serverName = $serverParams['SERVER_NAME'] ?? gethostname(); |
||
| 95 | $moduleName = $getParams['module'] ?? ''; |
||
| 96 | |||
| 97 | $data = []; |
||
| 98 | $data['status'] = 'undefined'; |
||
| 99 | $data['result']['moduleName'] = $moduleName; |
||
| 100 | |||
| 101 | $message['title'] = __('panel', 'test_msg_title', 'Testing Message from Host: ') . $serverName; |
||
| 102 | $message['body'] = __('panel', 'test_msg_body', 'Messenger module "{0}" has been tested and confirmed successfully.', [$moduleName]); |
||
| 103 | |||
| 104 | // @codeCoverageIgnoreStart |
||
| 105 | |||
| 106 | // Name the testing method. |
||
| 107 | $method = explode('-', $moduleName); |
||
| 108 | $method = implode('', array_map(function($word) { |
||
| 109 | return ucwords($word); |
||
| 110 | }, $method)); |
||
| 111 | |||
| 112 | // Call testing method if exists. |
||
| 113 | if ($this->{$method}($getParams, $message)) { |
||
| 114 | $data['status'] = 'success'; |
||
| 115 | } |
||
| 116 | |||
| 117 | $postParams = $request->getParsedBody(); |
||
| 118 | $postKey = 'messengers__' . $moduleName . '__confirm_test'; |
||
| 119 | |||
| 120 | if ('success' === $data['status']) { |
||
| 121 | $postParams[$postKey] = 'on'; |
||
| 122 | $this->saveConfig(); |
||
| 123 | |||
| 124 | } elseif ('error' === $data['status']) { |
||
| 125 | $postParams[$postKey] = 'off'; |
||
| 126 | $this->saveConfig(); |
||
| 127 | } |
||
| 128 | |||
| 129 | set_request($request->withParsedBody($postParams)); |
||
| 130 | |||
| 131 | // @codeCoverageIgnoreEnd |
||
| 132 | |||
| 133 | $data['result']['postKey'] = $postKey; |
||
| 134 | |||
| 135 | $output = json_encode($data); |
||
| 136 | |||
| 137 | return $this->respondJson($output); |
||
| 138 | } |
||
| 163 |