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 |
||
| 8 | class Bech32 |
||
| 9 | { |
||
| 10 | protected static $CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; |
||
| 11 | |||
| 12 | protected static $CHARSET_REV = [ |
||
| 13 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 14 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 15 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
||
| 16 | 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, |
||
| 17 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
| 18 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, |
||
| 19 | -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, |
||
| 20 | 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param int[] $values |
||
| 25 | * @param int $numValues |
||
| 26 | * @return int |
||
| 27 | */ |
||
| 28 | 46 | public static function polyMod(array $values, $numValues) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param string $hrp |
||
| 47 | * @param int $hrpLen |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | 46 | public static function hrpExpand($hrp, $hrpLen) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $hrp |
||
| 65 | * @param int[] $convertedDataChars |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | 46 | public static function verifyChecksum($hrp, array $convertedDataChars) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $sBech |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | 48 | public static function decodeCheck($sBech) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param array $data |
||
| 142 | * @param int $inLen |
||
| 143 | * @param int $fromBits |
||
| 144 | * @param int $toBits |
||
| 145 | * @param bool $pad |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | 32 | public static function convertBits(array $data, $inLen, $fromBits, $toBits, $pad = true) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $hrp |
||
| 184 | * @param int[] $convertedDataChars |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | 12 | public static function createChecksum($hrp, array $convertedDataChars) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $hrp |
||
| 201 | * @param array $combinedDataChars |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 12 | public static function encode($hrp, array $combinedDataChars) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $hrp |
||
| 218 | * @param WitnessProgram $witnessProgram |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 12 | public static function encodeSegwit($hrp, WitnessProgram $witnessProgram) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $hrp |
||
| 240 | * @param string $bech32 |
||
| 241 | * @return WitnessProgram |
||
| 242 | */ |
||
| 243 | 38 | public static function decodeSegwit($hrp, $bech32) |
|
| 272 | } |
||
| 273 |