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 ResourceLoaderContext 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 ResourceLoaderContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ResourceLoaderContext { |
||
| 32 | protected $resourceLoader; |
||
| 33 | protected $request; |
||
| 34 | protected $logger; |
||
| 35 | |||
| 36 | // Module content vary |
||
| 37 | protected $skin; |
||
| 38 | protected $language; |
||
| 39 | protected $debug; |
||
| 40 | protected $user; |
||
| 41 | |||
| 42 | // Request vary (in addition to cache vary) |
||
| 43 | protected $modules; |
||
| 44 | protected $only; |
||
| 45 | protected $version; |
||
| 46 | protected $raw; |
||
| 47 | protected $image; |
||
| 48 | protected $variant; |
||
| 49 | protected $format; |
||
| 50 | |||
| 51 | protected $direction; |
||
| 52 | protected $hash; |
||
| 53 | protected $userObj; |
||
| 54 | protected $imageObj; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param ResourceLoader $resourceLoader |
||
| 58 | * @param WebRequest $request |
||
| 59 | */ |
||
| 60 | public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to |
||
| 101 | * an array of module names like [ 'jquery.foo', 'jquery.bar', |
||
| 102 | * 'jquery.ui.baz', 'jquery.ui.quux' ] |
||
| 103 | * @param string $modules Packed module name list |
||
| 104 | * @return array Array of module names |
||
| 105 | */ |
||
| 106 | public static function expandModuleNames( $modules ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Return a dummy ResourceLoaderContext object suitable for passing into |
||
| 135 | * things that don't "really" need a context. |
||
| 136 | * @return ResourceLoaderContext |
||
| 137 | */ |
||
| 138 | public static function newDummyContext() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return ResourceLoader |
||
| 147 | */ |
||
| 148 | public function getResourceLoader() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return WebRequest |
||
| 154 | */ |
||
| 155 | public function getRequest() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @since 1.27 |
||
| 161 | * @return \Psr\Log\LoggerInterface |
||
| 162 | */ |
||
| 163 | public function getLogger() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | public function getModules() { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getLanguage() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function getDirection() { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getSkin() { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return string|null |
||
| 215 | */ |
||
| 216 | public function getUser() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get a Message object with context set. See wfMessage for parameters. |
||
| 222 | * |
||
| 223 | * @since 1.27 |
||
| 224 | * @param mixed ... |
||
| 225 | * @return Message |
||
| 226 | */ |
||
| 227 | public function msg() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Get the possibly-cached User object for the specified username |
||
| 238 | * |
||
| 239 | * @since 1.25 |
||
| 240 | * @return User |
||
| 241 | */ |
||
| 242 | public function getUserObj() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function getDebug() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return string|null |
||
| 266 | */ |
||
| 267 | public function getOnly() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @see ResourceLoaderModule::getVersionHash |
||
| 273 | * @see ResourceLoaderClientHtml::makeLoad |
||
| 274 | * @return string|null |
||
| 275 | */ |
||
| 276 | public function getVersion() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function getRaw() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return string|null |
||
| 289 | */ |
||
| 290 | public function getImage() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return string|null |
||
| 296 | */ |
||
| 297 | public function getVariant() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return string|null |
||
| 303 | */ |
||
| 304 | public function getFormat() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * If this is a request for an image, get the ResourceLoaderImage object. |
||
| 310 | * |
||
| 311 | * @since 1.25 |
||
| 312 | * @return ResourceLoaderImage|bool false if a valid object cannot be created |
||
| 313 | */ |
||
| 314 | public function getImageObj() { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | public function shouldIncludeScripts() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function shouldIncludeStyles() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function shouldIncludeMessages() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * All factors that uniquely identify this request, except 'modules'. |
||
| 366 | * |
||
| 367 | * The list of modules is excluded here for legacy reasons as most callers already |
||
| 368 | * split up handling of individual modules. Including it here would massively fragment |
||
| 369 | * the cache and decrease its usefulness. |
||
| 370 | * |
||
| 371 | * E.g. Used by RequestFileCache to form a cache key for storing the reponse output. |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function getHash() { |
||
| 394 | } |
||
| 395 |
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: