Completed
Pull Request — master (#49)
by thomas
16:35
created

Util::normalizeNetwork()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 42
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 14.7677

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 13
nop 2
dl 0
loc 42
ccs 25
cts 32
cp 0.7813
crap 14.7677
rs 5.1234
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 1
        } else if ($network === "RBCC") {
41
            // Regtest is magic
42
            $apiNetwork = "rBCC";
43
            $testnet = true;
44
        } else {
45
            // Default to bitcoin if they make no sense.
46 1
            $apiNetwork = "BTC";
47 1
            $testnet = false;
48
        }
49
50 116
        return [$apiNetwork, $testnet];
51
    }
52
53
    /**
54
     * normalize network string
55
     *
56
     * @param $network
57
     * @param $testnet
58
     * @return array
59
     * @throws \Exception
60
     */
61 116
    public static function normalizeNetwork($network, $testnet) {
62 116
        switch (strtolower($network)) {
63 116
            case 'btc':
64 13
            case 'bitcoin':
65 116
                $network = 'bitcoin';
66 116
                break;
67
68 13
            case 'tbtc':
69 10
            case 'bitcoin-testnet':
70 3
                $network = 'bitcoin';
71 3
                $testnet = true;
72 3
                break;
73 10
            case 'bcc':
74 5
            case 'bitcoincash':
75 5
                $network = 'bitcoincash';
76 5
                break;
77
78 5
            case 'tbcc':
79 2
            case 'bitcoincash-testnet':
80 3
                $network = 'bitcoincash';
81 3
                $testnet = true;
82 3
                break;
83
84 2
            case 'rbtc':
85
            case 'bitcoin-regtest':
86 2
                $network = 'bitcoin';
87 2
                $testnet = true;
88 2
                break;
89
90
            case 'rbcc':
91
            case 'bitcoincash-regtest':
92
                $network = 'bitcoincash';
93
                $testnet = true;
94
                break;
95
96
            default:
97
                throw new \Exception("Unknown network [{$network}]");
98
            // this comment silences a phpcs error.
99
        }
100
101 116
        return [$network, $testnet];
102
    }
103
104 26
    public static function arrayMapWithIndex(callable $fn, $arr) {
105 26
        $result = [];
106 26
        $assoc = null;
107 26
        foreach ($arr as $idx => $value) {
108 26
            list($newidx, $newvalue) = $fn($idx, $value);
109
110 26
            if ($assoc === null) {
111 26
                $assoc = $newidx !== null;
112
            }
113
114 26
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
115
                throw new \Exception("Mix of non assoc and assoc keys");
116
            }
117
118 26
            if ($assoc) {
119 26
                $result[$newidx] = $newvalue;
120
            } else {
121 26
                $result[] = $newvalue;
122
            }
123
        }
124
125 26
        return $result;
126
    }
127
}
128