Completed
Branch master (82f6b9)
by
unknown
06:14
created

Util::normalizeNetwork()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 28
nc 11
nop 2
dl 0
loc 36
rs 5.2653
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
    public static function all(callable $fn, array $arr) {
7
        $allvalues = array_map($fn, $arr);
8
        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
    public static function parseApiNetwork($network, $testnet) {
22
        $network = strtoupper($network);
23
24
        // deal with testnet special cases
25
        if (strlen($network) === 4 && $network[0] === 'T') {
26
            $testnet = true;
27
            $network = substr($network, 1);
28
        }
29
30
        if (strlen($network) === 3) {
31
            // work out apiNetwork
32
            $apiNetwork = $network;
33
            if ($testnet) {
34
                $apiNetwork = "t{$network}";
35
            }
36
        } else if ($network === "RBTC") {
37
            // Regtest is magic
38
            $apiNetwork = "rBTC";
39
            $testnet = true;
40
        } else {
41
            // Default to bitcoin if they make no sense.
42
            $apiNetwork = "BTC";
43
            $testnet = false;
44
        }
45
46
        return [$apiNetwork, $testnet];
47
    }
48
49
    /**
50
     * normalize network string
51
     *
52
     * @param $network
53
     * @param $testnet
54
     * @return array
55
     * @throws \Exception
56
     */
57
    public static function normalizeNetwork($network, $testnet) {
58
        switch (strtolower($network)) {
59
            case 'btc':
60
            case 'bitcoin':
61
                $network = 'bitcoin';
62
                break;
63
64
            case 'tbtc':
65
            case 'bitcoin-testnet':
66
                $network = 'bitcoin';
67
                $testnet = true;
68
                break;
69
            case 'bcc':
70
            case 'bitcoincash':
71
                $network = 'bitcoincash';
72
                break;
73
74
            case 'tbcc':
75
            case 'bitcoincash-testnet':
76
                $network = 'bitcoincash';
77
                $testnet = true;
78
                break;
79
80
            case 'rbtc':
81
            case 'bitcoin-regtest':
82
                $network = 'bitcoin';
83
                $testnet = true;
84
                break;
85
86
            default:
87
                throw new \Exception("Unknown network [{$network}]");
88
            // this comment silences a phpcs error.
89
        }
90
91
        return [$network, $testnet];
92
    }
93
94
    public static function arrayMapWithIndex(callable $fn, $arr) {
95
        $result = [];
96
        $assoc = null;
97
        foreach ($arr as $idx => $value) {
98
            list($newidx, $newvalue) = $fn($idx, $value);
99
100
            if ($assoc === null) {
101
                $assoc = $newidx !== null;
102
            }
103
104
            if ($newidx === null && $assoc || $newidx !== null && !$assoc) {
105
                throw new \Exception("Mix of non assoc and assoc keys");
106
            }
107
108
            if ($assoc) {
109
                $result[$newidx] = $newvalue;
110
            } else {
111
                $result[] = $newvalue;
112
            }
113
        }
114
115
        return $result;
116
    }
117
}
118