Completed
Pull Request — master (#99)
by thomas
42:14 queued 39:21
created

BitcoinCashAddressReader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B fromString() 0 15 6
B fromOutputScript() 0 24 5
1
<?php
2
3
namespace Blocktrail\SDK\Address;
4
5
use BitWasp\Bitcoin\Address\Base58AddressInterface;
6
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
7
use BitWasp\Bitcoin\Address\ScriptHashAddress;
8
use BitWasp\Bitcoin\Bitcoin;
9
use BitWasp\Bitcoin\Network\NetworkInterface;
10
use BitWasp\Bitcoin\Script\Classifier\OutputClassifier;
11
use BitWasp\Bitcoin\Script\ScriptInterface;
12
use BitWasp\Bitcoin\Script\ScriptType;
13
use BitWasp\Buffertools\BufferInterface;
14
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
15
use Blocktrail\SDK\Network\BitcoinCashNetworkInterface;
16
17
class BitcoinCashAddressReader extends AddressReaderBase
18
{
19
    /**
20
     * @var bool
21
     */
22
    private $useNewCashAddress;
23
24
    /**
25
     * BitcoinCashAddressReader constructor.
26
     * @param bool $useNewCashAddress
27
     */
28 6
    public function __construct($useNewCashAddress) {
29 6
        $this->useNewCashAddress = (bool) $useNewCashAddress;
30 6
    }
31
32
    /**
33
     * @param string $strAddress
34
     * @param NetworkInterface|null $network
35
     * @return Base58AddressInterface|CashAddress
36
     * @throws BlocktrailSDKException
37
     */
38 6
    public function fromString($strAddress, NetworkInterface $network = null) {
39 6
        $network = $network ?: Bitcoin::getNetwork();
40
41 6
        if (($base58Address = $this->readBase58($strAddress, $network))) {
42 4
            return $base58Address;
43
        }
44
45 4
        if ($this->useNewCashAddress && $network instanceof BitcoinCashNetworkInterface) {
46 4
            if (($base32Address = $this->readBase32($strAddress, $network))) {
47 4
                return $base32Address;
48
            }
49
        }
50
51
        throw new BlocktrailSDKException("Address not recognized");
52
    }
53
54
    /**
55
     * @param ScriptInterface $script
56
     * @return Base58AddressInterface|CashAddress
57
     */
58 2
    public function fromOutputScript(ScriptInterface $script) {
59 2
        $decode = (new OutputClassifier())->decode($script);
60
61 2
        switch ($decode->getType()) {
62 2
            case ScriptType::P2PKH:
63
                /** @var BufferInterface $solution */
64
                if ($this->useNewCashAddress) {
65
                    return new CashAddress(ScriptType::P2PKH, $decode->getSolution());
66
                } else {
67
                    return new PayToPubKeyHashAddress($decode->getSolution());
68
                }
69
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70 2
            case ScriptType::P2SH:
71
                /** @var BufferInterface $solution */
72 2
                if ($this->useNewCashAddress) {
73 1
                    return new CashAddress(ScriptType::P2SH, $decode->getSolution());
74
                } else {
75 1
                    return new ScriptHashAddress($decode->getSolution());
76
                }
77
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
78
            default:
79
                throw new \RuntimeException('Script type is not associated with an address');
80
        }
81
    }
82
}
83