MemberLoginFormWithSignup::__construct()   C
last analyzed

Complexity

Conditions 9
Paths 56

Size

Total Lines 72
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 6.0413
c 0
b 0
f 0
cc 9
eloc 44
nc 56
nop 5

How to fix   Long Method   

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
 * Log-in form for the "member" authentication method
4
 * @package sapphire
5
 * @subpackage security
6
 */
7
class MemberLoginFormWithSignup extends LoginForm
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
10
    /**
11
     * This field is used in the "You are logged in as %s" message
12
     * @var string
13
     */
14
    public $loggedInAsField = 'FirstName';
15
16
    protected $authenticator_class = 'MemberAuthenticatorWithSignup';
17
18
    /**
19
     * Constructor
20
     *
21
     * @param Controller $controller The parent controller, necessary to
22
     *                               create the appropriate form action tag.
23
     * @param string $name The method on the controller that will return this
24
     *                     form object.
25
     * @param FieldSet|FormField $fields All of the fields in the form - a
0 ignored issues
show
Documentation introduced by
Should the type for parameter $fields not be FieldSet|FormField|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
26
     *                                   {@link FieldSet} of {@link FormField}
27
     *                                   objects.
28
     * @param FieldSet|FormAction $actions All of the action buttons in the
0 ignored issues
show
Documentation introduced by
Should the type for parameter $actions not be FieldSet|FormAction|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
29
     *                                     form - a {@link FieldSet} of
30
     *                                     {@link FormAction} objects
31
     * @param bool $checkCurrentUser If set to TRUE, it will be checked if a
32
     *                               the user is currently logged in, and if
33
     *                               so, only a logout button will be rendered
34
     * @param string $authenticatorClassName Name of the authenticator class that this form uses.
0 ignored issues
show
Bug introduced by
There is no parameter named $authenticatorClassName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     */
36
    public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
37
    {
38
39
        // This is now set on the class directly to make it easier to create subclasses
40
        // $this->authenticator_class = $authenticatorClassName;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
42
        if (isset($_REQUEST['BackURL'])) {
43
            $backURL = $_REQUEST['BackURL'];
44
        } else {
45
            $backURL = Session::get('BackURL');
46
        }
47
        $member = Member::currentUser();
48
        $label = singleton('Member')->fieldLabel(Member::get_unique_identifier_field());
49
50
        if ($checkCurrentUser && $member) {
51
            $fields = new FieldSet(
52
                new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this),
0 ignored issues
show
Documentation introduced by
'AuthenticationMethod' is of type string, but the function expects a object<The>.

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...
Documentation introduced by
$this->authenticator_class is of type string, but the function expects a object<The>|null.

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...
Documentation introduced by
$this is of type this<MemberLoginFormWithSignup>, but the function expects a object<reference>|null.

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...
53
                new TextField("FirstNameSignup", "Voornaam", $member->FirstName),
54
                new TextField("SurnameSignup", "Achternaam", $member->Surname),
55
                new TextField("EmailSignup", $label, $member->Email),
56
                new PasswordField("PasswordSignup", _t('Member.PASSWORD', 'Password'))
57
            );
58
            $actions = new FieldSet(
59
                new FormAction("createorupdateaccount", _t('Member.UPDATEDETAILS', "Update your details")),
0 ignored issues
show
Documentation introduced by
'createorupdateaccount' is of type string, but the function expects a object<The>.

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...
60
                new FormAction("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else"))
0 ignored issues
show
Documentation introduced by
'logout' is of type string, but the function expects a object<The>.

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...
61
            );
62
        } else {
63
            if (!$fields) {
64
                $fields = new FieldSet(
65
                    new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this),
0 ignored issues
show
Documentation introduced by
'AuthenticationMethod' is of type string, but the function expects a object<The>.

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...
Documentation introduced by
$this->authenticator_class is of type string, but the function expects a object<The>|null.

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...
Documentation introduced by
$this is of type this<MemberLoginFormWithSignup>, but the function expects a object<reference>|null.

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...
66
                    new TextField("FirstNameSignup", "Voornaam", Session::get('SessionForms.MemberLoginFormWithSignup.FirstNameSignup'), null, $this),
67
                    new TextField("SurnameSignup", "Achternaam", Session::get('SessionForms.MemberLoginFormWithSignup.SurnameSignup'), null, $this),
68
                    new TextField("EmailSignup", $label, Session::get('SessionForms.MemberLoginFormWithSignup.EmailSignup'), null, $this),
69
                    new PasswordField("PasswordSignup", _t('Member.PASSWORD', 'Password'))
70
                );
71
                if (Security::$autologin_enabled) {
72
                    $fields->push(
73
                        new CheckboxField(
74
                            "RememberSignup",
0 ignored issues
show
Documentation introduced by
'RememberSignup' is of type string, but the function expects a object<The>.

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...
75
                            _t('Member.REMEMBERME', "Remember me next time?")
0 ignored issues
show
Documentation introduced by
_t('Member.REMEMBERME', 'Remember me next time?') is of type string, but the function expects a object<The>|null.

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...
76
                        )
77
                    );
78
                }
79
            }
80
            if (!$actions) {
81
                $actions = new FieldSet(
82
                    new FormAction('createorupdateaccount', _t('Member.BUTTONCREATEACCOUNT', "Create account"))
0 ignored issues
show
Documentation introduced by
'createorupdateaccount' is of type string, but the function expects a object<The>.

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...
83
                );
84
            }
85
        }
86
87
        if (isset($backURL)) {
88
            $fields->push(new HiddenField('BackURL', 'BackURL', $backURL));
0 ignored issues
show
Documentation introduced by
'BackURL' is of type string, but the function expects a object<The>.

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...
Documentation introduced by
'BackURL' is of type string, but the function expects a object<The>|null.

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...
Bug introduced by
The method push does only exist in FieldSet, but not in FormField.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
89
        }
90
        $requiredFields =
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $requiredFields is correct as parent::__construct($con...ame, $fields, $actions) (which targets LoginForm::__construct()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$requiredFields is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        parent::__construct($controller, $name, $fields, $actions);
92
        $validator = new RequiredFields(array("EmailSignup", "FirstNameSignup", "SurnameSignup", "PasswordSignup"));
93
        $validator->setForm($this);
94
        $this->validator = $validator;
95
96
        // Focus on the email input when the page is loaded
97
        // Only include this if other form JS validation is enabled
98
        if ($this->getValidator()->getJavascriptValidationHandler() != 'none') {
99
            Requirements::customScript(<<<JS
0 ignored issues
show
Documentation introduced by
' (function() { ... el.focus(); })();' is of type string, but the function expects a object<The>.

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...
100
				(function() {
101
					var el = document.getElementById("MemberLoginForm_LoginForm_EmailSignup");
102
					if(el && el.focus) el.focus();
103
				})();
104
JS
105
            );
106
        }
107
    }
108
109
    /**
110
     * Get message from session
111
     */
112
    protected function getMessageFromSession()
113
    {
114
        parent::getMessageFromSession();
115
        Session::set('MemberLoginFormWithSignup.force_message', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, 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...
116
    }
117
118
119
    /**
120
     * Login form handler method
121
     *
122
     * This method is called when the user clicks on "Log in"
123
     *
124
     * @param array $data Submitted data
125
     */
126
    public function createorupdateaccount($data, $form)
0 ignored issues
show
Coding Style introduced by
createorupdateaccount uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
127
    {
128
        $passwordOK = true;
129
        if (!$passwordOK) {
130
            Session::set('Security.Message.message',
131
                _t('Member.PASSWORDINVALID', "Your password is not valid.")
132
            );
133
            $loginLink = Director::absoluteURL(Security::Link("login"));
134
            if ($backURL) {
0 ignored issues
show
Bug introduced by
The variable $backURL seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
135
                $loginLink .= '?BackURL=' . urlencode($backURL);
0 ignored issues
show
Bug introduced by
The variable $backURL seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
136
            }
137
            Director::redirect($loginLink . '#' . $this->FormName() .'_tab');
138
        }
139
        if ($this->createOrUpdateUser($data, $form)) {
140
            Session::clear('SessionForms.MemberLoginForm.EmailSignup');
141
            Session::clear('SessionForms.MemberLoginForm.FirstNameSignup');
142
            Session::clear('SessionForms.MemberLoginForm.SurnameSignup');
143
            Session::clear('SessionForms.MemberLoginForm.RememberSignup');
144
            if (!isset($_REQUEST['BackURL'])) {
145
                if (Session::get("BackURL")) {
146
                    $_REQUEST['BackURL'] = Session::get("BackURL");
147
                }
148
            }
149
            Session::clear("BackURL");
150
            if (isset($_REQUEST['BackURL']) && $_REQUEST['BackURL'] && Director::is_site_url($_REQUEST['BackURL'])) {
151
                Director::redirect($_REQUEST['BackURL']);
152
            } elseif (Security::default_login_dest()) {
153
                Director::redirect(Director::absoluteBaseURL() . Security::default_login_dest());
154
            } else {
155
                $member = Member::currentUser();
156
                if ($member) {
157
                    $firstname = Convert::raw2xml($member->FirstName);
158
                    if (!empty($data['RememberSignup'])) {
159
                        Session::set('SessionForms.MemberLoginForm.RememberSignup', '1');
160
                        $member->logIn(true);
161
                    } else {
162
                        $member->logIn();
163
                    }
164
                    Session::set('Security.Message.message',
165
                        sprintf(_t('Member.THANKYOUFORCREATINGACCOUNT', "Thank you for creating an account, %s"), $firstname)
166
                    );
167
                    Session::set("Security.Message.type", "good");
168
                }
169
                Director::redirectBack();
0 ignored issues
show
Deprecated Code introduced by
The method Director::redirectBack() has been deprecated with message: 2.5 Use Controller->redirectBack()

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...
170
            }
171
        } else {
172
            Session::set('Security.Message.message',
173
                _t('Member.MEMBERALREADYEXISTS', "A member with this email already exists.")
174
            );
175
            Session::set("Security.Message.type", "error");
176
            Session::set('SessionForms.MemberLoginFormWithSignup.EmailSignupSignup', $data['EmailSignup']);
177
            Session::set('SessionForms.MemberLoginFormWithSignup.FirstNameSignup', $data['FirstNameSignup']);
178
            Session::set('SessionForms.MemberLoginFormWithSignup.SurnameSignup', $data['SurnameSignup']);
179
            Session::set('SessionForms.MemberLoginFormWithSignup.RememberSignup', isset($data['RememberSignup']));
0 ignored issues
show
Documentation introduced by
isset($data['RememberSignup']) is of type boolean, 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...
180
            if (isset($_REQUEST['BackURL'])) {
181
                $backURL = $_REQUEST['BackURL'];
182
            } else {
183
                $backURL = null;
184
            }
185
            if ($backURL) {
186
                Session::set('BackURL', $backURL);
187
            }
188
            if ($badLoginURL = Session::get("BadLoginURL")) {
189
                Director::redirect($badLoginURL);
190
            } else {
191
                // Show the right tab on failed login
192
                $loginLink = Director::absoluteURL(Security::Link("login"));
193
                if ($backURL) {
194
                    $loginLink .= '?BackURL=' . urlencode($backURL);
195
                }
196
                Director::redirect($loginLink . '#' . $this->FormName() .'_tab');
197
            }
198
        }
199
    }
200
201
202
    /**
203
     * Log out form handler method
204
     *
205
     * This method is called when the user clicks on "logout" on the form
206
     * created when the parameter <i>$checkCurrentUser</i> of the
207
     * {@link __construct constructor} was set to TRUE and the user was
208
     * currently logged in.
209
     */
210
    public function logout()
211
    {
212
        $s = new Security();
213
        $s->logout();
214
    }
215
216
217
  /**
218
   * Try to authenticate the user
219
   *
220
   * @param array Submitted data
221
   * @return Member Returns the member object on successful authentication
0 ignored issues
show
Documentation introduced by
Should the return type not be Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
222
   *                or NULL on failure.
223
   */
224
    public function createOrUpdateUser($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
    {
226
        $currentUserID = intval(Member::currentUserID()) - 0;
227
        $existingMember = DataObject::get_one("Member", "\"".Member::get_unique_identifier_field()."\" = '".Convert::raw2sql($data[Member::get_unique_identifier_field()."Signup"])."' AND \"Member\".\"ID\" <> ".$currentUserID);
228
        $loginMemberAfterCreation = true;
229
        $loggedInUser = $member = Member::currentUser();
230
        if ($existingMember && $loggedInUser) {
231
            $this->extend('authenticationFailed', $data);
232
            return null;
233
        } elseif ($existingMember && !$loggedInUser) {
234
            $member = $existingMember;
235
        } elseif ($loggedInUser) {
236
            $loginMemberAfterCreation = false;
237
        } else {
238
            $member = new Member();
239
        }
240
        $member->FirstName = trim(Convert::raw2sql($data["FirstNameSignup"]));
241
        $member->Surname = trim(Convert::raw2sql($data["SurnameSignup"]));
242
        $member->Email = trim(Convert::raw2sql($data["EmailSignup"]));
243
        $member->Password = trim(Convert::raw2sql($data["PasswordSignup"]));
244
        $member->write();
245
        if ($loginMemberAfterCreation) {
246
            $member->LogIn(isset($data['RememberSignup']));
247
        }
248
        return $member;
249
    }
250
}
251
252
253
254
255
class MemberLoginFormWithSignup_Validator extends RequiredFields
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
256
{
257
258
    /**
259
     * Ensures member unique id stays unique and other basic stuff...
260
     * @param $data = array Form Field Data
261
     * @return Boolean
262
     **/
263
    public function php($data)
264
    {
265
        $valid = parent::php($data);
266
        $uniqueFieldNameForMember = Member::get_unique_identifier_field();
267
        $uniqueFieldNameForForm = $uniqueFieldNameForMember."Signup";
268
        $loggedInMember = Member::currentUser();
269
        if (isset($data[$uniqueFieldNameForForm]) && $loggedInMember && $data[$uniqueFieldNameForForm]) {
270
            if (!$loggedInMember->IsShopAdmin()) {
271
                $uniqueFieldValue = Convert::raw2sql($data[$uniqueFieldNameForForm]);
272
                $anotherMember = DataObject::get_one('Member', "\"$uniqueFieldNameForMember\" = '$uniqueFieldValue' AND \"Member\".\"ID\" <> ".$loggedInMember->ID);
273
                //can't be taken
274
                if ($anotherMember->Password) {
275
                    $message = sprintf(
276
                        _t("Account.ALREADYTAKEN",  '%1$s is already taken by another member. Please log in or use another %2$s'),
277
                        $uniqueFieldValue,
278
                        $uniqueFieldNameForForm
279
                    );
280
                    $this->validationError(
281
                        $uniqueFieldNameForForm,
282
                        $message,
283
                        "required"
284
                    );
285
                    $valid = false;
286
                }
287
            }
288
        }
289
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
290
        // check password fields are the same before saving
291
        if(isset($data["Password"]["_Password"]) && isset($data["Password"]["_ConfirmPassword"])) {
292
            if($data["Password"]["_Password"] != $data["Password"]["_ConfirmPassword"]) {
293
                $this->validationError(
294
                    "Password",
295
                    _t('Account.PASSWORDSERROR', 'Passwords do not match.'),
296
                    "required"
297
                );
298
                $valid = false;
299
            }
300
            if(!$loggedInMember && !$data["Password"]["_Password"]) {
301
                $this->validationError(
302
                    "Password",
303
                    _t('Account.SELECTPASSWORD', 'Please select a password.'),
304
                    "required"
305
                );
306
                $valid = false;
307
            }
308
        }
309
        * */
310
        if (!$valid) {
311
            $this->form->sessionMessage(_t('Account.ERRORINFORM', 'We could not save your details, please check your errors below.'), "bad");
0 ignored issues
show
Documentation introduced by
_t('Account.ERRORINFORM'...ck your errors below.') is of type string, but the function expects a object<The>.

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...
Documentation introduced by
'bad' is of type string, but the function expects a object<Should>.

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...
312
        }
313
        return $valid;
314
    }
315
}
316