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