| Conditions | 27 |
| Paths | 14400 |
| Total Lines | 94 |
| Code Lines | 64 |
| 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 |
||
| 37 | public function execute() { |
||
| 38 | if ( $this->getUser()->isAnon() ) { |
||
| 39 | $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' ); |
||
| 40 | } elseif ( !$this->getUser()->isAllowed( 'editmyoptions' ) ) { |
||
| 41 | $this->dieUsage( "You don't have permission to edit your options", 'permissiondenied' ); |
||
| 42 | } |
||
| 43 | |||
| 44 | $params = $this->extractRequestParams(); |
||
| 45 | $changed = false; |
||
| 46 | |||
| 47 | if ( isset( $params['optionvalue'] ) && !isset( $params['optionname'] ) ) { |
||
| 48 | $this->dieUsageMsg( [ 'missingparam', 'optionname' ] ); |
||
| 49 | } |
||
| 50 | |||
| 51 | // Load the user from the master to reduce CAS errors on double post (T95839) |
||
| 52 | $user = $this->getUser()->getInstanceForUpdate(); |
||
| 53 | if ( !$user ) { |
||
| 54 | $this->dieUsage( 'Anonymous users cannot change preferences', 'notloggedin' ); |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( $params['reset'] ) { |
||
| 58 | $user->resetOptions( $params['resetkinds'], $this->getContext() ); |
||
| 59 | $changed = true; |
||
| 60 | } |
||
| 61 | |||
| 62 | $changes = []; |
||
| 63 | if ( count( $params['change'] ) ) { |
||
| 64 | foreach ( $params['change'] as $entry ) { |
||
| 65 | $array = explode( '=', $entry, 2 ); |
||
| 66 | $changes[$array[0]] = isset( $array[1] ) ? $array[1] : null; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | if ( isset( $params['optionname'] ) ) { |
||
| 70 | $newValue = isset( $params['optionvalue'] ) ? $params['optionvalue'] : null; |
||
| 71 | $changes[$params['optionname']] = $newValue; |
||
| 72 | } |
||
| 73 | if ( !$changed && !count( $changes ) ) { |
||
| 74 | $this->dieUsage( 'No changes were requested', 'nochanges' ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $prefs = Preferences::getPreferences( $user, $this->getContext() ); |
||
|
|
|||
| 78 | $prefsKinds = $user->getOptionKinds( $this->getContext(), $changes ); |
||
| 79 | |||
| 80 | $htmlForm = null; |
||
| 81 | foreach ( $changes as $key => $value ) { |
||
| 82 | switch ( $prefsKinds[$key] ) { |
||
| 83 | case 'registered': |
||
| 84 | // Regular option. |
||
| 85 | if ( $htmlForm === null ) { |
||
| 86 | // We need a dummy HTMLForm for the validate callback... |
||
| 87 | $htmlForm = new HTMLForm( [], $this ); |
||
| 88 | } |
||
| 89 | $field = HTMLForm::loadInputFromParameters( $key, $prefs[$key], $htmlForm ); |
||
| 90 | $validation = $field->validate( $value, $user->getOptions() ); |
||
| 91 | break; |
||
| 92 | case 'registered-multiselect': |
||
| 93 | case 'registered-checkmatrix': |
||
| 94 | // A key for a multiselect or checkmatrix option. |
||
| 95 | $validation = true; |
||
| 96 | $value = $value !== null ? (bool)$value : null; |
||
| 97 | break; |
||
| 98 | case 'userjs': |
||
| 99 | // Allow non-default preferences prefixed with 'userjs-', to be set by user scripts |
||
| 100 | if ( strlen( $key ) > 255 ) { |
||
| 101 | $validation = 'key too long (no more than 255 bytes allowed)'; |
||
| 102 | } elseif ( preg_match( '/[^a-zA-Z0-9_-]/', $key ) !== 0 ) { |
||
| 103 | $validation = 'invalid key (only a-z, A-Z, 0-9, _, - allowed)'; |
||
| 104 | } else { |
||
| 105 | $validation = true; |
||
| 106 | } |
||
| 107 | break; |
||
| 108 | case 'special': |
||
| 109 | $validation = 'cannot be set by this module'; |
||
| 110 | break; |
||
| 111 | case 'unused': |
||
| 112 | default: |
||
| 113 | $validation = 'not a valid preference'; |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | if ( $validation === true ) { |
||
| 117 | $user->setOption( $key, $value ); |
||
| 118 | $changed = true; |
||
| 119 | } else { |
||
| 120 | $this->setWarning( "Validation error for '$key': $validation" ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( $changed ) { |
||
| 125 | // Commit changes |
||
| 126 | $user->saveSettings(); |
||
| 127 | } |
||
| 128 | |||
| 129 | $this->getResult()->addValue( null, $this->getModuleName(), 'success' ); |
||
| 130 | } |
||
| 131 | |||
| 183 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: