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 |
||
| 29 | class RevisionDeleter { |
||
| 30 | /** List of known revdel types, with their corresponding list classes */ |
||
| 31 | private static $allowedTypes = [ |
||
| 32 | 'revision' => 'RevDelRevisionList', |
||
| 33 | 'archive' => 'RevDelArchiveList', |
||
| 34 | 'oldimage' => 'RevDelFileList', |
||
| 35 | 'filearchive' => 'RevDelArchivedFileList', |
||
| 36 | 'logging' => 'RevDelLogList', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** Type map to support old log entries */ |
||
| 40 | private static $deprecatedTypeMap = [ |
||
| 41 | 'oldid' => 'revision', |
||
| 42 | 'artimestamp' => 'archive', |
||
| 43 | 'oldimage' => 'oldimage', |
||
| 44 | 'fileid' => 'filearchive', |
||
| 45 | 'logid' => 'logging', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Lists the valid possible types for revision deletion. |
||
| 50 | * |
||
| 51 | * @since 1.22 |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public static function getTypes() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Gets the canonical type name, if any. |
||
| 60 | * |
||
| 61 | * @since 1.22 |
||
| 62 | * @param string $typeName |
||
| 63 | * @return string|null |
||
| 64 | */ |
||
| 65 | public static function getCanonicalTypeName( $typeName ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Instantiate the appropriate list class for a given list of IDs. |
||
| 74 | * |
||
| 75 | * @since 1.22 |
||
| 76 | * @param string $typeName RevDel type, see RevisionDeleter::getTypes() |
||
| 77 | * @param IContextSource $context |
||
| 78 | * @param Title $title |
||
| 79 | * @param array $ids |
||
| 80 | * @return RevDelList |
||
| 81 | * @throws MWException |
||
| 82 | */ |
||
| 83 | public static function createList( $typeName, IContextSource $context, Title $title, array $ids ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Checks for a change in the bitfield for a certain option and updates the |
||
| 94 | * provided array accordingly. |
||
| 95 | * |
||
| 96 | * @param string $desc Description to add to the array if the option was |
||
| 97 | * enabled / disabled. |
||
| 98 | * @param int $field The bitmask describing the single option. |
||
| 99 | * @param int $diff The xor of the old and new bitfields. |
||
| 100 | * @param int $new The new bitfield |
||
| 101 | * @param array $arr The array to update. |
||
| 102 | */ |
||
| 103 | protected static function checkItem( $desc, $field, $diff, $new, &$arr ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Gets an array of message keys describing the changes made to the |
||
| 111 | * visibility of the revision. |
||
| 112 | * |
||
| 113 | * If the resulting array is $arr, then $arr[0] will contain an array of |
||
| 114 | * keys describing the items that were hidden, $arr[1] will contain |
||
| 115 | * an array of keys describing the items that were unhidden, and $arr[2] |
||
| 116 | * will contain an array with a single message key, which can be one of |
||
| 117 | * "revdelete-restricted", "revdelete-unrestricted" indicating (un)suppression |
||
| 118 | * or null to indicate nothing in particular. |
||
| 119 | * You can turn the keys in $arr[0] and $arr[1] into message keys by |
||
| 120 | * appending -hid and -unhid to the keys respectively. |
||
| 121 | * |
||
| 122 | * @param int $n The new bitfield. |
||
| 123 | * @param int $o The old bitfield. |
||
| 124 | * @return array An array as described above. |
||
| 125 | * @since 1.19 public |
||
| 126 | */ |
||
| 127 | public static function getChanges( $n, $o ) { |
||
| 147 | |||
| 148 | /** Get DB field name for URL param... |
||
| 149 | * Future code for other things may also track |
||
| 150 | * other types of revision-specific changes. |
||
| 151 | * @param string $typeName |
||
| 152 | * @return string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name |
||
| 153 | */ |
||
| 154 | View Code Duplication | public static function getRelationType( $typeName ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the user right required for the RevDel type |
||
| 164 | * @since 1.22 |
||
| 165 | * @param string $typeName |
||
| 166 | * @return string User right |
||
| 167 | */ |
||
| 168 | View Code Duplication | public static function getRestriction( $typeName ) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Get the revision deletion constant for the RevDel type |
||
| 178 | * @since 1.22 |
||
| 179 | * @param string $typeName |
||
| 180 | * @return int RevDel constant |
||
| 181 | */ |
||
| 182 | View Code Duplication | public static function getRevdelConstant( $typeName ) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Suggest a target for the revision deletion |
||
| 192 | * @since 1.22 |
||
| 193 | * @param string $typeName |
||
| 194 | * @param Title|null $target User-supplied target |
||
| 195 | * @param array $ids |
||
| 196 | * @return Title|null |
||
| 197 | */ |
||
| 198 | public static function suggestTarget( $typeName, $target, array $ids ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if a revision still exists in the revision table. |
||
| 208 | * If it doesn't, returns the corresponding ar_timestamp field |
||
| 209 | * so that this key can be used instead. |
||
| 210 | * |
||
| 211 | * @param Title $title |
||
| 212 | * @param int $revid |
||
| 213 | * @return bool|mixed |
||
| 214 | */ |
||
| 215 | public static function checkRevisionExistence( $title, $revid ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Put together a rev_deleted bitfield |
||
| 234 | * @since 1.22 |
||
| 235 | * @param array $bitPars ExtractBitParams() params |
||
| 236 | * @param int $oldfield Current bitfield |
||
| 237 | * @return integer |
||
| 238 | */ |
||
| 239 | public static function extractBitfield( array $bitPars, $oldfield ) { |
||
| 251 | } |
||
| 252 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: