Complex classes like ResourceLoaderImage 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 ResourceLoaderImage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ResourceLoaderImage { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Map of allowed file extensions to their MIME types. |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected static $fileTypes = [ |
||
| 35 | 'svg' => 'image/svg+xml', |
||
| 36 | 'png' => 'image/png', |
||
| 37 | 'gif' => 'image/gif', |
||
| 38 | 'jpg' => 'image/jpg', |
||
| 39 | ]; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $name Image name |
||
| 43 | * @param string $module Module name |
||
| 44 | * @param string|array $descriptor Path to image file, or array structure containing paths |
||
| 45 | * @param string $basePath Directory to which paths in descriptor refer |
||
| 46 | * @param array $variants |
||
| 47 | * @throws InvalidArgumentException |
||
| 48 | */ |
||
| 49 | public function __construct( $name, $module, $descriptor, $basePath, $variants ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get name of this image. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getName() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get name of the module this image belongs to. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getModule() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the list of variants this image can be converted to. |
||
| 112 | * |
||
| 113 | * @return string[] |
||
| 114 | */ |
||
| 115 | public function getVariants() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the path to image file for given context. |
||
| 121 | * |
||
| 122 | * @param ResourceLoaderContext $context Any context |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getPath( ResourceLoaderContext $context ) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the extension of the image. |
||
| 140 | * |
||
| 141 | * @param string $format Format to get the extension for, 'original' or 'rasterized' |
||
| 142 | * @return string Extension without leading dot, e.g. 'png' |
||
| 143 | */ |
||
| 144 | public function getExtension( $format = 'original' ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the MIME type of the image. |
||
| 154 | * |
||
| 155 | * @param string $format Format to get the MIME type for, 'original' or 'rasterized' |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getMimeType( $format = 'original' ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get the load.php URL that will produce this image. |
||
| 165 | * |
||
| 166 | * @param ResourceLoaderContext $context Any context |
||
| 167 | * @param string $script URL to load.php |
||
| 168 | * @param string|null $variant Variant to get the URL for |
||
| 169 | * @param string $format Format to get the URL for, 'original' or 'rasterized' |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getUrl( ResourceLoaderContext $context, $script, $variant, $format ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the data: URI that will produce this image. |
||
| 187 | * |
||
| 188 | * @param ResourceLoaderContext $context Any context |
||
| 189 | * @param string|null $variant Variant to get the URI for |
||
| 190 | * @param string $format Format to get the URI for, 'original' or 'rasterized' |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getDataUri( ResourceLoaderContext $context, $variant, $format ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get actual image data for this image. This can be saved to a file or sent to the browser to |
||
| 201 | * produce the converted image. |
||
| 202 | * |
||
| 203 | * Call getExtension() or getMimeType() with the same $format argument to learn what file type the |
||
| 204 | * returned data uses. |
||
| 205 | * |
||
| 206 | * @param ResourceLoaderContext $context Image context, or any context if $variant and $format |
||
| 207 | * given. |
||
| 208 | * @param string|null $variant Variant to get the data for. Optional; if given, overrides the data |
||
| 209 | * from $context. |
||
| 210 | * @param string $format Format to get the data for, 'original' or 'rasterized'. Optional; if |
||
| 211 | * given, overrides the data from $context. |
||
| 212 | * @return string|false Possibly binary image data, or false on failure |
||
| 213 | * @throws MWException If the image file doesn't exist |
||
| 214 | */ |
||
| 215 | public function getImageData( ResourceLoaderContext $context, $variant = false, $format = false ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Send response headers (using the header() function) that are necessary to correctly serve the |
||
| 250 | * image data for this image, as returned by getImageData(). |
||
| 251 | * |
||
| 252 | * Note that the headers are independent of the language or image variant. |
||
| 253 | * |
||
| 254 | * @param ResourceLoaderContext $context Image context |
||
| 255 | */ |
||
| 256 | public function sendResponseHeaders( ResourceLoaderContext $context ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Convert this image, which is assumed to be SVG, to given variant. |
||
| 268 | * |
||
| 269 | * @param array $variantConf Array with a 'color' key, its value will be used as fill color |
||
| 270 | * @param ResourceLoaderContext $context Image context |
||
| 271 | * @return string New SVG file data |
||
| 272 | */ |
||
| 273 | protected function variantize( $variantConf, ResourceLoaderContext $context ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Massage the SVG image data for converters which don't understand some path data syntax. |
||
| 288 | * |
||
| 289 | * This is necessary for rsvg and ImageMagick when compiled with rsvg support. |
||
| 290 | * Upstream bug is https://bugzilla.gnome.org/show_bug.cgi?id=620923, fixed 2014-11-10, so |
||
| 291 | * this will be needed for a while. (T76852) |
||
| 292 | * |
||
| 293 | * @param string $svg SVG image data |
||
| 294 | * @return string Massaged SVG image data |
||
| 295 | */ |
||
| 296 | protected function massageSvgPathdata( $svg ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Convert passed image data, which is assumed to be SVG, to PNG. |
||
| 313 | * |
||
| 314 | * @param string $svg SVG image data |
||
| 315 | * @return string|bool PNG image data, or false on failure |
||
| 316 | */ |
||
| 317 | protected function rasterize( $svg ) { |
||
| 395 | } |
||
| 396 |
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: