Completed
Pull Request — master (#127)
by thomas
67:16 queued 63:10
created

WalletV3Sweeper::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 8
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
crap 3.054
rs 9.7
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Blocktrail\SDK;
4
5
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
6
use BitWasp\Buffertools\BufferInterface;
7
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
8
use Btccom\JustEncrypt\Encryption;
9
use Btccom\JustEncrypt\EncryptionMnemonic;
10
11
class WalletV3Sweeper extends WalletSweeper
12
{
13
    /**
14
     * @param string              $encryptedPrimaryMnemonic
15
     * @param string              $encryptedSecretMnemonic
16
     * @param string              $backupMnemonic
17
     * @param BufferInterface     $passphrase
18
     * @param array               $blocktrailPublicKeys
19
     * @param UnspentOutputFinder $utxoFinder
20
     * @param string              $network
21
     * @param bool                $testnet
22
     * @throws BlocktrailSDKException
23
     */
24 1
    public function __construct($encryptedPrimaryMnemonic, $encryptedSecretMnemonic, $backupMnemonic, BufferInterface $passphrase, array $blocktrailPublicKeys, UnspentOutputFinder $utxoFinder, $network = 'btc', $testnet = false) {
25
        // cleanup copy paste errors from mnemonics
26 1
        $encryptedPrimaryMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedPrimaryMnemonic))));
27 1
        $encryptedSecretMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedSecretMnemonic))));
28 1
        $backupMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupMnemonic))));
29
30 1
        if (!($secret = Encryption::decrypt(EncryptionMnemonic::decode($encryptedSecretMnemonic), $passphrase))) {
31
            throw new BlocktrailSDKException("Failed to decret password encrypted secret");
32
        }
33
34 1
        if (!($primarySeed = Encryption::decrypt(EncryptionMnemonic::decode($encryptedPrimaryMnemonic), $secret))) {
35
            throw new BlocktrailSDKException("failed to decrypt encrypted primary seed! (weird!)");
36
        }
37
38 1
        $backupMnemonic = MnemonicFactory::bip39()->mnemonicToEntropy($backupMnemonic);
39 1
        parent::__construct($primarySeed, $backupMnemonic, $blocktrailPublicKeys, $utxoFinder, $network, $testnet);
40 1
    }
41
}
42