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 |
||
| 46 | class ExternalStore { |
||
| 47 | /** |
||
| 48 | * Get an external store object of the given type, with the given parameters |
||
| 49 | * |
||
| 50 | * @param string $proto Type of external storage, should be a value in $wgExternalStores |
||
| 51 | * @param array $params Associative array of ExternalStoreMedium parameters |
||
| 52 | * @return ExternalStoreMedium|bool The store class or false on error |
||
| 53 | */ |
||
| 54 | public static function getStoreObject( $proto, array $params = [] ) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Fetch data from given URL |
||
| 69 | * |
||
| 70 | * @param string $url The URL of the text to get |
||
| 71 | * @param array $params Associative array of ExternalStoreMedium parameters |
||
| 72 | * @return string|bool The text stored or false on error |
||
| 73 | * @throws MWException |
||
| 74 | */ |
||
| 75 | View Code Duplication | public static function fetchFromURL( $url, array $params = [] ) { |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Fetch data from multiple URLs with a minimum of round trips |
||
| 96 | * |
||
| 97 | * @param array $urls The URLs of the text to get |
||
| 98 | * @return array Map from url to its data. Data is either string when found |
||
| 99 | * or false on failure. |
||
| 100 | */ |
||
| 101 | public static function batchFetchFromURLs( array $urls ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Store a data item to an external store, identified by a partial URL |
||
| 130 | * The protocol part is used to identify the class, the rest is passed to the |
||
| 131 | * class itself as a parameter. |
||
| 132 | * |
||
| 133 | * @param string $url A partial external store URL ("<store type>://<location>") |
||
| 134 | * @param string $data |
||
| 135 | * @param array $params Associative array of ExternalStoreMedium parameters |
||
| 136 | * @return string|bool The URL of the stored data item, or false on error |
||
| 137 | * @throws MWException |
||
| 138 | */ |
||
| 139 | View Code Duplication | public static function insert( $url, $data, array $params = [] ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Like insert() above, but does more of the work for us. |
||
| 160 | * This function does not need a url param, it builds it by |
||
| 161 | * itself. It also fails-over to the next possible clusters |
||
| 162 | * provided by $wgDefaultExternalStore. |
||
| 163 | * |
||
| 164 | * @param string $data |
||
| 165 | * @param array $params Associative array of ExternalStoreMedium parameters |
||
| 166 | * @return string|bool The URL of the stored data item, or false on error |
||
| 167 | * @throws MWException |
||
| 168 | */ |
||
| 169 | public static function insertToDefault( $data, array $params = [] ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Like insert() above, but does more of the work for us. |
||
| 177 | * This function does not need a url param, it builds it by |
||
| 178 | * itself. It also fails-over to the next possible clusters |
||
| 179 | * as provided in the first parameter. |
||
| 180 | * |
||
| 181 | * @param array $tryStores Refer to $wgDefaultExternalStore |
||
| 182 | * @param string $data |
||
| 183 | * @param array $params Associative array of ExternalStoreMedium parameters |
||
| 184 | * @return string|bool The URL of the stored data item, or false on error |
||
| 185 | * @throws MWException |
||
| 186 | */ |
||
| 187 | public static function insertWithFallback( array $tryStores, $data, array $params = [] ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $data |
||
| 222 | * @param string $wiki |
||
| 223 | * @return string|bool The URL of the stored data item, or false on error |
||
| 224 | * @throws MWException |
||
| 225 | */ |
||
| 226 | public static function insertToForeignDefault( $data, $wiki ) { |
||
| 229 | } |
||
| 230 |
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: