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

WechatConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
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 15
dl 0
loc 17
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
use Yansongda\Pay\Pay;
8
9
class WechatConfig implements ConfigInterface
10
{
11
    public function __construct(
12
        public string $mch_id,
13
        public string $mch_secret_key,
14
        public string $mch_secret_cert,
15
        public string $mch_public_cert_path,
16
        public string $notify_url,
17
        public string $mch_secret_key_v2 = '',
18
        public string $mp_app_id = '',
19
        public string $mini_app_id = '',
20
        public string $app_id = '',
21
        public string $sub_mp_app_id = '',
22
        public string $sub_app_id = '',
23
        public string $sub_mini_app_id = '',
24
        public string $sub_mch_id = '',
25
        public array $wechat_public_cert_path = [],
26
        public int $mode = Pay::MODE_NORMAL,
27
    ) {
28
    }
29
30
    public function toArray(): array
31
    {
32
        return [
33
            'mch_id' => $this->mch_id,
34
            'mch_secret_key_v2' => $this->mch_secret_key_v2,
35
            'mch_secret_key' => $this->mch_secret_key,
36
            'mch_secret_cert' => $this->mch_secret_cert,
37
            'mch_public_cert_path' => $this->mch_public_cert_path,
38
            'notify_url' => $this->notify_url,
39
            'mp_app_id' => $this->mp_app_id,
40
            'mini_app_id' => $this->mini_app_id,
41
            'app_id' => $this->app_id,
42
            'sub_mp_app_id' => $this->sub_mp_app_id,
43
            'sub_app_id' => $this->sub_app_id,
44
            'sub_mini_app_id' => $this->sub_mini_app_id,
45
            'sub_mch_id' => $this->sub_mch_id,
46
            'wechat_public_cert_path' => $this->wechat_public_cert_path,
47
            'mode' => $this->mode,
48
        ];
49
    }
50
51
    public static function fromArray(array $config): self
52
    {
53
        return new self(
54
            mch_id: $config['mch_id'] ?? '',
55
            mch_secret_key: $config['mch_secret_key'] ?? '',
56
            mch_secret_cert: $config['mch_secret_cert'] ?? '',
57
            mch_public_cert_path: $config['mch_public_cert_path'] ?? '',
58
            notify_url: $config['notify_url'] ?? '',
59
            mch_secret_key_v2: $config['mch_secret_key_v2'] ?? '',
60
            mp_app_id: $config['mp_app_id'] ?? '',
61
            mini_app_id: $config['mini_app_id'] ?? '',
62
            app_id: $config['app_id'] ?? '',
63
            sub_mp_app_id: $config['sub_mp_app_id'] ?? '',
64
            sub_app_id: $config['sub_app_id'] ?? '',
65
            sub_mini_app_id: $config['sub_mini_app_id'] ?? '',
66
            sub_mch_id: $config['sub_mch_id'] ?? '',
67
            wechat_public_cert_path: $config['wechat_public_cert_path'] ?? [],
68
            mode: $config['mode'] ?? Pay::MODE_NORMAL,
69
        );
70
    }
71
}
72