Completed
Push — master ( 9621c0...4bb787 )
by Tobias
01:47
created

PaymentSetting::toArray()   D

Complexity

Conditions 10
Paths 72

Size

Total Lines 29
Code Lines 18

Duplication

Lines 9
Ratio 31.03 %

Code Coverage

Tests 18
CRAP Score 10

Importance

Changes 0
Metric Value
dl 9
loc 29
ccs 18
cts 18
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 18
nc 72
nop 0
crap 10

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
declare(strict_types=1);
4
5
namespace Billogram\Model\Setting;
6
7
use Billogram\Model\CreatableFromArray;
8
9
/**
10
 * @author Ibrahim Hizeoui <[email protected]>
11
 */
12
class PaymentSetting implements CreatableFromArray
13
{
14
    /**
15
     * @var string
16
     */
17
    private $bankgiro;
18
19
    /**
20
     * @var string
21
     */
22
    private $plusgiro;
23
24
    /**
25
     * @var array
26
     */
27
    private $domesticBankAccount;
28
29
    /**
30
     * @var array
31
     */
32
    private $internationalBankAccount;
33
34
    /**
35
     * @return mixed
36
     */
37
    public function getBankgiro()
38
    {
39
        return $this->bankgiro;
40
    }
41
42
    /**
43
     * @param $bankgiro
44
     *
45
     * @return PaymentSetting
46
     */
47
    public function withBankgiro($bankgiro)
48
    {
49
        $new = clone $this;
50
        $new->bankgiro = $bankgiro;
51
52
        return $new;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getPlusgiro(): string
59
    {
60
        return $this->plusgiro;
61
    }
62
63
    /**
64
     * @param string $plusgiro
65
     *
66
     * @return PaymentSetting
67
     */
68
    public function withPlusgiro(string $plusgiro)
69
    {
70
        $new = clone $this;
71
        $new->plusgiro = $plusgiro;
72
73
        return $new;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function getDomesticBankAccount()
80
    {
81
        return $this->domesticBankAccount;
82
    }
83
84
    /**
85
     * @param $domesticBankAccount
86
     *
87
     * @return PaymentSetting
88
     */
89
    public function withDomesticBankAccount($domesticBankAccount)
90
    {
91
        $new = clone $this;
92
        $new->domesticBankAccount = $domesticBankAccount;
93
94
        return $new;
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getInternationalBankAccount()
101
    {
102
        return $this->internationalBankAccount;
103
    }
104
105
    /**
106
     * @param $internationalBankAccount
107
     *
108
     * @return PaymentSetting
109
     */
110
    public function withInternationalBankAccount($internationalBankAccount)
111
    {
112
        $new = clone $this;
113
        $new->internationalBankAccount = $internationalBankAccount;
114
115
        return $new;
116
    }
117
118
    /**
119
     * Create an API response object from the HTTP response from the API server.
120
     *
121
     * @param array $data
122
     *
123
     * @return self
124
     */
125 2
    public static function createFromArray(array $data)
126
    {
127 2
        $paymentSetting = new self();
128 2
        $paymentSetting->bankgiro = $data['bankgiro'] ?? null;
129 2
        $paymentSetting->plusgiro = $data['plusgiro'] ?? null;
130 2
        $paymentSetting->domesticBankAccount = ['account_no' => $data['domestic_bank_account']['account_no'], 'clearing_no' => $data['domestic_bank_account']['clearing_no']] ?? null;
0 ignored issues
show
Documentation Bug introduced by Ibrahimhizeoui
It seems like array('account_no' => $d...'clearing_no']) ?? null can be null. However, the property $domesticBankAccount is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
131 2
        $paymentSetting->internationalBankAccount = ['bank' => $data['international_bank_account']['bank'], 'iban' => $data['international_bank_account']['iban'], 'bic' => $data['international_bank_account']['bic'], 'swift' => $data['international_bank_account']['swift']] ?? null;
0 ignored issues
show
Documentation Bug introduced by Ibrahimhizeoui
It seems like array('bank' => $data['i...unt']['swift']) ?? null can be null. However, the property $internationalBankAccount is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
132
133 2
        return $paymentSetting;
134
    }
135
136 1
    public function toArray()
137
    {
138 1
        $data = [];
139 1
        if ($this->bankgiro !== null) {
140 1
            $data['bankgiro'] = $this->bankgiro;
141
        }
142 1
        if ($this->plusgiro !== null) {
143 1
            $data['plusgiro'] = $this->plusgiro;
144
        }
145 1
        if ($this->domesticBankAccount['account_no'] !== null && $this->domesticBankAccount['clearing_no'] !== null) {
146 1
            $data['domestic_bank_account']['account_no'] = $this->domesticBankAccount['account_no'];
147 1
            $data['domestic_bank_account']['clearing_no'] = $this->domesticBankAccount['clearing_no'];
148
        }
149 1
        if ($this->internationalBankAccount['iban'] !== null) {
150 1
            $data['international_bank_account']['iban'] = $this->internationalBankAccount['iban'];
151
152 1 View Code Duplication
            if ($this->internationalBankAccount['iban'] !== null && $this->internationalBankAccount['bank'] !== null) {
0 ignored issues
show
Duplication introduced by Ibrahimhizeoui
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153 1
                $data['international_bank_account']['bank'] = $this->internationalBankAccount['bank'];
154
            }
155 1 View Code Duplication
            if ($this->internationalBankAccount['bic'] !== null) {
0 ignored issues
show
Duplication introduced by Ibrahimhizeoui
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156 1
                $data['international_bank_account']['bic'] = $this->internationalBankAccount['bic'];
157
            }
158 1 View Code Duplication
            if ($this->internationalBankAccount['swift'] !== null) {
0 ignored issues
show
Duplication introduced by Ibrahimhizeoui
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159 1
                $data['international_bank_account']['swift'] = $this->internationalBankAccount['swift'];
160
            }
161
        }
162
163 1
        return $data;
164
    }
165
}
166