| Conditions | 13 |
| Paths | 43 |
| Total Lines | 79 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 78 | public function filter($in, $out, &$consumed, $closing) |
||
| 79 | { |
||
| 80 | while ($bucket = stream_bucket_make_writeable($in)) { |
||
| 81 | $this->buffer .= $bucket->data; |
||
| 82 | $this->remaining += $bucket->datalen; |
||
| 83 | |||
| 84 | if ($this->remaining > $this->size) { |
||
| 85 | $this->buffer = substr($this->buffer, 0, $this->size - $this->remaining); |
||
| 86 | $this->remaining = $this->size; |
||
| 87 | } |
||
| 88 | |||
| 89 | $encryptionText = ''; |
||
| 90 | |||
| 91 | // write header |
||
| 92 | if ($this->context === null) { |
||
| 93 | /** |
||
| 94 | * @var WinZipAesExtraField|null $winZipExtra |
||
| 95 | */ |
||
| 96 | $winZipExtra = $this->entry->getExtraField(WinZipAesExtraField::HEADER_ID); |
||
| 97 | |||
| 98 | if ($winZipExtra === null) { |
||
| 99 | throw new RuntimeException('$winZipExtra is null'); |
||
| 100 | } |
||
| 101 | $saltSize = $winZipExtra->getSaltSize(); |
||
| 102 | |||
| 103 | try { |
||
| 104 | $salt = random_bytes($saltSize); |
||
| 105 | } catch (\Exception $e) { |
||
| 106 | throw new \RuntimeException('Oops, our server is bust and cannot generate any random data.', 1, $e); |
||
| 107 | } |
||
| 108 | $password = $this->entry->getPassword(); |
||
| 109 | |||
| 110 | if ($password === null) { |
||
| 111 | throw new RuntimeException('$password is null'); |
||
| 112 | } |
||
| 113 | $this->context = new WinZipAesContext( |
||
| 114 | $winZipExtra->getEncryptionStrength(), |
||
| 115 | $password, |
||
| 116 | $salt |
||
| 117 | ); |
||
| 118 | |||
| 119 | $encryptionText .= $salt . $this->context->getPasswordVerifier(); |
||
| 120 | } |
||
| 121 | |||
| 122 | // encrypt data |
||
| 123 | $offset = 0; |
||
| 124 | $len = \strlen($this->buffer); |
||
| 125 | $remaining = $this->remaining - $this->size; |
||
| 126 | |||
| 127 | if ($remaining >= WinZipAesContext::BLOCK_SIZE && $len < WinZipAesContext::BLOCK_SIZE) { |
||
| 128 | return \PSFS_FEED_ME; |
||
| 129 | } |
||
| 130 | $limit = max($len, $remaining); |
||
| 131 | |||
| 132 | if ($remaining > $limit && ($limit % WinZipAesContext::BLOCK_SIZE) !== 0) { |
||
| 133 | $limit -= ($limit % WinZipAesContext::BLOCK_SIZE); |
||
| 134 | } |
||
| 135 | |||
| 136 | while ($offset < $limit) { |
||
| 137 | $this->context->updateIv(); |
||
| 138 | $length = min(WinZipAesContext::BLOCK_SIZE, $limit - $offset); |
||
| 139 | $encryptionText .= $this->context->encrypt( |
||
| 140 | substr($this->buffer, 0, $length) |
||
| 141 | ); |
||
| 142 | $offset += $length; |
||
| 143 | $this->buffer = substr($this->buffer, $length); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($remaining === 0) { |
||
| 147 | $encryptionText .= $this->context->getHmac(); |
||
| 148 | } |
||
| 149 | |||
| 150 | $bucket->data = $encryptionText; |
||
| 151 | $consumed += $bucket->datalen; |
||
| 152 | |||
| 153 | stream_bucket_append($out, $bucket); |
||
| 154 | } |
||
| 155 | |||
| 156 | return \PSFS_PASS_ON; |
||
| 157 | } |
||
| 159 |