| Conditions | 10 |
| Paths | 13 |
| Total Lines | 43 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 25 | public function getDecryptChatData(int $seq, int $limit, int $retry = 0): array |
||
| 26 | { |
||
| 27 | $config = $this->getConfig(); |
||
| 28 | if (!isset($config['private_keys'])) { |
||
| 29 | throw new InvalidArgumentException('缺少配置:private_keys[{"version":"private_key"}]'); |
||
| 30 | } |
||
| 31 | $privateKeys = $config['private_keys']; |
||
| 32 | |||
| 33 | try { |
||
| 34 | $chatData = json_decode($this->getChatData($seq, $limit), true)['chatdata']; |
||
| 35 | $newChatData = []; |
||
| 36 | $lastSeq = 0; |
||
| 37 | foreach ($chatData as $i => $item) { |
||
| 38 | $lastSeq = $item['seq']; |
||
| 39 | if (!isset($privateKeys[$item['publickey_ver']])) { |
||
| 40 | continue; |
||
| 41 | } |
||
| 42 | |||
| 43 | $decryptRandKey = null; |
||
| 44 | openssl_private_decrypt( |
||
| 45 | base64_decode($item['encrypt_random_key']), |
||
| 46 | $decryptRandKey, |
||
| 47 | $privateKeys[$item['publickey_ver']], |
||
| 48 | OPENSSL_PKCS1_PADDING |
||
| 49 | ); |
||
| 50 | |||
| 51 | // TODO 无法解密,一般为秘钥不匹配 |
||
| 52 | // 临时补丁方案,需要改为支持多版本key |
||
| 53 | if ($decryptRandKey === null) { |
||
| 54 | continue; |
||
| 55 | } |
||
| 56 | |||
| 57 | $newChatData[$i] = json_decode($this->decryptData($decryptRandKey, $item['encrypt_chat_msg']), true); |
||
| 58 | $newChatData[$i]['seq'] = $item['seq']; |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!empty($chatData) && empty($chatData) && $retry && $retry < 10) { |
||
| 62 | return $this->getDecryptChatData($lastSeq, $limit, ++$retry); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $newChatData; |
||
| 66 | } catch (\Exception $e) { |
||
| 67 | throw new FinanceSDKException($e->getMessage(), $e->getCode()); |
||
| 68 | } |
||
| 141 |