Complex classes like MediaFile 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 MediaFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class MediaFile { |
||
| 27 | /** @var string The filename */ |
||
| 28 | private $multimedia_file_refn = ''; |
||
| 29 | |||
| 30 | /** @var string The file extension; jpeg, txt, mp4, etc. */ |
||
| 31 | private $multimedia_format = ''; |
||
| 32 | |||
| 33 | /** @var string The type of document; newspaper, microfiche, etc. */ |
||
| 34 | private $source_media_type = ''; |
||
| 35 | /** @var string The filename */ |
||
| 36 | |||
| 37 | /** @var string The name of the document */ |
||
| 38 | private $descriptive_title = ''; |
||
| 39 | |||
| 40 | /** @var Media $media The media object to which this file belongs */ |
||
| 41 | private $media; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | private $fact_id; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create a MediaFile from raw GEDCOM data. |
||
| 48 | * |
||
| 49 | * @param string $gedcom |
||
| 50 | * @param Media $media |
||
| 51 | */ |
||
| 52 | public function __construct($gedcom, Media $media) { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the filename. |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function filename(): string { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Get the format. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function format(): string { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the type. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function type(): string { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the title. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function title(): string { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get the fact ID. |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function factId(): string { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Display an image-thumbnail or a media-icon, and add markup for image viewers such as colorbox. |
||
| 120 | * |
||
| 121 | * @param int $width Pixels |
||
| 122 | * @param int $height Pixels |
||
| 123 | * @param string $fit "crop" or "contain" |
||
| 124 | * @param string[] $attributes Additional HTML attributes |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function displayImage($width, $height, $fit, $attributes = []) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * A list of image attributes |
||
| 159 | * |
||
| 160 | * @return string[] |
||
| 161 | */ |
||
| 162 | public function attributes(): array { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * check if the file exists on this server |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | public function fileExists() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Is the media file actually a URL? |
||
| 190 | */ |
||
| 191 | public function isExternal(): bool { |
||
| 192 | return strpos($this->multimedia_file_refn, '://') !== false; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Is the media file an image? |
||
| 197 | */ |
||
| 198 | public function isImage(): bool { |
||
| 199 | return in_array($this->extension(), ['jpeg', 'jpg', 'gif', 'png']); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Where is the file stored on disk? |
||
| 204 | */ |
||
| 205 | public function folder(): string { |
||
| 206 | return WT_DATA_DIR . $this->media->getTree()->getPreference('MEDIA_DIRECTORY'); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * A user-friendly view of the file size |
||
| 211 | * |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | private function fileSizeBytes(): int { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * get the media file size in KB |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function fileSizeKB() { |
||
| 235 | |||
| 236 | /////////////////////////////////////////////////////////////////////////// |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the filename on the server - for those (very few!) functions which actually |
||
| 240 | * need the filename, such as mediafirewall.php and the PDF reports. |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getServerFilename() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * get image properties |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function getImageAttributes() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generate a URL for an image. |
||
| 327 | * |
||
| 328 | * @param int $width Maximum width in pixels |
||
| 329 | * @param int $height Maximum height in pixels |
||
| 330 | * @param string $fit "crop" or "contain" |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function imageUrl($width, $height, $fit) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * What file extension is used by this file? |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | public function extension() { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * What is the mime-type of this object? |
||
| 383 | * For simplicity and efficiency, use the extension, rather than the contents. |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function mimeType() { |
||
| 442 | } |
||
| 443 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: