Complex classes like BloomFilter 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 BloomFilter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class BloomFilter extends Serializable |
||
| 16 | { |
||
| 17 | const LN2SQUARED = '0.4804530139182014246671025263266649717305529515945455'; |
||
| 18 | const LN2 = '0.6931471805599453094172321214581765680755001343602552'; |
||
| 19 | const MAX_HASH_FUNCS = '50'; |
||
| 20 | const MAX_FILTER_SIZE = 36000; // bytes |
||
| 21 | const TWEAK_START = '4221880213'; // 0xFBA4C795 |
||
| 22 | |||
| 23 | const UPDATE_NONE = 0; |
||
| 24 | const UPDATE_ALL = 1; |
||
| 25 | const UPDATE_P2PUBKEY_ONLY = 2; |
||
| 26 | const UPDATE_MASK = 3; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Math |
||
| 30 | */ |
||
| 31 | private $math; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | private $empty = true; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | private $full = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $numHashFuncs; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $vFilter = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int|string |
||
| 55 | */ |
||
| 56 | private $nTweak; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Flags |
||
| 60 | */ |
||
| 61 | private $flags; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Math $math |
||
| 65 | * @param array $vFilter |
||
| 66 | * @param int $numHashFuncs |
||
| 67 | * @param int|string $nTweak |
||
| 68 | * @param Flags $flags |
||
| 69 | 132 | */ |
|
| 70 | public function __construct(Math $math, array $vFilter, $numHashFuncs, $nTweak, Flags $flags) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $size |
||
| 82 | * @return array |
||
| 83 | 102 | */ |
|
| 84 | public static function emptyFilter($size) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Create the Bloom Filter given the number of elements, a false positive rate, |
||
| 91 | * and the flags governing how the filter should be updated. |
||
| 92 | * |
||
| 93 | * @param Math $math |
||
| 94 | * @param int $nElements |
||
| 95 | * @param float $nFpRate |
||
| 96 | * @param int $nTweak |
||
| 97 | * @param Flags $flags |
||
| 98 | * @return BloomFilter |
||
| 99 | 102 | */ |
|
| 100 | public static function create(Math $math, $nElements, $nFpRate, $nTweak, Flags $flags) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param int $flag |
||
| 115 | * @return bool |
||
| 116 | 36 | */ |
|
| 117 | 4 | public function checkFlag($flag) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return bool |
||
| 124 | 6 | */ |
|
| 125 | public function isUpdateNone() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return bool |
||
| 132 | 36 | */ |
|
| 133 | public function isUpdateAll() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return bool |
||
| 140 | 24 | */ |
|
| 141 | public function isUpdatePubKeyOnly() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return bool |
||
| 148 | 96 | */ |
|
| 149 | public function isEmpty() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return bool |
||
| 156 | 120 | */ |
|
| 157 | public function isFull() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | 24 | */ |
|
| 165 | public function getData() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return int |
||
| 172 | 24 | */ |
|
| 173 | public function getNumHashFuncs() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return int|string |
||
| 180 | 24 | */ |
|
| 181 | public function getTweak() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return Flags |
||
| 188 | 24 | */ |
|
| 189 | public function getFlags() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $nElements |
||
| 196 | * @param float $fpRate |
||
| 197 | * @return int |
||
| 198 | 102 | */ |
|
| 199 | public static function idealSize($nElements, $fpRate) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param int $filterSize |
||
| 226 | * @param int $nElements |
||
| 227 | * @return int |
||
| 228 | 102 | */ |
|
| 229 | public static function idealNumHashFuncs($filterSize, $nElements) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param int $nHashNum |
||
| 253 | * @param Buffer $data |
||
| 254 | * @return string |
||
| 255 | 78 | */ |
|
| 256 | public function hash($nHashNum, Buffer $data) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param Buffer $data |
||
| 266 | * @return $this |
||
| 267 | 84 | */ |
|
| 268 | public function insertData(Buffer $data) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param OutPointInterface $outPoint |
||
| 285 | * @return BloomFilter |
||
| 286 | */ |
||
| 287 | 24 | public function insertOutPoint(OutPointInterface $outPoint) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param Buffer $data |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | 42 | public function containsData(Buffer $data) |
|
| 316 | 72 | ||
| 317 | /** |
||
| 318 | 72 | * @param OutPointInterface $outPoint |
|
| 319 | 72 | * @return bool |
|
| 320 | */ |
||
| 321 | 72 | public function containsOutPoint(OutPointInterface $outPoint) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public function hasAcceptableSize() |
||
| 333 | 30 | ||
| 334 | /** |
||
| 335 | * @param TransactionInterface $tx |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function isRelevantAndUpdate(TransactionInterface $tx) |
||
| 394 | 30 | ||
| 395 | 30 | /** |
|
| 396 | 30 | * |
|
| 397 | 6 | */ |
|
| 398 | public function updateEmptyFull() |
||
| 411 | 132 | ||
| 412 | 132 | /** |
|
| 413 | 132 | * @return Buffer |
|
| 414 | 132 | */ |
|
| 415 | 132 | public function getBuffer() |
|
| 419 | } |
||
| 420 |