| Conditions | 6 |
| Paths | 6 |
| Total Lines | 113 |
| Code Lines | 79 |
| 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 |
||
| 86 | protected function getFormFields() { |
||
| 87 | $fields = []; |
||
| 88 | |||
| 89 | if ( $this->par !== null ) { |
||
| 90 | $this->botPassword = BotPassword::newFromCentralId( $this->userId, $this->par ); |
||
| 91 | if ( !$this->botPassword ) { |
||
| 92 | $this->botPassword = BotPassword::newUnsaved( [ |
||
| 93 | 'centralId' => $this->userId, |
||
| 94 | 'appId' => $this->par, |
||
| 95 | ] ); |
||
| 96 | } |
||
| 97 | |||
| 98 | $sep = BotPassword::getSeparator(); |
||
| 99 | $fields[] = [ |
||
| 100 | 'type' => 'info', |
||
| 101 | 'label-message' => 'username', |
||
| 102 | 'default' => $this->getUser()->getName() . $sep . $this->par |
||
| 103 | ]; |
||
| 104 | |||
| 105 | if ( $this->botPassword->isSaved() ) { |
||
| 106 | $fields['resetPassword'] = [ |
||
| 107 | 'type' => 'check', |
||
| 108 | 'label-message' => 'botpasswords-label-resetpassword', |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | $lang = $this->getLanguage(); |
||
| 113 | $showGrants = MWGrants::getValidGrants(); |
||
| 114 | $fields['grants'] = [ |
||
| 115 | 'type' => 'checkmatrix', |
||
| 116 | 'label-message' => 'botpasswords-label-grants', |
||
| 117 | 'help-message' => 'botpasswords-help-grants', |
||
| 118 | 'columns' => [ |
||
| 119 | $this->msg( 'botpasswords-label-grants-column' )->escaped() => 'grant' |
||
| 120 | ], |
||
| 121 | 'rows' => array_combine( |
||
| 122 | array_map( 'MWGrants::getGrantsLink', $showGrants ), |
||
| 123 | $showGrants |
||
| 124 | ), |
||
| 125 | 'default' => array_map( |
||
| 126 | function( $g ) { |
||
| 127 | return "grant-$g"; |
||
| 128 | }, |
||
| 129 | $this->botPassword->getGrants() |
||
| 130 | ), |
||
| 131 | 'tooltips' => array_combine( |
||
| 132 | array_map( 'MWGrants::getGrantsLink', $showGrants ), |
||
| 133 | array_map( |
||
| 134 | function( $rights ) use ( $lang ) { |
||
| 135 | return $lang->semicolonList( array_map( 'User::getRightDescription', $rights ) ); |
||
| 136 | }, |
||
| 137 | array_intersect_key( MWGrants::getRightsByGrant(), array_flip( $showGrants ) ) |
||
| 138 | ) |
||
| 139 | ), |
||
| 140 | 'force-options-on' => array_map( |
||
| 141 | function( $g ) { |
||
| 142 | return "grant-$g"; |
||
| 143 | }, |
||
| 144 | MWGrants::getHiddenGrants() |
||
| 145 | ), |
||
| 146 | ]; |
||
| 147 | |||
| 148 | $fields['restrictions'] = [ |
||
| 149 | 'class' => 'HTMLRestrictionsField', |
||
| 150 | 'required' => true, |
||
| 151 | 'default' => $this->botPassword->getRestrictions(), |
||
| 152 | ]; |
||
| 153 | |||
| 154 | } else { |
||
| 155 | $linkRenderer = $this->getLinkRenderer(); |
||
| 156 | $dbr = BotPassword::getDB( DB_REPLICA ); |
||
| 157 | $res = $dbr->select( |
||
| 158 | 'bot_passwords', |
||
| 159 | [ 'bp_app_id' ], |
||
| 160 | [ 'bp_user' => $this->userId ], |
||
| 161 | __METHOD__ |
||
| 162 | ); |
||
| 163 | foreach ( $res as $row ) { |
||
|
|
|||
| 164 | $fields[] = [ |
||
| 165 | 'section' => 'existing', |
||
| 166 | 'type' => 'info', |
||
| 167 | 'raw' => true, |
||
| 168 | 'default' => $linkRenderer->makeKnownLink( |
||
| 169 | $this->getPageTitle( $row->bp_app_id ), |
||
| 170 | $row->bp_app_id |
||
| 171 | ), |
||
| 172 | ]; |
||
| 173 | } |
||
| 174 | |||
| 175 | $fields['appId'] = [ |
||
| 176 | 'section' => 'createnew', |
||
| 177 | 'type' => 'textwithbutton', |
||
| 178 | 'label-message' => 'botpasswords-label-appid', |
||
| 179 | 'buttondefault' => $this->msg( 'botpasswords-label-create' )->text(), |
||
| 180 | 'buttonflags' => [ 'progressive', 'primary' ], |
||
| 181 | 'required' => true, |
||
| 182 | 'size' => BotPassword::APPID_MAXLENGTH, |
||
| 183 | 'maxlength' => BotPassword::APPID_MAXLENGTH, |
||
| 184 | 'validation-callback' => function ( $v ) { |
||
| 185 | $v = trim( $v ); |
||
| 186 | return $v !== '' && strlen( $v ) <= BotPassword::APPID_MAXLENGTH; |
||
| 187 | }, |
||
| 188 | ]; |
||
| 189 | |||
| 190 | $fields[] = [ |
||
| 191 | 'type' => 'hidden', |
||
| 192 | 'default' => 'new', |
||
| 193 | 'name' => 'op', |
||
| 194 | ]; |
||
| 195 | } |
||
| 196 | |||
| 197 | return $fields; |
||
| 198 | } |
||
| 199 | |||
| 344 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.