| Conditions | 3 |
| Paths | 1 |
| Total Lines | 130 |
| Code Lines | 75 |
| Lines | 52 |
| Ratio | 40 % |
| Changes | 3 | ||
| 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 |
||
| 40 | public function init(Service $service) |
||
| 41 | { |
||
| 42 | $service->get( |
||
| 43 | '/static/ip', |
||
| 44 | function (Request $request) { |
||
| 45 | // we typically deal with CNs, not user IDs, but the part of |
||
| 46 | // the CN before the first '_' is considered the user ID |
||
| 47 | $userId = InputValidation::userId( |
||
| 48 | $request->getUrl()->getQueryParameter('user_id'), |
||
| 49 | false // OPTIONAL |
||
| 50 | ); |
||
| 51 | $commonName = InputValidation::commonName( |
||
| 52 | $request->getUrl()->getQueryParameter('common_name'), |
||
| 53 | false // OPTIONAL |
||
| 54 | ); |
||
| 55 | |||
| 56 | if (!is_null($userId)) { |
||
| 57 | // per user |
||
| 58 | $ipConfig = $this->staticConfig->getStaticAddresses($userId); |
||
| 59 | } elseif (!is_null($commonName)) { |
||
| 60 | // per CN |
||
| 61 | $ipConfig = $this->staticConfig->getStaticAddress($commonName); |
||
| 62 | } else { |
||
| 63 | // all |
||
| 64 | $ipConfig = $this->staticConfig->getStaticAddresses(); |
||
| 65 | } |
||
| 66 | |||
| 67 | $response = new JsonResponse(); |
||
| 68 | $response->setBody( |
||
| 69 | array( |
||
| 70 | 'ok' => true, |
||
| 71 | 'ip' => $ipConfig, |
||
| 72 | 'ipRange' => $this->staticConfig->getIpRange()->getRange(), |
||
| 73 | 'poolRange' => $this->staticConfig->getPoolRange()->getRange(), |
||
| 74 | ) |
||
| 75 | ); |
||
| 76 | |||
| 77 | return $response; |
||
| 78 | } |
||
| 79 | ); |
||
| 80 | |||
| 81 | $service->post( |
||
| 82 | '/static/ip', |
||
| 83 | function (Request $request) { |
||
| 84 | $commonName = InputValidation::commonName( |
||
| 85 | $request->getPostParameter('common_name'), |
||
| 86 | true // REQUIRED |
||
| 87 | ); |
||
| 88 | $v4 = InputValidation::v4( |
||
| 89 | $request->getPostParameter('v4'), |
||
| 90 | false // OPTIONAL |
||
| 91 | ); |
||
| 92 | |||
| 93 | $response = new JsonResponse(); |
||
| 94 | $response->setBody( |
||
| 95 | array( |
||
| 96 | 'ok' => $this->staticConfig->setStaticAddresses($commonName, $v4), |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | $this->logger->info('setting static IP', array('cn' => $commonName, 'v4' => $v4)); |
||
| 101 | |||
| 102 | return $response; |
||
| 103 | } |
||
| 104 | ); |
||
| 105 | |||
| 106 | $service->post( |
||
| 107 | '/ccd/disable', |
||
| 108 | View Code Duplication | function (Request $request) { |
|
|
|
|||
| 109 | $commonName = InputValidation::commonName( |
||
| 110 | $request->getPostParameter('common_name'), |
||
| 111 | true // REQUIRED |
||
| 112 | ); |
||
| 113 | |||
| 114 | $this->logger->info('disabling cn', array('cn' => $commonName)); |
||
| 115 | |||
| 116 | $response = new JsonResponse(); |
||
| 117 | $response->setBody( |
||
| 118 | array( |
||
| 119 | 'ok' => $this->staticConfig->disableCommonName($commonName), |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | |||
| 123 | return $response; |
||
| 124 | } |
||
| 125 | ); |
||
| 126 | |||
| 127 | $service->delete( |
||
| 128 | '/ccd/disable', |
||
| 129 | View Code Duplication | function (Request $request) { |
|
| 130 | $commonName = InputValidation::commonName( |
||
| 131 | $request->getUrl()->getQueryParameter('common_name'), |
||
| 132 | true // REQUIRED |
||
| 133 | ); |
||
| 134 | |||
| 135 | $this->logger->info('enabling cn', array('cn' => $commonName)); |
||
| 136 | |||
| 137 | $response = new JsonResponse(); |
||
| 138 | $response->setBody( |
||
| 139 | array( |
||
| 140 | 'ok' => $this->staticConfig->enableCommonName($commonName), |
||
| 141 | ) |
||
| 142 | ); |
||
| 143 | |||
| 144 | return $response; |
||
| 145 | } |
||
| 146 | ); |
||
| 147 | |||
| 148 | $service->get( |
||
| 149 | '/ccd/disable', |
||
| 150 | View Code Duplication | function (Request $request) { |
|
| 151 | // we typically deal with CNs, not user IDs, but the part of |
||
| 152 | // the CN before the first '_' is considered the user ID |
||
| 153 | $userId = InputValidation::userId( |
||
| 154 | $request->getUrl()->getQueryParameter('user_id'), |
||
| 155 | false // OPTIONAL |
||
| 156 | ); |
||
| 157 | |||
| 158 | $response = new JsonResponse(); |
||
| 159 | $response->setBody( |
||
| 160 | array( |
||
| 161 | 'ok' => true, |
||
| 162 | 'disabled' => $this->staticConfig->getDisabledCommonNames($userId), |
||
| 163 | ) |
||
| 164 | ); |
||
| 165 | |||
| 166 | return $response; |
||
| 167 | } |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | } |
||
| 171 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.