UpdateParser::getCustomerPatchData()   F
last analyzed

Complexity

Conditions 13
Paths 4096

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 57
ccs 0
cts 29
cp 0
rs 2.45
c 0
b 0
f 0
cc 13
nc 4096
nop 1
crap 182

How to fix   Long Method    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 LauLamanApps\eCurring\Http\Resource;
6
7
use DateTime;
8
use LauLamanApps\eCurring\Resource\Customer;
9
use LauLamanApps\eCurring\Resource\Subscription;
10
11
final class UpdateParser implements UpdateParserInterface
12
{
13
    public function parse(Updatable $object): array
14
    {
15
        if ($object instanceof Customer) {
16
            return $this->getCustomerPatchData($object);
17
        }
18
19
        if ($object instanceof Subscription) {
20
            return $this->getSubscriptionPatchData($object);
21
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 19 is false. This is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
22
    }
23
24
    private function getCustomerPatchData(Customer $customer): array
25
    {
26
        $data = [
27
            'first_name' => $customer->getFirstName(),
28
            'last_name' => $customer->getLastName(),
29
            'email' => $customer->getEmail(),
30
        ];
31
32
        if ($customer->getGender()) {
33
            $data['gender'] = $customer->getGender();
34
        }
35
36
        if ($customer->getMiddleName()) {
37
            $data['middle_name'] = $customer->getMiddleName();
38
        }
39
40
        if ($customer->getCompanyName()) {
41
            $data['company_name'] = $customer->getCompanyName();
42
        }
43
44
        if ($customer->getVatNumber()) {
45
            $data['vat_number'] = $customer->getVatNumber();
46
        }
47
48
        if ($customer->getPostalcode()) {
49
            $data['postalcode'] = $customer->getPostalcode();
50
        }
51
52
        if ($customer->getHouseNumber()) {
53
            $data['house_number'] = $customer->getHouseNumber();
54
        }
55
56
        if ($customer->getHouseNumberAdd()) {
57
            $data['house_number_add'] = $customer->getHouseNumberAdd();
58
        }
59
60
        if ($customer->getStreet()) {
61
            $data['street'] = $customer->getStreet();
62
        }
63
64
        if ($customer->getCity()) {
65
            $data['city'] = $customer->getCity();
66
        }
67
68
        if ($customer->getCountryCode()) {
69
            $data['country_iso2'] = $customer->getCountryCode();
70
        }
71
72
        if ($customer->getLanguage()) {
73
            $data['language'] = $customer->getLanguage();
74
        }
75
76
        if ($customer->getTelephone()) {
77
            $data['telephone '] = $customer->getTelephone();
78
        }
79
80
        return $data;
81
    }
82
83
    private function getSubscriptionPatchData(Subscription $subscription): array
84
    {
85
        $data = [];
86
87
        if ($subscription->getMandate()) {
88
            $data['mandate_accepted'] = $subscription->getMandate()->isAccepted();
89
            $data['mandate_accepted_date'] = $subscription->getMandate()->getAcceptedDate()->format(DateTime::ATOM);
90
        }
91
92
        if ($subscription->getStartDate()) {
93
            $data['start_date '] = $subscription->getStartDate()->format(DateTime::ATOM);
94
        }
95
96
        if ($subscription->getStatus()) {
97
            $data['status '] = $subscription->getStatus()->getValue();
98
        }
99
100
        if ($subscription->getCancelDate()) {
101
            $data['cancel_date '] = $subscription->getCancelDate()->format(DateTime::ATOM);
102
        }
103
104
        if ($subscription->getResumeDate()) {
105
            $data['resume_date '] = $subscription->getResumeDate()->format(DateTime::ATOM);
106
        }
107
108
        if ($subscription->isConfirmationSent() !== null) {
0 ignored issues
show
introduced by
The condition $subscription->isConfirmationSent() !== null is always true.
Loading history...
109
            $data['confirmation_sent '] = $subscription->isConfirmationSent();
110
        }
111
112
        if ($subscription->getSubscriptionWebhookUrl()) {
113
            $data['subscription_webhook_url '] = $subscription->getSubscriptionWebhookUrl();
114
        }
115
116
        if ($subscription->getTransactionWebhookUrl()) {
117
            $data['transaction_webhook_url '] = $subscription->getTransactionWebhookUrl();
118
        }
119
120
        if ($subscription->getSuccessRedirectUrl()) {
121
            $data['success_redirect_url '] = $subscription->getSuccessRedirectUrl();
122
        }
123
124
        return $data;
125
    }
126
}
127