| Conditions | 15 |
| Paths | 45 |
| Total Lines | 87 |
| Code Lines | 51 |
| 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 |
||
| 85 | public function filter($in, $out, &$consumed, $closing) |
||
| 86 | { |
||
| 87 | while ($bucket = stream_bucket_make_writeable($in)) { |
||
| 88 | $this->buffer .= $bucket->data; |
||
| 89 | $this->readLength += $bucket->datalen; |
||
| 90 | |||
| 91 | if ($this->readLength > $this->entry->getCompressedSize()) { |
||
| 92 | $this->buffer = substr($this->buffer, 0, $this->entry->getCompressedSize() - $this->readLength); |
||
| 93 | } |
||
| 94 | |||
| 95 | // read header |
||
| 96 | if ($this->context === null) { |
||
| 97 | /** |
||
| 98 | * @var WinZipAesExtraField|null $winZipExtra |
||
| 99 | */ |
||
| 100 | $winZipExtra = $this->entry->getExtraField(WinZipAesExtraField::HEADER_ID); |
||
| 101 | |||
| 102 | if ($winZipExtra === null) { |
||
| 103 | throw new RuntimeException('$winZipExtra is null'); |
||
| 104 | } |
||
| 105 | $saltSize = $winZipExtra->getSaltSize(); |
||
| 106 | $headerSize = $saltSize + WinZipAesContext::PASSWORD_VERIFIER_SIZE; |
||
| 107 | |||
| 108 | if (\strlen($this->buffer) < $headerSize) { |
||
| 109 | return \PSFS_FEED_ME; |
||
| 110 | } |
||
| 111 | |||
| 112 | $salt = substr($this->buffer, 0, $saltSize); |
||
| 113 | $passwordVerifier = substr($this->buffer, $saltSize, WinZipAesContext::PASSWORD_VERIFIER_SIZE); |
||
| 114 | $password = $this->entry->getPassword(); |
||
| 115 | |||
| 116 | if ($password === null) { |
||
| 117 | throw new RuntimeException('$password is null'); |
||
| 118 | } |
||
| 119 | $this->context = new WinZipAesContext($winZipExtra->getEncryptionStrength(), $password, $salt); |
||
| 120 | unset($password); |
||
| 121 | |||
| 122 | // Verify password. |
||
| 123 | if ($passwordVerifier !== $this->context->getPasswordVerifier()) { |
||
| 124 | throw new ZipAuthenticationException('Invalid password'); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->encBlockPosition = 0; |
||
| 128 | $this->encBlockLength = $this->entry->getCompressedSize() - $headerSize - WinZipAesContext::FOOTER_SIZE; |
||
| 129 | |||
| 130 | $this->buffer = substr($this->buffer, $headerSize); |
||
| 131 | } |
||
| 132 | |||
| 133 | // encrypt data |
||
| 134 | $plainText = ''; |
||
| 135 | $offset = 0; |
||
| 136 | $len = \strlen($this->buffer); |
||
| 137 | $remaining = $this->encBlockLength - $this->encBlockPosition; |
||
| 138 | |||
| 139 | if ($remaining >= WinZipAesContext::BLOCK_SIZE && $len < WinZipAesContext::BLOCK_SIZE) { |
||
| 140 | return \PSFS_FEED_ME; |
||
| 141 | } |
||
| 142 | $limit = min($len, $remaining); |
||
| 143 | |||
| 144 | if ($remaining > $limit && ($limit % WinZipAesContext::BLOCK_SIZE) !== 0) { |
||
| 145 | $limit -= ($limit % WinZipAesContext::BLOCK_SIZE); |
||
| 146 | } |
||
| 147 | |||
| 148 | while ($offset < $limit) { |
||
| 149 | $this->context->updateIv(); |
||
| 150 | $length = min(WinZipAesContext::BLOCK_SIZE, $limit - $offset); |
||
| 151 | $data = substr($this->buffer, 0, $length); |
||
| 152 | $plainText .= $this->context->decryption($data); |
||
| 153 | $offset += $length; |
||
| 154 | $this->buffer = substr($this->buffer, $length); |
||
| 155 | } |
||
| 156 | $this->encBlockPosition += $offset; |
||
| 157 | |||
| 158 | if ( |
||
| 159 | $this->encBlockPosition === $this->encBlockLength && |
||
| 160 | \strlen($this->buffer) === WinZipAesContext::FOOTER_SIZE |
||
| 161 | ) { |
||
| 162 | $this->authenticationCode = $this->buffer; |
||
| 163 | $this->buffer = ''; |
||
| 164 | } |
||
| 165 | |||
| 166 | $bucket->data = $plainText; |
||
| 167 | $consumed += $bucket->datalen; |
||
| 168 | stream_bucket_append($out, $bucket); |
||
| 169 | } |
||
| 170 | |||
| 171 | return \PSFS_PASS_ON; |
||
| 172 | } |
||
| 188 |