Completed
Push — master ( 644ae6...bba86b )
by Daniel
10:38
created

ChangePasswordForm::doChangePassword()   D

Complexity

Conditions 13
Paths 47

Size

Total Lines 85
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 46
nc 47
nop 1
dl 0
loc 85
rs 4.9922
c 0
b 0
f 0

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
namespace SilverStripe\Security;
4
5
use SilverStripe\Control\Session;
6
use SilverStripe\Control\RequestHandler;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\FormField;
9
use SilverStripe\Forms\PasswordField;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\Form;
13
14
/**
15
 * Standard Change Password Form
16
 */
17
class ChangePasswordForm extends Form
18
{
19
    /**
20
     * Constructor
21
     *
22
     * @param RequestHandler $controller The parent controller, necessary to create the appropriate form action tag.
23
     * @param string $name The method on the controller that will return this form object.
24
     * @param FieldList|FormField $fields All of the fields in the form - a {@link FieldList} of
25
     * {@link FormField} objects.
26
     * @param FieldList|FormAction $actions All of the action buttons in the form - a {@link FieldList} of
27
     */
28
    public function __construct($controller, $name, $fields = null, $actions = null)
29
    {
30
        $backURL = $controller->getBackURL() ?: Session::get('BackURL');
31
32
        if (!$fields) {
33
            $fields = new FieldList();
34
35
            // Security/changepassword?h=XXX redirects to Security/changepassword
36
            // without GET parameter to avoid potential HTTP referer leakage.
37
            // In this case, a user is not logged in, and no 'old password' should be necessary.
38
            if (Member::currentUser()) {
39
                $fields->push(new PasswordField("OldPassword", _t('Member.YOUROLDPASSWORD', "Your old password")));
40
            }
41
42
            $fields->push(new PasswordField("NewPassword1", _t('Member.NEWPASSWORD', "New Password")));
43
            $fields->push(new PasswordField("NewPassword2", _t('Member.CONFIRMNEWPASSWORD', "Confirm New Password")));
44
        }
45
        if (!$actions) {
46
            $actions = new FieldList(
47
                new FormAction("doChangePassword", _t('Member.BUTTONCHANGEPASSWORD', "Change Password"))
48
            );
49
        }
50
51
        if ($backURL) {
52
            $fields->push(new HiddenField('BackURL', false, $backURL));
0 ignored issues
show
Bug introduced by
The method push does only exist in SilverStripe\Forms\FieldList, but not in SilverStripe\Forms\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...
53
        }
54
55
        parent::__construct($controller, $name, $fields, $actions);
0 ignored issues
show
Bug introduced by
It seems like $fields defined by parameter $fields on line 28 can also be of type object<SilverStripe\Forms\FormField>; however, SilverStripe\Forms\Form::__construct() does only seem to accept null|object<SilverStripe\Forms\FieldList>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $actions defined by parameter $actions on line 28 can also be of type object<SilverStripe\Forms\FormAction>; however, SilverStripe\Forms\Form::__construct() does only seem to accept null|object<SilverStripe\Forms\FieldList>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
    }
57
58
    /**
59
     * @return ChangePasswordHandler
60
     */
61
    protected function buildRequestHandler()
62
    {
63
        return ChangePasswordHandler::create($this);
64
    }
65
}
66