Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 13 | class SpecialChangeCredentials extends AuthManagerSpecialPage { |
||
| 14 | protected static $allowedActions = [ AuthManager::ACTION_CHANGE ]; |
||
| 15 | |||
| 16 | protected static $messagePrefix = 'changecredentials'; |
||
| 17 | |||
| 18 | /** Change action needs user data; remove action does not */ |
||
| 19 | protected static $loadUserData = true; |
||
| 20 | |||
| 21 | public function __construct( $name = 'ChangeCredentials' ) { |
||
| 24 | |||
| 25 | protected function getGroupName() { |
||
| 28 | |||
| 29 | public function isListed() { |
||
| 33 | |||
| 34 | public function doesWrites() { |
||
| 37 | |||
| 38 | protected function getDefaultAction( $subPage ) { |
||
| 41 | |||
| 42 | protected function getPreservedParams( $withToken = false ) { |
||
| 51 | |||
| 52 | public function onAuthChangeFormFields( |
||
| 53 | array $requests, array $fieldInfo, array &$formDescriptor, $action |
||
| 54 | ) { |
||
| 55 | // This method is never called for remove actions. |
||
| 56 | |||
| 57 | $extraFields = []; |
||
| 58 | Hooks::run( 'ChangePasswordForm', [ &$extraFields ], '1.27' ); |
||
| 59 | foreach ( $extraFields as $extra ) { |
||
| 60 | list( $name, $label, $type, $default ) = $extra; |
||
| 61 | $formDescriptor[$name] = [ |
||
| 62 | 'type' => $type, |
||
| 63 | 'name' => $name, |
||
| 64 | 'label-message' => $label, |
||
| 65 | 'default' => $default, |
||
| 66 | ]; |
||
| 67 | |||
| 68 | } |
||
| 69 | |||
| 70 | return parent::onAuthChangeFormFields( $requests, $fieldInfo, $formDescriptor, $action ); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function execute( $subPage ) { |
||
| 74 | $this->setHeaders(); |
||
| 75 | $this->outputHeader(); |
||
| 76 | |||
| 77 | $this->loadAuth( $subPage ); |
||
| 78 | |||
| 79 | if ( !$subPage ) { |
||
| 80 | $this->showSubpageList(); |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( !$this->authRequests ) { |
||
| 85 | // messages used: changecredentials-invalidsubpage, removecredentials-invalidsubpage |
||
| 86 | $this->showSubpageList( $this->msg( static::$messagePrefix . '-invalidsubpage', $subPage ) ); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $status = $this->trySubmit(); |
||
| 91 | |||
| 92 | if ( $status === false || !$status->isOK() ) { |
||
| 93 | $this->displayForm( $status ); |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $response = $status->getValue(); |
||
| 98 | |||
| 99 | switch ( $response->status ) { |
||
| 100 | case AuthenticationResponse::PASS: |
||
| 101 | $this->success(); |
||
| 102 | break; |
||
| 103 | case AuthenticationResponse::FAIL: |
||
| 104 | $this->displayForm( Status::newFatal( $response->message ) ); |
||
| 105 | break; |
||
| 106 | default: |
||
| 107 | throw new LogicException( 'invalid AuthenticationResponse' ); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function loadAuth( $subPage, $authAction = null, $reset = false ) { |
||
| 122 | |||
| 123 | protected function getAuthFormDescriptor( $requests, $action ) { |
||
| 130 | |||
| 131 | protected function getAuthForm( array $requests, $action ) { |
||
| 132 | $form = parent::getAuthForm( $requests, $action ); |
||
| 133 | $req = reset( $requests ); |
||
| 134 | $info = $req->describeCredentials(); |
||
| 135 | |||
| 136 | $form->addPreText( |
||
| 137 | Html::openElement( 'dl' ) |
||
| 138 | . Html::element( 'dt', [], wfMessage( 'credentialsform-provider' ) ) |
||
| 139 | . Html::element( 'dd', [], $info['provider'] ) |
||
| 140 | . Html::element( 'dt', [], wfMessage( 'credentialsform-account' ) ) |
||
| 141 | . Html::element( 'dd', [], $info['account'] ) |
||
| 142 | . Html::closeElement( 'dl' ) |
||
| 143 | ); |
||
| 144 | |||
| 145 | // messages used: changecredentials-submit removecredentials-submit |
||
| 146 | $form->setSubmitTextMsg( static::$messagePrefix . '-submit' ); |
||
| 147 | $form->showCancel()->setCancelTarget( $this->getReturnUrl() ?: Title::newMainPage() ); |
||
|
|
|||
| 148 | |||
| 149 | return $form; |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function needsSubmitButton( array $requests ) { |
||
| 153 | // Change/remove forms show are built from a single AuthenticationRequest and do not allow |
||
| 154 | // for redirect flow; they always need a submit button. |
||
| 155 | return true; |
||
| 156 | } |
||
| 157 | |||
| 158 | View Code Duplication | public function handleFormSubmit( $data ) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param Message|null $error |
||
| 173 | */ |
||
| 174 | protected function showSubpageList( $error = null ) { |
||
| 175 | $out = $this->getOutput(); |
||
| 176 | |||
| 177 | if ( $error ) { |
||
| 178 | $out->addHTML( $error->parse() ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $groupedRequests = []; |
||
| 182 | foreach ( $this->authRequests as $req ) { |
||
| 183 | $info = $req->describeCredentials(); |
||
| 184 | $groupedRequests[(string)$info['provider']][] = $req; |
||
| 185 | } |
||
| 186 | |||
| 187 | $linkRenderer = $this->getLinkRenderer(); |
||
| 188 | $out->addHTML( Html::openElement( 'dl' ) ); |
||
| 189 | foreach ( $groupedRequests as $group => $members ) { |
||
| 190 | $out->addHTML( Html::element( 'dt', [], $group ) ); |
||
| 191 | foreach ( $members as $req ) { |
||
| 192 | /** @var AuthenticationRequest $req */ |
||
| 193 | $info = $req->describeCredentials(); |
||
| 194 | $out->addHTML( Html::rawElement( 'dd', [], |
||
| 195 | $linkRenderer->makeLink( |
||
| 196 | $this->getPageTitle( $req->getUniqueId() ), |
||
| 197 | $info['account'] |
||
| 198 | ) |
||
| 199 | ) ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | $out->addHTML( Html::closeElement( 'dl' ) ); |
||
| 203 | } |
||
| 204 | |||
| 205 | protected function success() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | protected function getReturnUrl() { |
||
| 241 | |||
| 242 | protected function getRequestBlacklist() { |
||
| 245 | } |
||
| 246 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.