| Conditions | 5 |
| Paths | 1 |
| Total Lines | 124 |
| Code Lines | 68 |
| Lines | 53 |
| Ratio | 42.74 % |
| 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 |
||
| 50 | public function init(Service $service) |
||
| 51 | { |
||
| 52 | $service->get( |
||
| 53 | '/config/users', |
||
| 54 | function (Request $request, TokenInfo $tokenInfo) { |
||
| 55 | Utils::requireScope($tokenInfo, ['admin']); |
||
| 56 | |||
| 57 | $userConfigArray = []; |
||
| 58 | View Code Duplication | foreach ($this->io->readFolder($this->configDir) as $configFile) { |
|
|
|
|||
| 59 | $userConfig = new UserConfig( |
||
| 60 | Json::decode( |
||
| 61 | $this->io->readFile($configFile) |
||
| 62 | ) |
||
| 63 | ); |
||
| 64 | // never expose the OTP secret |
||
| 65 | $userConfig->hideOtpSecret(); |
||
| 66 | $userConfigArray[basename($configFile)] = $userConfig->toArray(); |
||
| 67 | } |
||
| 68 | |||
| 69 | $response = new JsonResponse(); |
||
| 70 | $response->setBody($userConfigArray); |
||
| 71 | |||
| 72 | return $response; |
||
| 73 | } |
||
| 74 | ); |
||
| 75 | |||
| 76 | $service->get( |
||
| 77 | '/config/users/:userId', |
||
| 78 | View Code Duplication | function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
| 79 | Utils::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 80 | InputValidation::userId($userId); |
||
| 81 | |||
| 82 | $fileName = sprintf('%s/%s', $this->configDir, $userId); |
||
| 83 | if (!$this->io->isFile($fileName)) { |
||
| 84 | // if the file does not exist, use default values |
||
| 85 | $userConfig = new UserConfig([]); |
||
| 86 | } else { |
||
| 87 | $userConfig = new UserConfig( |
||
| 88 | Json::decode( |
||
| 89 | $this->io->readFile( |
||
| 90 | sprintf('%s/%s', $this->configDir, $userId) |
||
| 91 | ) |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | // never expose the OTP secret |
||
| 95 | $userConfig->hideOtpSecret(); |
||
| 96 | } |
||
| 97 | |||
| 98 | $response = new JsonResponse(); |
||
| 99 | $response->setBody($userConfig->toArray()); |
||
| 100 | |||
| 101 | return $response; |
||
| 102 | } |
||
| 103 | ); |
||
| 104 | |||
| 105 | $service->put( |
||
| 106 | '/config/users/:userId/otp_secret', |
||
| 107 | function (Request $request, TokenInfo $tokenInfo, $userId) { |
||
| 108 | Utils::requireScope($tokenInfo, ['admin', 'portal']); |
||
| 109 | InputValidation::userId($userId); |
||
| 110 | |||
| 111 | $fileName = sprintf('%s/%s', $this->configDir, $userId); |
||
| 112 | if (!$this->io->isFile($fileName)) { |
||
| 113 | // if the file does not exist, use default values |
||
| 114 | $userConfig = new UserConfig([]); |
||
| 115 | } else { |
||
| 116 | $userConfig = new UserConfig( |
||
| 117 | Json::decode( |
||
| 118 | $this->io->readFile( |
||
| 119 | sprintf('%s/%s', $this->configDir, $userId) |
||
| 120 | ) |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | // never expose the OTP secret |
||
| 124 | $userConfig->hideOtpSecret(); |
||
| 125 | } |
||
| 126 | |||
| 127 | if (false !== $userConfig->getOtpSecret()) { |
||
| 128 | // an OTP secret was already set, it is not allowed to |
||
| 129 | // update the otp_secret using this API call |
||
| 130 | throw new BadRequestException('otp_secret already set'); |
||
| 131 | } |
||
| 132 | |||
| 133 | // we wrap the request body in an UserConfig object to validate |
||
| 134 | // whatever is there |
||
| 135 | $requestUserConfig = new UserConfig(Json::decode($request->getBody())); |
||
| 136 | |||
| 137 | // we extract the OTP secret from the request body and set it |
||
| 138 | $userConfig->setOtpSecret($requestUserConfig->getOtpSecret()); |
||
| 139 | |||
| 140 | $this->io->writeFile( |
||
| 141 | sprintf('%s/%s', $this->configDir, $userId), |
||
| 142 | Json::encode($userConfig->toArray()) |
||
| 143 | ); |
||
| 144 | |||
| 145 | $response = new JsonResponse(); |
||
| 146 | $response->setBody(['ok' => true]); |
||
| 147 | |||
| 148 | return $response; |
||
| 149 | } |
||
| 150 | ); |
||
| 151 | |||
| 152 | $service->put( |
||
| 153 | '/config/users/:userId', |
||
| 154 | View Code Duplication | function (Request $request, TokenInfo $tokenInfo, $userId) { |
|
| 155 | Utils::requireScope($tokenInfo, ['admin']); |
||
| 156 | InputValidation::userId($userId); |
||
| 157 | |||
| 158 | // we wrap the request body in an UserConfig object to validate |
||
| 159 | // whatever is there |
||
| 160 | $requestUserConfig = new UserConfig(Json::decode($request->getBody())); |
||
| 161 | |||
| 162 | $this->io->writeFile( |
||
| 163 | sprintf('%s/%s', $this->configDir, $userId), |
||
| 164 | Json::encode($requestUserConfig->toArray()) |
||
| 165 | ); |
||
| 166 | |||
| 167 | $response = new JsonResponse(); |
||
| 168 | $response->setBody(['ok' => true]); |
||
| 169 | |||
| 170 | return $response; |
||
| 171 | } |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
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.