ShopAccountForm_Validator::php()   F
last analyzed

Complexity

Conditions 22
Paths 2106

Size

Total Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
dl 0
loc 102
rs 0
c 0
b 0
f 0
nc 2106
nop 2

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
4
class ShopAccountForm_Validator extends RequiredFields
5
{
6
7
    /**
8
     * Ensures member unique id stays unique and other basic stuff...
9
     *
10
     * @param array $data               = array Form Field Data
11
     * @param bool  $allowExistingEmail - see comment below
12
     *
13
     * @return bool
14
     **/
15
    public function php($data, $allowExistingEmail = false)
16
    {
17
        $this->form->saveDataToSession();
18
        $valid = parent::php($data);
19
        $uniqueFieldName = Member::get_unique_identifier_field();
0 ignored issues
show
Deprecated Code introduced by
The method Member::get_unique_identifier_field() has been deprecated with message: 4.0 Use the "Member.unique_identifier_field" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
20
        $loggedInMember = Member::currentUser();
21
        $loggedInMemberID = 0;
22
        if (isset($data[$uniqueFieldName]) && $data[$uniqueFieldName]) {
23
            $isShopAdmin = false;
24
            if ($loggedInMember) {
25
                $loggedInMemberID = $loggedInMember->ID;
26
                if ($loggedInMember->IsShopAdmin()) {
27
                    $isShopAdmin = true;
28
                }
29
            }
30
            if ($isShopAdmin || $allowExistingEmail) {
31
                //do nothing
32
            } else {
33
                $uniqueFieldValue = Convert::raw2sql($data[$uniqueFieldName]);
34
                //can't be taken
35
                $otherMembersWithSameEmail = Member::get()
36
                    ->filter(array($uniqueFieldName => $uniqueFieldValue))
37
                    ->exclude(array('ID' => $loggedInMemberID));
38
                if ($otherMembersWithSameEmail->count()) {
39
                    //we allow existing email
40
                    // if we are currently NOT logged in
41
                    // in case we place an order!
42
                    if ($allowExistingEmail) {
43
                        //do nothing
44
                    } else {
45
                        $message = _t(
46
                            'Account.ALREADYTAKEN',
47
                            '{uniqueFieldValue} is already taken by another member. Please log in or use another {uniqueFieldName}.',
48
                            array('uniqueFieldValue' => $uniqueFieldValue, 'uniqueFieldName' => $uniqueFieldName)
0 ignored issues
show
Documentation introduced by
array('uniqueFieldValue'...e' => $uniqueFieldName) is of type array<string,array|strin...ueFieldName":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
                        );
50
                        $this->validationError(
51
                            $uniqueFieldName,
52
                            $message,
53
                            'required'
54
                        );
55
                        $valid = false;
56
                    }
57
                }
58
            }
59
        }
60
        // check password fields are the same before saving
61
        if (isset($data['PasswordCheck1']) && isset($data['PasswordCheck2'])) {
62
            if ($data['PasswordCheck1'] != $data['PasswordCheck2']) {
63
                $this->validationError(
64
                    'PasswordCheck1',
65
                    _t('Account.PASSWORDSERROR', 'Passwords do not match.'),
66
                    'required'
67
                );
68
                $valid = false;
69
            }
70
            //if you are not logged in, you have not provided a password and the settings require you to be logged in then
71
            //we have a problem
72
            if (!$loggedInMember && !$data['PasswordCheck1'] && EcommerceConfig::get('EcommerceRole', 'must_have_account_to_purchase')) {
73
                $this->validationError(
74
                    'PasswordCheck1',
75
                    _t('Account.SELECTPASSWORD', 'Please select a password.'),
76
                    'required'
77
                );
78
                $valid = false;
79
            }
80
            $letterCount = strlen($data['PasswordCheck1']);
81
            $minLength = Config::inst()->get('ShopAccountForm_Validator', 'minimum_password_length');
82
            if ($letterCount > 0 && $letterCount < $minLength) {
83
                $this->validationError(
84
                    'PasswordCheck1',
85
                    _t('Account.PASSWORDMINIMUMLENGTH', 'Password does not meet minimum standards.'),
86
                    'required'
87
                );
88
                $valid = false;
89
            }
90
        }
91
        if (isset($data['FirstName'])) {
92
            if (strlen($data['FirstName']) < 2) {
93
                $this->validationError(
94
                    'FirstName',
95
                    _t('Account.NOFIRSTNAME', 'Please enter your first name.'),
96
                    'required'
97
                );
98
                $valid = false;
99
            }
100
        }
101
        if (isset($data['Surname'])) {
102
            if (strlen($data['Surname']) < 2) {
103
                $this->validationError(
104
                    'Surname',
105
                    _t('Account.NOSURNAME', 'Please enter your surname.'),
106
                    'required'
107
                );
108
                $valid = false;
109
            }
110
        }
111
        if (!$valid) {
112
            $this->form->sessionMessage(_t('Account.ERRORINFORM', 'We could not save your details, please check your errors below.'), 'bad');
113
        }
114
115
        return $valid;
116
    }
117
}
118