Completed
Pull Request — master (#49)
by thomas
97:33 queued 28:50
created

Util::normalizeNetwork()   C

Complexity

Conditions 15
Paths 15

Size

Total Lines 47
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 15.185

Importance

Changes 0
Metric Value
cc 15
eloc 38
nc 15
nop 2
dl 0
loc 47
ccs 29
cts 32
cp 0.9063
crap 15.185
rs 5.0256
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Blocktrail\SDK;
4
5
abstract class Util {
6 4
    public static function all(callable $fn, array $arr) {
7 4
        $allvalues = array_map($fn, $arr);
8 4
        return count(array_unique($allvalues)) === 1 && end($allvalues) === true;
9
    }
10
11
    /**
12
     * Given a $network string and $testnet bool, this function
13
     * will apply the old method (conditionally prepending 't'
14
     * to the network if ($testnet).. but also some new special
15
     * cases which ignore the testnet setting.
16
     *
17
     * @param string $network
18
     * @param bool $testnet
19
     * @return array
20
     */
21 116
    public static function parseApiNetwork($network, $testnet) {
22 116
        $network = strtoupper($network);
23
24
        // deal with testnet special cases
25 116
        if (strlen($network) === 4 && $network[0] === 'T') {
26 4
            $testnet = true;
27 4
            $network = substr($network, 1);
28
        }
29
30 116
        if (strlen($network) === 3) {
31
            // work out apiNetwork
32 116
            $apiNetwork = $network;
33 116
            if ($testnet) {
34 116
                $apiNetwork = "t{$network}";
35
            }
36 3
        } else if ($network === "RBTC") {
37
            // Regtest is magic
38 2
            $apiNetwork = "rBTC";
39 2
            $testnet = true;
40
        } else if ($network === "RBCH") {
41
            // Regtest is magic
42 1
            $apiNetwork = "rBCH";
43 1
            $testnet = true;
44
        } else {
45
            // Default to bitcoin if they make no sense.
46 116
            $apiNetwork = "BTC";
47
            $testnet = false;
48
        }
49
50
        return [$apiNetwork, $testnet];
51
    }
52
53
    /**
54
     * normalize network string
55
     *
56
     * @param $network
57 116
     * @param $testnet
58 116
     * @return array
59 116
     * @throws \Exception
60 13
     */
61 116
    public static function normalizeNetwork($network, $testnet) {
62 116
        $regtest = false;
63
        switch (strtolower($network)) {
64 13
            case 'btc':
65 10
            case 'bitcoin':
66 3
                $network = 'bitcoin';
67 3
                break;
68 3
69 10
            case 'tbtc':
70 5
            case 'bitcoin-testnet':
71 5
                $network = 'bitcoin';
72 5
                $testnet = true;
73
                break;
74 5
            case 'bcc':
75 2
            case 'bch':
76 3
            case 'bitcoincash':
77 3
                $network = 'bitcoincash';
78 3
                break;
79
80 2
            case 'tbch':
81
            case 'tbcc':
82 2
            case 'bitcoincash-testnet':
83 2
                $network = 'bitcoincash';
84 2
                $testnet = true;
85
                break;
86
87
            case 'rbtc':
88
            case 'bitcoin-regtest':
89
                $network = 'bitcoin';
90
                $testnet = true;
91 116
                $regtest = true;
92
                break;
93
94 26
            case 'rbch':
95 26
            case 'bitcoincash-regtest':
96 26
                $network = 'bitcoincash';
97 26
                $testnet = true;
98 26
                $regtest = true;
99
                break;
100 26
101 26
            default:
102
                throw new \Exception("Unknown network [{$network}]");
103
            // this comment silences a phpcs error.
104 26
        }
105
106
        return [$network, $testnet, $regtest];
107
    }
108 26
109 26
    public static function arrayMapWithIndex(callable $fn, $arr) {
110
        $result = [];
111 26
        $assoc = null;
112
        foreach ($arr as $idx => $value) {
113
            list($newidx, $newvalue) = $fn($idx, $value);
114
115 26
            if ($assoc === null) {
116
                $assoc = $newidx !== null;
117
            }
118
119
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
120
                throw new \Exception("Mix of non assoc and assoc keys");
121
            }
122
123
            if ($assoc) {
124
                $result[$newidx] = $newvalue;
125
            } else {
126
                $result[] = $newvalue;
127
            }
128
        }
129
130
        return $result;
131
    }
132
}
133