Conditions | 10 |
Paths | 10 |
Total Lines | 83 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
62 | public function changepassword() |
||
63 | { |
||
64 | $request = $this->getRequest(); |
||
65 | |||
66 | // Extract the member from the URL. |
||
67 | /** @var Member $member */ |
||
68 | $member = null; |
||
69 | if ($request->getVar('m') !== null) { |
||
70 | $member = Member::get()->filter(['ID' => (int)$request->getVar('m')])->first(); |
||
71 | } |
||
72 | $token = $request->getVar('t'); |
||
73 | |||
74 | // Check whether we are merely changin password, or resetting. |
||
75 | if ($token !== null && $member && $member->validateAutoLoginToken($token)) { |
||
76 | $this->setSessionToken($member, $token); |
||
|
|||
77 | |||
78 | // Redirect to myself, but without the hash in the URL |
||
79 | return $this->redirect($this->link); |
||
80 | } |
||
81 | |||
82 | if (Session::get('AutoLoginHash')) { |
||
83 | $message = DBField::create_field( |
||
84 | 'HTMLFragment', |
||
85 | '<p>' . _t( |
||
86 | 'SilverStripe\\Security\\Security.ENTERNEWPASSWORD', |
||
87 | 'Please enter a new password.' |
||
88 | ) . '</p>' |
||
89 | ); |
||
90 | |||
91 | // Subsequent request after the "first load with hash" (see previous if clause). |
||
92 | return [ |
||
93 | 'Content' => $message, |
||
94 | 'Form' => $this->changePasswordForm() |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | if (Security::getCurrentUser()) { |
||
99 | // Logged in user requested a password change form. |
||
100 | $message = DBField::create_field( |
||
101 | 'HTMLFragment', |
||
102 | '<p>' . _t( |
||
103 | 'SilverStripe\\Security\\Security.CHANGEPASSWORDBELOW', |
||
104 | 'You can change your password below.' |
||
105 | ) . '</p>' |
||
106 | ); |
||
107 | |||
108 | return [ |
||
109 | 'Content' => $message, |
||
110 | 'Form' => $this->changePasswordForm() |
||
111 | ]; |
||
112 | } |
||
113 | // Show a friendly message saying the login token has expired |
||
114 | if ($token !== null && $member && !$member->validateAutoLoginToken($token)) { |
||
115 | $message = [ |
||
116 | 'Content' => DBField::create_field( |
||
117 | 'HTMLFragment', |
||
118 | _t( |
||
119 | 'SilverStripe\\Security\\Security.NOTERESETLINKINVALID', |
||
120 | '<p>The password reset link is invalid or expired.</p>' |
||
121 | . '<p>You can request a new one <a href="{link1}">here</a> or change your password after' |
||
122 | . ' you <a href="{link2}">logged in</a>.</p>', |
||
123 | [ |
||
124 | 'link1' => $this->link('lostpassword'), |
||
125 | 'link2' => $this->link('login') |
||
126 | ] |
||
127 | ) |
||
128 | ) |
||
129 | ]; |
||
130 | |||
131 | return [ |
||
132 | 'Content' => $message, |
||
133 | ]; |
||
134 | } |
||
135 | |||
136 | // Someone attempted to go to changepassword without token or being logged in |
||
137 | return Security::permissionFailure( |
||
138 | Controller::curr(), |
||
139 | _t( |
||
140 | 'SilverStripe\\Security\\Security.ERRORPASSWORDPERMISSION', |
||
141 | 'You must be logged in in order to change your password!' |
||
142 | ) |
||
143 | ); |
||
144 | } |
||
145 | |||
309 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.