Conditions | 9 |
Paths | 21 |
Total Lines | 53 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 2 | 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 |
||
29 | public static function decode(&$text, bool $ignore = false): bool|string |
||
|
|||
30 | { |
||
31 | $crc = ''; |
||
32 | // Extract the yEnc string itself. |
||
33 | if (preg_match( |
||
34 | '/=ybegin.*size=([^ $]+).*\\r\\n(.*)\\r\\n=yend.*size=([^ $\\r\\n]+)(.*)/ims', |
||
35 | $text, |
||
36 | $encoded |
||
37 | )) { |
||
38 | if (preg_match('/crc32=([^ $\\r\\n]+)/ims', $encoded[4], $trailer)) { |
||
39 | $crc = trim($trailer[1]); |
||
40 | } |
||
41 | |||
42 | [$headerSize, $encoded, $trailerSize] = $encoded; |
||
43 | } else { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | // Remove line breaks from the string. |
||
48 | $encoded = trim(str_replace("\r\n", '', $encoded)); |
||
49 | |||
50 | // Make sure the header and trailer file sizes match up. |
||
51 | if ($headerSize !== $trailerSize) { |
||
52 | $message = 'Header and trailer file sizes do not match. This is a violation of the yEnc specification.'; |
||
53 | throw new \RuntimeException($message); |
||
54 | } |
||
55 | |||
56 | // Decode. |
||
57 | $decoded = ''; |
||
58 | $encodedLength = \strlen($encoded); |
||
59 | for ($chr = 0; $chr < $encodedLength; $chr++) { |
||
60 | $decoded .= ( |
||
61 | $encoded[$chr] === '=' ? |
||
62 | \chr((\ord($encoded[$chr]) - 42) % 256) : |
||
63 | \chr((((\ord($encoded[++$chr]) - 64) % 256) - 42) % 256) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | // Make sure the decoded file size is the same as the size specified in the header. |
||
68 | if (\strlen($decoded) !== $headerSize) { |
||
69 | $message = 'Header file size ('.$headerSize.') and actual file size ('.\strlen($decoded).') do not match. The file is probably corrupt.'; |
||
70 | |||
71 | throw new \RuntimeException($message); |
||
72 | } |
||
73 | |||
74 | // Check the CRC value |
||
75 | if ($crc !== '' && (strtolower($crc) !== strtolower(sprintf('%04X', crc32($decoded))))) { |
||
76 | $message = 'CRC32 checksums do not match. The file is probably corrupt.'; |
||
77 | |||
78 | throw new \RuntimeException($message); |
||
79 | } |
||
80 | |||
81 | return $decoded; |
||
82 | } |
||
177 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.