| Conditions | 16 |
| Paths | 16 |
| Total Lines | 58 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 272 |
| 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 |
||
| 33 | protected function changeOutputFormat($bytes, $direction = true) |
||
| 34 | { |
||
| 35 | if ($this->cipherFormat === self::ENCRYPTION_OUTPUT_RAW) { |
||
|
1 ignored issue
–
show
|
|||
| 36 | return $bytes; |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($direction == true) { |
||
| 40 | switch ($this->cipherFormat) { |
||
| 41 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
||
|
1 ignored issue
–
show
|
|||
| 42 | $bytes = bin2hex($bytes); |
||
| 43 | break; |
||
| 44 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
||
|
1 ignored issue
–
show
|
|||
| 45 | $bytes = StringBuilder::stringToUpper(bin2hex($bytes)); |
||
| 46 | break; |
||
| 47 | case self::ENCRYPTION_OUTPUT_BASE_64: |
||
|
1 ignored issue
–
show
|
|||
| 48 | $bytes = base64_encode($bytes); |
||
| 49 | break; |
||
| 50 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
||
|
1 ignored issue
–
show
|
|||
| 51 | default: |
||
| 52 | $bytes = base64_encode($bytes); |
||
| 53 | $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $bytes); |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | } else { |
||
| 57 | $hexCasePattern = '/^[a-f0-9]+$/'; |
||
| 58 | $base64Pattern = '%^[a-zA-Z0-9/+]*={0,2}$%'; |
||
| 59 | $base64UrlFriendlyPattern = '/^[a-zA-Z0-9_-]+$/'; |
||
| 60 | |||
| 61 | switch ($this->cipherFormat) { |
||
| 62 | case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
||
| 63 | case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
||
| 64 | if (preg_match($hexCasePattern, StringBuilder::stringToLower($bytes))) { |
||
| 65 | $bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
||
| 66 | } |
||
| 67 | break; |
||
| 68 | case self::ENCRYPTION_OUTPUT_BASE_64: |
||
| 69 | if (preg_match($base64Pattern, $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { |
||
| 70 | $bytes = base64_decode($bytes); |
||
| 71 | } |
||
| 72 | break; |
||
| 73 | case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
||
| 74 | default: |
||
| 75 | if (preg_match($base64UrlFriendlyPattern, $bytes)) { |
||
| 76 | $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); |
||
| 77 | $times = StringBuilder::stringLength($bytes) % 4; |
||
| 78 | |||
| 79 | // Instead of str_pad for encoding friendly appending |
||
| 80 | for ($i = 0; $i < $times; $i++) { |
||
| 81 | $bytes .= '='; |
||
| 82 | } |
||
| 83 | |||
| 84 | $bytes = base64_decode($bytes); |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | return $bytes; |
||
| 91 | } |
||
| 136 |