Completed
Push — master ( 4733ab...0dcfa5 )
by Damian
07:49 queued 07:22
created

ChangePasswordForm   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
A getFormFields() 0 16 2
A getFormActions() 0 11 1
1
<?php
2
3
namespace SilverStripe\Security\MemberAuthenticator;
4
5
use SilverStripe\Control\RequestHandler;
6
use SilverStripe\Control\Session;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\Form;
9
use SilverStripe\Forms\FormAction;
10
use SilverStripe\Forms\FormField;
11
use SilverStripe\Forms\HiddenField;
12
use SilverStripe\Forms\PasswordField;
13
use SilverStripe\Security\Security;
14
15
/**
16
 * Standard Change Password Form
17
 */
18
class ChangePasswordForm extends Form
19
{
20
    /**
21
     * Constructor
22
     *
23
     * @param RequestHandler $controller The parent controller, necessary to create the appropriate form action tag.
24
     * @param string $name The method on the controller that will return this form object.
25
     * @param FieldList|FormField $fields All of the fields in the form - a {@link FieldList} of
26
     * {@link FormField} objects.
27
     * @param FieldList|FormAction $actions All of the action buttons in the form - a {@link FieldList} of
28
     */
29
    public function __construct($controller, $name, $fields = null, $actions = null)
30
    {
31
        $backURL = $controller->getBackURL() ?: Session::get('BackURL');
32
33
        if (!$fields) {
34
            $fields = $this->getFormFields();
35
        }
36
        if (!$actions) {
37
            $actions = $this->getFormActions();
38
        }
39
40
        if ($backURL) {
41
            $fields->push(HiddenField::create('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...
42
        }
43
44
        parent::__construct($controller, $name, $fields, $actions);
0 ignored issues
show
Bug introduced by
It seems like $fields defined by parameter $fields on line 29 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 29 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...
45
    }
46
47
    /**
48
     * @return FieldList
49
     */
50
    protected function getFormFields()
51
    {
52
        $fields = FieldList::create();
53
54
        // Security/changepassword?h=XXX redirects to Security/changepassword
55
        // without GET parameter to avoid potential HTTP referer leakage.
56
        // In this case, a user is not logged in, and no 'old password' should be necessary.
57
        if (Security::getCurrentUser()) {
58
            $fields->push(PasswordField::create('OldPassword', _t('SilverStripe\\Security\\Member.YOUROLDPASSWORD', 'Your old password')));
59
        }
60
61
        $fields->push(PasswordField::create('NewPassword1', _t('SilverStripe\\Security\\Member.NEWPASSWORD', 'New Password')));
62
        $fields->push(PasswordField::create('NewPassword2', _t('SilverStripe\\Security\\Member.CONFIRMNEWPASSWORD', 'Confirm New Password')));
63
64
        return $fields;
65
    }
66
67
    /**
68
     * @return FieldList
69
     */
70
    protected function getFormActions()
71
    {
72
        $actions = FieldList::create(
73
            FormAction::create(
74
                'doChangePassword',
75
                _t('SilverStripe\\Security\\Member.BUTTONCHANGEPASSWORD', 'Change Password')
76
            )
77
        );
78
79
        return $actions;
80
    }
81
}
82