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 int |
||
| 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 int $flags |
||
| 69 | */ |
||
| 70 | 132 | public function __construct(Math $math, array $vFilter, $numHashFuncs, $nTweak, $flags) |
|
| 71 | { |
||
| 72 | 132 | $this->math = $math; |
|
| 73 | 132 | $this->vFilter = $vFilter; |
|
| 74 | 132 | $this->numHashFuncs = $numHashFuncs; |
|
| 75 | 132 | $this->nTweak = $nTweak; |
|
| 76 | 132 | $this->flags = $flags; |
|
| 77 | 132 | $this->updateEmptyFull(); |
|
| 78 | 132 | } |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param int $size |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 102 | 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 int $flags |
||
| 98 | * @return BloomFilter |
||
| 99 | */ |
||
| 100 | 102 | public static function create(Math $math, $nElements, $nFpRate, $nTweak, $flags) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function isUpdateNone() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function isUpdateAll() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function isUpdatePubKeyOnly() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function isEmpty() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function isFull() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getData() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function getNumHashFuncs() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return int|string |
||
| 171 | */ |
||
| 172 | public function getTweak() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function getFlags() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param int $nElements |
||
| 187 | * @param float $fpRate |
||
| 188 | * @return int |
||
| 189 | 24 | */ |
|
| 190 | public static function idealSize($nElements, $fpRate) |
||
| 214 | 102 | ||
| 215 | 102 | /** |
|
| 216 | * @param int $filterSize |
||
| 217 | 102 | * @param int $nElements |
|
| 218 | 102 | * @return int |
|
| 219 | */ |
||
| 220 | 102 | public static function idealNumHashFuncs($filterSize, $nElements) |
|
| 241 | |||
| 242 | 102 | /** |
|
| 243 | 102 | * @param int $nHashNum |
|
| 244 | 102 | * @param BufferInterface $data |
|
| 245 | * @return string |
||
| 246 | 102 | */ |
|
| 247 | 102 | public function hash($nHashNum, BufferInterface $data) |
|
| 254 | |||
| 255 | /** |
||
| 256 | 78 | * @param BufferInterface $data |
|
| 257 | * @return $this |
||
| 258 | 78 | */ |
|
| 259 | 78 | public function insertData(BufferInterface $data) |
|
| 273 | |||
| 274 | 78 | /** |
|
| 275 | 78 | * @param OutPointInterface $outPoint |
|
| 276 | 78 | * @return BloomFilter |
|
| 277 | 78 | */ |
|
| 278 | public function insertOutPoint(OutPointInterface $outPoint) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param BufferInterface $data |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | 24 | public function containsData(BufferInterface $data) |
|
| 307 | 72 | ||
| 308 | /** |
||
| 309 | 72 | * @param OutPointInterface $outPoint |
|
| 310 | 72 | * @return bool |
|
| 311 | */ |
||
| 312 | 72 | public function containsOutPoint(OutPointInterface $outPoint) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public function hasAcceptableSize() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param TransactionInterface $tx |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | 6 | public function isRelevantAndUpdate(TransactionInterface $tx) |
|
| 385 | 30 | ||
| 386 | 30 | /** |
|
| 387 | 6 | * |
|
| 388 | */ |
||
| 389 | 30 | public function updateEmptyFull() |
|
| 402 | 132 | ||
| 403 | 132 | /** |
|
| 404 | 132 | * @return BufferInterface |
|
| 405 | 132 | */ |
|
| 406 | 132 | public function getBuffer() |
|
| 410 | } |
||
| 411 |