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

UnipayConfig::__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
use Yansongda\Pay\Pay;
8
9
class UnipayConfig implements ConfigInterface
10
{
11
    public function __construct(
12
        public string $mch_id,
13
        public string $mch_cert_path,
14
        public string $mch_cert_password,
15
        public string $unipay_public_cert_path,
16
        public string $return_url,
17
        public string $notify_url,
18
        public string $mch_secret_key = '',
19
        public int $mode = Pay::MODE_NORMAL,
20
    ) {
21
    }
22
23
    public function toArray(): array
24
    {
25
        return [
26
            'mch_id' => $this->mch_id,
27
            'mch_secret_key' => $this->mch_secret_key,
28
            'mch_cert_path' => $this->mch_cert_path,
29
            'mch_cert_password' => $this->mch_cert_password,
30
            'unipay_public_cert_path' => $this->unipay_public_cert_path,
31
            'return_url' => $this->return_url,
32
            'notify_url' => $this->notify_url,
33
            'mode' => $this->mode,
34
        ];
35
    }
36
37
    public static function fromArray(array $config): self
38
    {
39
        return new self(
40
            mch_id: $config['mch_id'] ?? '',
41
            mch_cert_path: $config['mch_cert_path'] ?? '',
42
            mch_cert_password: $config['mch_cert_password'] ?? '',
43
            unipay_public_cert_path: $config['unipay_public_cert_path'] ?? '',
44
            return_url: $config['return_url'] ?? '',
45
            notify_url: $config['notify_url'] ?? '',
46
            mch_secret_key: $config['mch_secret_key'] ?? '',
47
            mode: $config['mode'] ?? Pay::MODE_NORMAL,
48
        );
49
    }
50
}
51