Conditions | 10 |
Paths | 13 |
Total Lines | 70 |
Lines | 11 |
Ratio | 15.71 % |
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 |
||
66 | public function createSignRequest(CreateU2fSignRequestCommand $command) |
||
67 | { |
||
68 | $this->logger->info('Create U2F sign request'); |
||
69 | |||
70 | $body = [ |
||
71 | 'requester' => ['institution' => $command->institution, 'identity' => $command->identityId], |
||
72 | 'key_handle' => ['value' => $command->keyHandle], |
||
73 | ]; |
||
74 | |||
75 | $response = $this->guzzleClient->post('api/u2f/create-sign-request', ['json' => $body, 'http_errors' => false]); |
||
76 | $statusCode = $response->getStatusCode(); |
||
77 | |||
78 | try { |
||
79 | $result = JsonHelper::decode((string) $response->getBody()); |
||
80 | } catch (CoreRuntimeException $e) { |
||
81 | $this->logger->error('U2F sign request creation failed; JSON decoding failed.'); |
||
82 | |||
83 | return SignRequestCreationResult::apiError(); |
||
84 | } |
||
85 | |||
86 | $hasErrors = isset($result['errors']) |
||
87 | && is_array($result['errors']) |
||
88 | && $result['errors'] === array_filter($result['errors'], 'is_string'); |
||
89 | |||
90 | View Code Duplication | if ($hasErrors && $statusCode >= 400 && $statusCode < 600) { |
|
|
|||
91 | $this->logger->critical( |
||
92 | sprintf( |
||
93 | 'U2F sign request creation failed; HTTP %d with errors "%s"', |
||
94 | $statusCode, |
||
95 | join(', ', $result['errors']) |
||
96 | ) |
||
97 | ); |
||
98 | |||
99 | return SignRequestCreationResult::apiError(); |
||
100 | } |
||
101 | |||
102 | $actualKeys = array_keys($result); |
||
103 | $expectedKeys = ['app_id', 'challenge', 'key_handle', 'version']; |
||
104 | if ($statusCode != 200 || array_diff($actualKeys, $expectedKeys) !== array_diff($expectedKeys, $actualKeys)) { |
||
105 | $this->logger->critical( |
||
106 | sprintf( |
||
107 | 'U2F API behaving nonconformingly, returned response or status code (%d) unexpected', |
||
108 | $statusCode |
||
109 | ) |
||
110 | ); |
||
111 | |||
112 | return SignRequestCreationResult::apiError(); |
||
113 | } |
||
114 | |||
115 | $signRequest = new SignRequest(); |
||
116 | $signRequest->appId = $result['app_id']; |
||
117 | $signRequest->challenge = $result['challenge']; |
||
118 | $signRequest->keyHandle = $result['key_handle']; |
||
119 | $signRequest->version = $result['version']; |
||
120 | |||
121 | $violations = $this->validator->validate($signRequest); |
||
122 | if (count($violations) > 0) { |
||
123 | $this->logger->critical( |
||
124 | sprintf( |
||
125 | 'U2F API behaving nonconformingly, returned sign request does not validate', |
||
126 | $statusCode |
||
127 | ), |
||
128 | ['errors' => $this->mapViolationsToErrorStrings($violations, 'sign_request')] |
||
129 | ); |
||
130 | |||
131 | return SignRequestCreationResult::apiError(); |
||
132 | } |
||
133 | |||
134 | return SignRequestCreationResult::success($signRequest); |
||
135 | } |
||
136 | |||
230 |
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.