| Conditions | 7 |
| Paths | 1 |
| Total Lines | 126 |
| Code Lines | 71 |
| Lines | 51 |
| Ratio | 40.48 % |
| Changes | 1 | ||
| 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 |
||
| 37 | public function init(Service $service) |
||
| 38 | { |
||
| 39 | $service->get( |
||
| 40 | '/static/ip', |
||
| 41 | function (Request $request, UserInfoInterface $userInfo) { |
||
|
|
|||
| 42 | // we typically deal with CNs, not user IDs, but the part of |
||
| 43 | // the CN before the first '_' is considered the user ID |
||
| 44 | $userId = $request->getUrl()->getQueryParameter('user_id'); |
||
| 45 | $commonName = $request->getUrl()->getQueryParameter('common_name'); |
||
| 46 | if (!is_null($userId)) { |
||
| 47 | // per user |
||
| 48 | Utils::validateUserId($userId); |
||
| 49 | $ipConfig = $this->staticConfig->getStaticAddresses($userId); |
||
| 50 | } elseif (!is_null($commonName)) { |
||
| 51 | // per CN |
||
| 52 | Utils::validateCommonName($commonName); |
||
| 53 | $ipConfig = $this->staticConfig->getStaticAddress($commonName); |
||
| 54 | } else { |
||
| 55 | // all |
||
| 56 | $ipConfig = $this->staticConfig->getStaticAddresses(); |
||
| 57 | } |
||
| 58 | |||
| 59 | $response = new JsonResponse(); |
||
| 60 | $response->setBody( |
||
| 61 | array( |
||
| 62 | 'ok' => true, |
||
| 63 | 'ip' => $ipConfig, |
||
| 64 | 'ipRange' => $this->staticConfig->getIpRange()->getRange(), |
||
| 65 | 'poolRange' => $this->staticConfig->getPoolRange()->getRange(), |
||
| 66 | ) |
||
| 67 | ); |
||
| 68 | |||
| 69 | return $response; |
||
| 70 | } |
||
| 71 | ); |
||
| 72 | |||
| 73 | $service->post( |
||
| 74 | '/static/ip', |
||
| 75 | function (Request $request, UserInfoInterface $userInfo) { |
||
| 76 | $commonName = $request->getPostParameter('common_name'); |
||
| 77 | if (is_null($commonName)) { |
||
| 78 | throw new BadRequestException('missing common_name'); |
||
| 79 | } |
||
| 80 | Utils::validateCommonName($commonName); |
||
| 81 | |||
| 82 | $v4 = $request->getPostParameter('v4'); |
||
| 83 | if (empty($v4)) { |
||
| 84 | $v4 = null; |
||
| 85 | } |
||
| 86 | |||
| 87 | $response = new JsonResponse(); |
||
| 88 | $response->setBody( |
||
| 89 | array( |
||
| 90 | 'ok' => $this->staticConfig->setStaticAddresses($commonName, $v4), |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | |||
| 94 | // $this->logInfo('setting static IP', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName, 'v4' => $v4)); |
||
| 95 | |||
| 96 | return $response; |
||
| 97 | } |
||
| 98 | ); |
||
| 99 | |||
| 100 | $service->post( |
||
| 101 | '/ccd/disable', |
||
| 102 | View Code Duplication | function (Request $request, UserInfoInterface $userInfo) { |
|
| 103 | $commonName = $request->getPostParameter('common_name'); |
||
| 104 | if (is_null($commonName)) { |
||
| 105 | throw new BadRequestException('missing common_name'); |
||
| 106 | } |
||
| 107 | Utils::validateCommonName($commonName); |
||
| 108 | |||
| 109 | // $this->logInfo('disabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName)); |
||
| 110 | |||
| 111 | $response = new JsonResponse(); |
||
| 112 | $response->setBody( |
||
| 113 | array( |
||
| 114 | 'ok' => $this->staticConfig->disableCommonName($commonName), |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | |||
| 118 | return $response; |
||
| 119 | } |
||
| 120 | ); |
||
| 121 | |||
| 122 | $service->delete( |
||
| 123 | '/ccd/disable', |
||
| 124 | View Code Duplication | function (Request $request, UserInfoInterface $userInfo) { |
|
| 125 | $commonName = $request->getUrl()->getQueryParameter('common_name'); |
||
| 126 | Utils::validateCommonName($commonName); |
||
| 127 | |||
| 128 | // $this->logInfo('enabling cn', array('api_user' => $userInfo->getUserId(), 'cn' => $commonName)); |
||
| 129 | |||
| 130 | $response = new JsonResponse(); |
||
| 131 | $response->setBody( |
||
| 132 | array( |
||
| 133 | 'ok' => $this->staticConfig->enableCommonName($commonName), |
||
| 134 | ) |
||
| 135 | ); |
||
| 136 | |||
| 137 | return $response; |
||
| 138 | } |
||
| 139 | ); |
||
| 140 | |||
| 141 | $service->get( |
||
| 142 | '/ccd/disable', |
||
| 143 | View Code Duplication | function (Request $request, UserInfoInterface $userInfo) { |
|
| 144 | // we typically deal with CNs, not user IDs, but the part of |
||
| 145 | // the CN before the first '_' is considered the user ID |
||
| 146 | $userId = $request->getUrl()->getQueryParameter('user_id'); |
||
| 147 | if (!is_null($userId)) { |
||
| 148 | Utils::validateUserId($userId); |
||
| 149 | } |
||
| 150 | |||
| 151 | $response = new JsonResponse(); |
||
| 152 | $response->setBody( |
||
| 153 | array( |
||
| 154 | 'ok' => true, |
||
| 155 | 'disabled' => $this->staticConfig->getDisabledCommonNames($userId), |
||
| 156 | ) |
||
| 157 | ); |
||
| 158 | |||
| 159 | return $response; |
||
| 160 | } |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.