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:
Complex classes like SpecialEmailUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SpecialEmailUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class SpecialEmailUser extends UnlistedSpecialPage { |
||
| 30 | protected $mTarget; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var User|string $mTargetObj |
||
| 34 | */ |
||
| 35 | protected $mTargetObj; |
||
| 36 | |||
| 37 | public function __construct() { |
||
| 40 | |||
| 41 | public function doesWrites() { |
||
| 44 | |||
| 45 | public function getDescription() { |
||
| 53 | |||
| 54 | protected function getFormFields() { |
||
| 103 | |||
| 104 | public function execute( $par ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Validate target User |
||
| 186 | * |
||
| 187 | * @param string $target Target user name |
||
| 188 | * @return User User object on success or a string on error |
||
| 189 | */ |
||
| 190 | public static function getTarget( $target ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check whether a user is allowed to send email |
||
| 217 | * |
||
| 218 | * @param User $user |
||
| 219 | * @param string $editToken Edit token |
||
| 220 | * @param Config $config optional for backwards compatibility |
||
| 221 | * @return string|null Null on success or string on error |
||
| 222 | */ |
||
| 223 | public static function getPermissionsError( $user, $editToken, Config $config = null ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Form to ask for target user name. |
||
| 266 | * |
||
| 267 | * @param string $name User name submitted. |
||
| 268 | * @return string Form asking for user name. |
||
| 269 | */ |
||
| 270 | protected function userForm( $name ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Submit callback for an HTMLForm object, will simply call submit(). |
||
| 304 | * |
||
| 305 | * @since 1.20 |
||
| 306 | * @param array $data |
||
| 307 | * @param HTMLForm $form |
||
| 308 | * @return Status|string|bool |
||
| 309 | */ |
||
| 310 | public static function uiSubmit( array $data, HTMLForm $form ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Really send a mail. Permissions should have been checked using |
||
| 316 | * getPermissionsError(). It is probably also a good |
||
| 317 | * idea to check the edit token and ping limiter in advance. |
||
| 318 | * |
||
| 319 | * @param array $data |
||
| 320 | * @param IContextSource $context |
||
| 321 | * @return Status|string|bool Status object, or potentially a String on error |
||
| 322 | * or maybe even true on success if anything uses the EmailUser hook. |
||
| 323 | */ |
||
| 324 | public static function submit( array $data, IContextSource $context ) { |
||
| 325 | $config = $context->getConfig(); |
||
| 326 | |||
| 327 | $target = self::getTarget( $data['Target'] ); |
||
| 328 | if ( !$target instanceof User ) { |
||
| 329 | // Messages used here: notargettext, noemailtext, nowikiemailtext |
||
| 330 | return $context->msg( $target . 'text' )->parseAsBlock(); |
||
| 331 | } |
||
| 332 | |||
| 333 | $to = MailAddress::newFromUser( $target ); |
||
| 334 | $from = MailAddress::newFromUser( $context->getUser() ); |
||
| 335 | $subject = $data['Subject']; |
||
| 336 | $text = $data['Text']; |
||
| 337 | |||
| 338 | // Add a standard footer and trim up trailing newlines |
||
| 339 | $text = rtrim( $text ) . "\n\n-- \n"; |
||
| 340 | $text .= $context->msg( 'emailuserfooter', |
||
| 341 | $from->name, $to->name )->inContentLanguage()->text(); |
||
| 342 | |||
| 343 | $error = ''; |
||
| 344 | if ( !Hooks::run( 'EmailUser', [ &$to, &$from, &$subject, &$text, &$error ] ) ) { |
||
| 345 | return $error; |
||
| 346 | } |
||
| 347 | |||
| 348 | View Code Duplication | if ( $config->get( 'UserEmailUseReplyTo' ) ) { |
|
| 349 | /** |
||
| 350 | * Put the generic wiki autogenerated address in the From: |
||
| 351 | * header and reserve the user for Reply-To. |
||
| 352 | * |
||
| 353 | * This is a bit ugly, but will serve to differentiate |
||
| 354 | * wiki-borne mails from direct mails and protects against |
||
| 355 | * SPF and bounce problems with some mailers (see below). |
||
| 356 | */ |
||
| 357 | $mailFrom = new MailAddress( $config->get( 'PasswordSender' ), |
||
| 358 | wfMessage( 'emailsender' )->inContentLanguage()->text() ); |
||
| 359 | $replyTo = $from; |
||
| 360 | } else { |
||
| 361 | /** |
||
| 362 | * Put the sending user's e-mail address in the From: header. |
||
| 363 | * |
||
| 364 | * This is clean-looking and convenient, but has issues. |
||
| 365 | * One is that it doesn't as clearly differentiate the wiki mail |
||
| 366 | * from "directly" sent mails. |
||
| 367 | * |
||
| 368 | * Another is that some mailers (like sSMTP) will use the From |
||
| 369 | * address as the envelope sender as well. For open sites this |
||
| 370 | * can cause mails to be flunked for SPF violations (since the |
||
| 371 | * wiki server isn't an authorized sender for various users' |
||
| 372 | * domains) as well as creating a privacy issue as bounces |
||
| 373 | * containing the recipient's e-mail address may get sent to |
||
| 374 | * the sending user. |
||
| 375 | */ |
||
| 376 | $mailFrom = $from; |
||
| 377 | $replyTo = null; |
||
| 378 | } |
||
| 379 | |||
| 380 | $status = UserMailer::send( $to, $mailFrom, $subject, $text, [ |
||
| 381 | 'replyTo' => $replyTo, |
||
| 382 | ] ); |
||
| 383 | |||
| 384 | if ( !$status->isGood() ) { |
||
| 385 | return $status; |
||
| 386 | } else { |
||
| 387 | // if the user requested a copy of this mail, do this now, |
||
| 388 | // unless they are emailing themselves, in which case one |
||
| 389 | // copy of the message is sufficient. |
||
| 390 | if ( $data['CCMe'] && $to != $from ) { |
||
| 391 | $ccTo = $from; |
||
| 392 | $ccFrom = $from; |
||
| 393 | $ccSubject = $context->msg( 'emailccsubject' )->rawParams( |
||
| 394 | $target->getName(), $subject )->text(); |
||
| 395 | $ccText = $text; |
||
| 396 | |||
| 397 | Hooks::run( 'EmailUserCC', [ &$ccTo, &$ccFrom, &$ccSubject, &$ccText ] ); |
||
| 398 | |||
| 399 | View Code Duplication | if ( $config->get( 'UserEmailUseReplyTo' ) ) { |
|
| 400 | $mailFrom = new MailAddress( |
||
| 401 | $config->get( 'PasswordSender' ), |
||
| 402 | wfMessage( 'emailsender' )->inContentLanguage()->text() |
||
| 403 | ); |
||
| 404 | $replyTo = $ccFrom; |
||
| 405 | } else { |
||
| 406 | $mailFrom = $ccFrom; |
||
| 407 | $replyTo = null; |
||
| 408 | } |
||
| 409 | |||
| 410 | $ccStatus = UserMailer::send( |
||
| 411 | $ccTo, $mailFrom, $ccSubject, $ccText, [ |
||
| 412 | 'replyTo' => $replyTo, |
||
| 413 | ] ); |
||
| 414 | $status->merge( $ccStatus ); |
||
| 415 | } |
||
| 416 | |||
| 417 | Hooks::run( 'EmailUserComplete', [ $to, $from, $subject, $text ] ); |
||
| 418 | |||
| 419 | return $status; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Return an array of subpages beginning with $search that this special page will accept. |
||
| 425 | * |
||
| 426 | * @param string $search Prefix to search for |
||
| 427 | * @param int $limit Maximum number of results to return (usually 10) |
||
| 428 | * @param int $offset Number of results to skip (usually 0) |
||
| 429 | * @return string[] Matching subpages |
||
| 430 | */ |
||
| 431 | View Code Duplication | public function prefixSearchSubpages( $search, $limit, $offset ) { |
|
| 440 | |||
| 441 | protected function getGroupName() { |
||
| 444 | } |
||
| 445 |