Complex classes like Bech32 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Bech32, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Bech32 |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected static $charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected static $charsetKey = [ |
||
| 20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 21 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 22 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 23 | 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, |
||
| 24 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
| 25 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, |
||
| 26 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
| 27 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected static $generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param int[] $values |
||
| 37 | * @param int $numValues |
||
| 38 | * @return int |
||
| 39 | */ |
||
| 40 | public static function polyMod(array $values, $numValues) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Expands the human readable part into a character array for checksumming. |
||
| 58 | * @param string $hrp |
||
| 59 | * @param int $hrpLen |
||
| 60 | * @return array |
||
| 61 | */ |
||
| 62 | public static function hrpExpand($hrp, $hrpLen) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Converts words of $fromBits bits to $toBits bits in size. |
||
| 77 | * |
||
| 78 | * @param int[] $data - character array of data to convert |
||
| 79 | * @param int $inLen - number of elements in array |
||
| 80 | * @param int $fromBits - word (bit count) size of provided data |
||
| 81 | * @param int $toBits - requested word size (bit count) |
||
| 82 | * @param bool $pad - whether to pad (only when encoding) |
||
| 83 | * @return array |
||
| 84 | * @throws Bech32Exception |
||
| 85 | */ |
||
| 86 | public static function convertBits(array $data, $inLen, $fromBits, $toBits, $pad = true) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $hrp |
||
| 122 | * @param int[] $convertedDataChars |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public static function createChecksum($hrp, array $convertedDataChars) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Verifies the checksum given $hrp and $convertedDataChars. |
||
| 139 | * |
||
| 140 | * @param string $hrp |
||
| 141 | * @param int[] $convertedDataChars |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public static function verifyChecksum($hrp, array $convertedDataChars) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param string $hrp |
||
| 154 | * @param array $combinedDataChars |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public static function encode($hrp, array $combinedDataChars) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Validates a bech32 string and returns [$hrp, $dataChars] if |
||
| 172 | * the conversion was successful. An exception is thrown on invalid |
||
| 173 | * data. |
||
| 174 | * |
||
| 175 | * @param string $sBech - the bech32 encoded string |
||
| 176 | * @return array - returns [$hrp, $dataChars] |
||
| 177 | * @throws Bech32Exception |
||
| 178 | */ |
||
| 179 | public static function decode($sBech) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Takes the $hrp and $witnessProgram and produces a native segwit |
||
| 241 | * address. |
||
| 242 | * |
||
| 243 | * @param string $hrp |
||
| 244 | * @param WitnessProgram $witnessProgram |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public static function encodeSegwit($hrp, WitnessProgram $witnessProgram) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Decodes the provided $bech32 string, validating against |
||
| 258 | * the chosen prefix. |
||
| 259 | * |
||
| 260 | * @param string $hrp |
||
| 261 | * @param string $bech32 |
||
| 262 | * @return WitnessProgram |
||
| 263 | * @throws Bech32Exception |
||
| 264 | */ |
||
| 265 | public static function decodeSegwit($hrp, $bech32) |
||
| 294 | } |
||
| 295 |