| Conditions | 7 | 
| Paths | 6 | 
| Total Lines | 64 | 
| Code Lines | 30 | 
| 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 declare(strict_types=1); | ||
| 49 | public function reset(HTTPRequest $request): HTTPResponse | ||
| 50 |     { | ||
| 51 |         if (!$request->isPOST() || !$request->param('ID')) { | ||
| 52 | return $this->jsonResponse( | ||
| 53 | [ | ||
| 54 | 'error' => _t(__CLASS__ . '.BAD_REQUEST', 'Invalid request') | ||
| 55 | ], | ||
| 56 | 400 | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | $body = json_decode($request->getBody() ?? '', true); | ||
| 61 | |||
| 62 |         if (!SecurityToken::inst()->check($body['csrf_token'] ?? null)) { | ||
| 63 | return $this->jsonResponse( | ||
| 64 | [ | ||
| 65 | 'error' => _t(__CLASS__ . '.INVALID_CSRF_TOKEN', 'Invalid or missing CSRF token') | ||
| 66 | ], | ||
| 67 | 400 | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 |         if (!Permission::check(MemberExtension::MFA_ADMINISTER_REGISTERED_METHODS)) { | ||
| 72 | return $this->jsonResponse( | ||
| 73 | [ | ||
| 74 | 'error' => _t( | ||
| 75 | __CLASS__ . '.INSUFFICIENT_PERMISSIONS', | ||
| 76 | 'Insufficient permissions to reset user' | ||
| 77 | ) | ||
| 78 | ], | ||
| 79 | 403 | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | |||
| 83 | /** @var Member $memberToReset */ | ||
| 84 |         $memberToReset = Member::get()->byID($request->param('ID')); | ||
| 85 | |||
| 86 |         if ($memberToReset === null) { | ||
| 87 | return $this->jsonResponse( | ||
| 88 | [ | ||
| 89 | 'error' => _t( | ||
| 90 | __CLASS__ . '.INVALID_MEMBER', | ||
| 91 | 'Requested member for reset not found' | ||
| 92 | ) | ||
| 93 | ], | ||
| 94 | 403 | ||
| 95 | ); | ||
| 96 | } | ||
| 97 | |||
| 98 | $sent = $this->sendResetEmail($memberToReset); | ||
| 99 | |||
| 100 |         if (!$sent) { | ||
| 101 | return $this->jsonResponse( | ||
| 102 | [ | ||
| 103 | 'error' => _t( | ||
| 104 | __CLASS__ . '.EMAIL_NOT_SENT', | ||
| 105 | 'Email sending failed' | ||
| 106 | ) | ||
| 107 | ], | ||
| 108 | 500 | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | return $this->jsonResponse(['success' => true], 200); | ||
| 113 | } | ||
| 162 | 
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: