Passed
Pull Request — master (#1124)
by
unknown
03:55 queued 01:03
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 10
rs 10

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
declare(strict_types=1);
4
5
namespace Yansongda\Pay\Config;
6
7
class Config implements ConfigInterface
8
{
9
    public function __construct(
10
        public ?AlipayConfig $alipay = null,
11
        public ?WechatConfig $wechat = null,
12
        public ?UnipayConfig $unipay = null,
13
        public ?DouyinConfig $douyin = null,
14
        public ?JsbConfig $jsb = null,
15
        public ?LoggerConfig $logger = null,
16
        public ?HttpConfig $http = null,
17
        public array $additional = [],
18
    ) {
19
    }
20
21
    public function toArray(): array
22
    {
23
        $config = [];
24
25
        if ($this->alipay !== null) {
26
            $config['alipay'] = ['default' => $this->alipay->toArray()];
27
        }
28
29
        if ($this->wechat !== null) {
30
            $config['wechat'] = ['default' => $this->wechat->toArray()];
31
        }
32
33
        if ($this->unipay !== null) {
34
            $config['unipay'] = ['default' => $this->unipay->toArray()];
35
        }
36
37
        if ($this->douyin !== null) {
38
            $config['douyin'] = ['default' => $this->douyin->toArray()];
39
        }
40
41
        if ($this->jsb !== null) {
42
            $config['jsb'] = ['default' => $this->jsb->toArray()];
43
        }
44
45
        if ($this->logger !== null) {
46
            $config['logger'] = $this->logger->toArray();
47
        }
48
49
        if ($this->http !== null) {
50
            $config['http'] = $this->http->toArray();
51
        }
52
53
        return array_merge($config, $this->additional);
54
    }
55
56
    public static function fromArray(array $config): self
57
    {
58
        $alipay = null;
59
        if (isset($config['alipay']['default'])) {
60
            $alipay = AlipayConfig::fromArray($config['alipay']['default']);
61
        } elseif (isset($config['alipay']) && !isset($config['alipay']['default'])) {
62
            // Handle case where alipay config might be provided directly
63
            $firstKey = array_key_first($config['alipay']);
64
            if (is_array($config['alipay'][$firstKey])) {
65
                $alipay = AlipayConfig::fromArray($config['alipay'][$firstKey]);
66
            }
67
        }
68
69
        $wechat = null;
70
        if (isset($config['wechat']['default'])) {
71
            $wechat = WechatConfig::fromArray($config['wechat']['default']);
72
        } elseif (isset($config['wechat']) && !isset($config['wechat']['default'])) {
73
            $firstKey = array_key_first($config['wechat']);
74
            if (is_array($config['wechat'][$firstKey])) {
75
                $wechat = WechatConfig::fromArray($config['wechat'][$firstKey]);
76
            }
77
        }
78
79
        $unipay = null;
80
        if (isset($config['unipay']['default'])) {
81
            $unipay = UnipayConfig::fromArray($config['unipay']['default']);
82
        } elseif (isset($config['unipay']) && !isset($config['unipay']['default'])) {
83
            $firstKey = array_key_first($config['unipay']);
84
            if (is_array($config['unipay'][$firstKey])) {
85
                $unipay = UnipayConfig::fromArray($config['unipay'][$firstKey]);
86
            }
87
        }
88
89
        $douyin = null;
90
        if (isset($config['douyin']['default'])) {
91
            $douyin = DouyinConfig::fromArray($config['douyin']['default']);
92
        } elseif (isset($config['douyin']) && !isset($config['douyin']['default'])) {
93
            $firstKey = array_key_first($config['douyin']);
94
            if (is_array($config['douyin'][$firstKey])) {
95
                $douyin = DouyinConfig::fromArray($config['douyin'][$firstKey]);
96
            }
97
        }
98
99
        $jsb = null;
100
        if (isset($config['jsb']['default'])) {
101
            $jsb = JsbConfig::fromArray($config['jsb']['default']);
102
        } elseif (isset($config['jsb']) && !isset($config['jsb']['default'])) {
103
            $firstKey = array_key_first($config['jsb']);
104
            if (is_array($config['jsb'][$firstKey])) {
105
                $jsb = JsbConfig::fromArray($config['jsb'][$firstKey]);
106
            }
107
        }
108
109
        $logger = isset($config['logger']) ? LoggerConfig::fromArray($config['logger']) : null;
110
        $http = isset($config['http']) ? HttpConfig::fromArray($config['http']) : null;
111
112
        // Store additional config that doesn't match known structures
113
        $additional = [];
114
        $knownKeys = ['alipay', 'wechat', 'unipay', 'douyin', 'jsb', 'logger', 'http'];
115
        foreach ($config as $key => $value) {
116
            if (!in_array($key, $knownKeys)) {
117
                $additional[$key] = $value;
118
            }
119
        }
120
121
        return new self(
122
            alipay: $alipay,
123
            wechat: $wechat,
124
            unipay: $unipay,
125
            douyin: $douyin,
126
            jsb: $jsb,
127
            logger: $logger,
128
            http: $http,
129
            additional: $additional,
130
        );
131
    }
132
}
133