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 ResourceLoaderImageModule 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 ResourceLoaderImageModule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class ResourceLoaderImageModule extends ResourceLoaderModule { |
||
| 30 | |||
| 31 | protected $definition = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Local base path, see __construct() |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $localBasePath = ''; |
||
| 38 | |||
| 39 | protected $origin = self::ORIGIN_CORE_SITEWIDE; |
||
| 40 | |||
| 41 | protected $images = []; |
||
| 42 | protected $variants = []; |
||
| 43 | protected $prefix = null; |
||
| 44 | protected $selectorWithoutVariant = '.{prefix}-{name}'; |
||
| 45 | protected $selectorWithVariant = '.{prefix}-{name}-{variant}'; |
||
| 46 | protected $targets = [ 'desktop', 'mobile' ]; |
||
| 47 | |||
| 48 | /** @var string Position on the page to load this module at */ |
||
| 49 | protected $position = 'bottom'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructs a new module from an options array. |
||
| 53 | * |
||
| 54 | * @param array $options List of options; if not given or empty, an empty module will be |
||
| 55 | * constructed |
||
| 56 | * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults |
||
| 57 | * to $IP |
||
| 58 | * |
||
| 59 | * Below is a description for the $options array: |
||
| 60 | * @par Construction options: |
||
| 61 | * @code |
||
| 62 | * [ |
||
| 63 | * // Base path to prepend to all local paths in $options. Defaults to $IP |
||
| 64 | * 'localBasePath' => [base path], |
||
| 65 | * // Path to JSON file that contains any of the settings below |
||
| 66 | * 'data' => [file path string] |
||
| 67 | * // CSS class prefix to use in all style rules |
||
| 68 | * 'prefix' => [CSS class prefix], |
||
| 69 | * // Alternatively: Format of CSS selector to use in all style rules |
||
| 70 | * 'selector' => [CSS selector template, variables: {prefix} {name} {variant}], |
||
| 71 | * // Alternatively: When using variants |
||
| 72 | * 'selectorWithoutVariant' => [CSS selector template, variables: {prefix} {name}], |
||
| 73 | * 'selectorWithVariant' => [CSS selector template, variables: {prefix} {name} {variant}], |
||
| 74 | * // List of variants that may be used for the image files |
||
| 75 | * 'variants' => [ |
||
| 76 | * [theme name] => [ |
||
| 77 | * [variant name] => [ |
||
| 78 | * 'color' => [color string, e.g. '#ffff00'], |
||
| 79 | * 'global' => [boolean, if true, this variant is available |
||
| 80 | * for all images of this type], |
||
| 81 | * ], |
||
| 82 | * ... |
||
| 83 | * ], |
||
| 84 | * ... |
||
| 85 | * ], |
||
| 86 | * // List of image files and their options |
||
| 87 | * 'images' => [ |
||
| 88 | * [theme name] => [ |
||
| 89 | * [icon name] => [ |
||
| 90 | * 'file' => [file path string or array whose values are file path strings |
||
| 91 | * and whose keys are 'default', 'ltr', 'rtl', a single |
||
| 92 | * language code like 'en', or a list of language codes like |
||
| 93 | * 'en,de,ar'], |
||
| 94 | * 'variants' => [array of variant name strings, variants |
||
| 95 | * available for this image], |
||
| 96 | * ], |
||
| 97 | * ... |
||
| 98 | * ], |
||
| 99 | * ... |
||
| 100 | * ], |
||
| 101 | * ] |
||
| 102 | * @endcode |
||
| 103 | * @throws InvalidArgumentException |
||
| 104 | */ |
||
| 105 | public function __construct( $options = [], $localBasePath = null ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Parse definition and external JSON data, if referenced. |
||
| 113 | */ |
||
| 114 | protected function loadFromDefinition() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get CSS class prefix used by this module. |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function getPrefix() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get CSS selector templates used by this module. |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function getSelectors() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get a ResourceLoaderImage object for given image. |
||
| 222 | * @param string $name Image name |
||
| 223 | * @param ResourceLoaderContext $context |
||
| 224 | * @return ResourceLoaderImage|null |
||
| 225 | */ |
||
| 226 | public function getImage( $name, ResourceLoaderContext $context ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get ResourceLoaderImage objects for all images. |
||
| 234 | * @param ResourceLoaderContext $context |
||
| 235 | * @return ResourceLoaderImage[] Array keyed by image name |
||
| 236 | */ |
||
| 237 | public function getImages( ResourceLoaderContext $context ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get list of variants in this module that are 'global', i.e., available |
||
| 282 | * for every image regardless of image options. |
||
| 283 | * @param ResourceLoaderContext $context |
||
| 284 | * @return string[] |
||
| 285 | */ |
||
| 286 | public function getGlobalVariants( ResourceLoaderContext $context ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param ResourceLoaderContext $context |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function getStyles( ResourceLoaderContext $context ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * SVG support using a transparent gradient to guarantee cross-browser |
||
| 361 | * compatibility (browsers able to understand gradient syntax support also SVG). |
||
| 362 | * http://pauginer.tumblr.com/post/36614680636/invisible-gradient-technique |
||
| 363 | * |
||
| 364 | * Keep synchronized with the .background-image-svg LESS mixin in |
||
| 365 | * /resources/src/mediawiki.less/mediawiki.mixins.less. |
||
| 366 | * |
||
| 367 | * @param string $primary Primary URI |
||
| 368 | * @param string $fallback Fallback URI |
||
| 369 | * @return string[] CSS declarations to use given URIs as background-image |
||
| 370 | */ |
||
| 371 | protected function getCssDeclarations( $primary, $fallback ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function supportsURLLoading() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get the definition summary for this module. |
||
| 389 | * |
||
| 390 | * @param ResourceLoaderContext $context |
||
| 391 | * @return array |
||
| 392 | */ |
||
| 393 | public function getDefinitionSummary( ResourceLoaderContext $context ) { |
||
| 394 | $this->loadFromDefinition(); |
||
| 395 | $summary = parent::getDefinitionSummary( $context ); |
||
| 396 | |||
| 397 | $options = []; |
||
| 398 | foreach ( [ |
||
| 399 | 'localBasePath', |
||
| 400 | 'images', |
||
| 401 | 'variants', |
||
| 402 | 'prefix', |
||
| 403 | 'selectorWithoutVariant', |
||
| 404 | 'selectorWithVariant', |
||
| 405 | ] as $member ) { |
||
| 406 | $options[$member] = $this->{$member}; |
||
| 407 | }; |
||
| 408 | |||
| 409 | $summary[] = [ |
||
| 410 | 'options' => $options, |
||
| 411 | 'fileHashes' => $this->getFileHashes( $context ), |
||
| 412 | ]; |
||
| 413 | return $summary; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Helper method for getDefinitionSummary. |
||
| 418 | */ |
||
| 419 | protected function getFileHashes( ResourceLoaderContext $context ) { |
||
| 420 | $this->loadFromDefinition(); |
||
| 421 | $files = []; |
||
| 422 | foreach ( $this->getImages( $context ) as $name => $image ) { |
||
| 423 | $files[] = $image->getPath( $context ); |
||
| 424 | } |
||
| 425 | $files = array_values( array_unique( $files ) ); |
||
| 426 | return array_map( [ __CLASS__, 'safeFileHash' ], $files ); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Extract a local base path from module definition information. |
||
| 431 | * |
||
| 432 | * @param array $options Module definition |
||
| 433 | * @param string $localBasePath Path to use if not provided in module definition. Defaults |
||
| 434 | * to $IP |
||
| 435 | * @return string Local base path |
||
| 436 | */ |
||
| 437 | public static function extractLocalBasePath( $options, $localBasePath = null ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getPosition() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getType() { |
||
| 465 | } |
||
| 466 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: