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

AlipayConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
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 10
dl 0
loc 12
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 AlipayConfig implements ConfigInterface
10
{
11
    public function __construct(
12
        public string $app_id,
13
        public string $app_secret_cert,
14
        public string $app_public_cert_path,
15
        public string $alipay_public_cert_path,
16
        public string $alipay_root_cert_path,
17
        public string $return_url = '',
18
        public string $notify_url = '',
19
        public string $app_auth_token = '',
20
        public string $service_provider_id = '',
21
        public int $mode = Pay::MODE_NORMAL,
22
    ) {
23
    }
24
25
    public function toArray(): array
26
    {
27
        return [
28
            'app_id' => $this->app_id,
29
            'app_secret_cert' => $this->app_secret_cert,
30
            'app_public_cert_path' => $this->app_public_cert_path,
31
            'alipay_public_cert_path' => $this->alipay_public_cert_path,
32
            'alipay_root_cert_path' => $this->alipay_root_cert_path,
33
            'return_url' => $this->return_url,
34
            'notify_url' => $this->notify_url,
35
            'app_auth_token' => $this->app_auth_token,
36
            'service_provider_id' => $this->service_provider_id,
37
            'mode' => $this->mode,
38
        ];
39
    }
40
41
    public static function fromArray(array $config): self
42
    {
43
        return new self(
44
            app_id: $config['app_id'] ?? '',
45
            app_secret_cert: $config['app_secret_cert'] ?? '',
46
            app_public_cert_path: $config['app_public_cert_path'] ?? '',
47
            alipay_public_cert_path: $config['alipay_public_cert_path'] ?? '',
48
            alipay_root_cert_path: $config['alipay_root_cert_path'] ?? '',
49
            return_url: $config['return_url'] ?? '',
50
            notify_url: $config['notify_url'] ?? '',
51
            app_auth_token: $config['app_auth_token'] ?? '',
52
            service_provider_id: $config['service_provider_id'] ?? '',
53
            mode: $config['mode'] ?? Pay::MODE_NORMAL,
54
        );
55
    }
56
}
57