Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Image { |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Wiki class |
||
| 7 | * |
||
| 8 | * @var Wiki |
||
| 9 | * @access protected |
||
| 10 | */ |
||
| 11 | protected $wiki; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Page class |
||
| 15 | * |
||
| 16 | * @var Page |
||
| 17 | * @access protected |
||
| 18 | */ |
||
| 19 | protected $page; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * MIME type of image |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | * @access protected |
||
| 26 | */ |
||
| 27 | protected $mime; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Bitdepth of image |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | * @access protected |
||
| 34 | */ |
||
| 35 | protected $bitdepth; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * SHA1 hash of image |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | * @access protected |
||
| 42 | */ |
||
| 43 | protected $hash; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Size of image |
||
| 47 | * |
||
| 48 | * @var int |
||
| 49 | * @access protected |
||
| 50 | */ |
||
| 51 | protected $size; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Metadata stored in the image |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | * @access protected |
||
| 58 | */ |
||
| 59 | protected $metadata = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * URL to direct image |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | * @access protected |
||
| 66 | */ |
||
| 67 | protected $url; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Timestamp that of the most recent upload |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | * @access protected |
||
| 74 | */ |
||
| 75 | protected $timestamp; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Username of the most recent uploader |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | * @access protected |
||
| 82 | */ |
||
| 83 | protected $user; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Width of image |
||
| 87 | * |
||
| 88 | * @var int |
||
| 89 | * @access protected |
||
| 90 | */ |
||
| 91 | protected $width; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Height of image |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | * @access protected |
||
| 98 | */ |
||
| 99 | protected $height; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sanitized name for local storage (namespace, colons, etc all removed) |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | * @access protected |
||
| 106 | */ |
||
| 107 | protected $localname; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Image name, with namespace |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | * @access protected |
||
| 114 | */ |
||
| 115 | protected $title; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Image name, without namespace |
||
| 119 | * |
||
| 120 | * @var string |
||
| 121 | * @access protected |
||
| 122 | */ |
||
| 123 | protected $rawtitle; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * List of pages where the image is used |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | * @access protected |
||
| 130 | */ |
||
| 131 | protected $usage = array(); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * List of previous uploads |
||
| 135 | * |
||
| 136 | * @var array |
||
| 137 | * @access protected |
||
| 138 | */ |
||
| 139 | protected $history = array(); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Other images with identical SHA1 hashes |
||
| 143 | * |
||
| 144 | * @var array |
||
| 145 | * @access protected |
||
| 146 | */ |
||
| 147 | protected $duplicates = array(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Whether image itself exists or not |
||
| 151 | * |
||
| 152 | * @var bool |
||
| 153 | * @access protected |
||
| 154 | */ |
||
| 155 | protected $exists = true; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Construction method for the Image class |
||
| 159 | * |
||
| 160 | * @access public |
||
| 161 | * @param Wiki &$wikiClass The Wiki class object |
||
| 162 | * @param string $title The title of the image |
||
| 163 | * @param int $pageid The ID of the image page (optional) |
||
| 164 | * @return Image |
||
|
|
|||
| 165 | */ |
||
| 166 | public function __construct( Wiki &$wikiClass, $title = null, $pageid = null ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * |
||
| 209 | * @access public |
||
| 210 | * @link https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii |
||
| 211 | * @param int $limit Number of results to limit to. Default 50. |
||
| 212 | * @param array $prop What image information to get. Default all values. |
||
| 213 | * @return array|bool False if not found, otherwise array of info indexed by revision |
||
| 214 | */ |
||
| 215 | public function get_stashinfo( $limit = 50, $prop = array( |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns various information about the image |
||
| 239 | * |
||
| 240 | * @access public |
||
| 241 | * @param int $limit Number of revisions to get info about. Default 1 |
||
| 242 | * @param int $width Width of image. Default -1 (no width) |
||
| 243 | * @param int $height Height of image. Default -1 (no height) |
||
| 244 | * @param string $start Timestamp to start at. Default null |
||
| 245 | * @param string $end Timestamp to end at. Default null |
||
| 246 | * @param string[] $prop Properties to retrieve. Default array( 'timestamp', 'user', 'comment', 'url', 'size', 'sha1', 'mime', 'metadata', 'archivename', 'bitdepth' ) |
||
| 247 | * @param string $version Version of metadata to use. Default 'latest' |
||
| 248 | * @param string $urlparam A handler specific parameter string. Default null |
||
| 249 | * @param bool $localonly Look only for files in the local repository. Default false |
||
| 250 | * @return array|bool False if file does not exist, otherwise array of info indexed by revision |
||
| 251 | */ |
||
| 252 | public function imageinfo( $limit = 1, $width = -1, $height = -1, $start = null, $end = null, $prop = array( |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns the upload history of the image. If function was already called earlier in the script, it will return the local cache unless $force is set to true. |
||
| 287 | * |
||
| 288 | * @access public |
||
| 289 | * @param string $dir Which direction to go. Default 'older' |
||
| 290 | * @param int $limit Number of revisions to get. Default null (all revisions) |
||
| 291 | * @param bool $force Force generation of the cache. Default false (use cache). |
||
| 292 | * @return array Upload history. |
||
| 293 | */ |
||
| 294 | public function get_history( $dir = 'older', $limit = null, $force = false ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Returns all pages where the image is used. If function was already called earlier in the script, it will return the local cache unless $force is set to true. |
||
| 303 | * |
||
| 304 | * @access public |
||
| 305 | * @param string|array $namespace Namespaces to look in. If set as a string, must be set in the syntax "0|1|2|...". If an array, simply the namespace IDs to look in. Default null. |
||
| 306 | * @param string $redirects How to filter for redirects. Options are "all", "redirects", or "nonredirects". Default "all". |
||
| 307 | * @param bool $followRedir If linking page is a redirect, find all pages that link to that redirect as well. Default false. |
||
| 308 | * @param int|null $limit |
||
| 309 | * @param bool $force Force regeneration of the cache. Default false (use cache). |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function get_usage( $namespace = null, $redirects = "all", $followRedir = false, $limit = null, $force = false ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns an array of all files with identical sha1 hashes |
||
| 347 | * |
||
| 348 | * @param int $limit Number of duplicates to get. Default null (all) |
||
| 349 | * @param bool $force Force regeneration of the cache. Default false (use cache). |
||
| 350 | * @return array Duplicate files |
||
| 351 | */ |
||
| 352 | public function get_duplicates( $limit = null, $force = false ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Revert a file to an old version |
||
| 402 | * |
||
| 403 | * @access public |
||
| 404 | * @param string $comment Comment for inthe upload in logs (default: '') |
||
| 405 | * @param string $revertto Archive name of the revision to revert to. Default null. |
||
| 406 | * @return boolean |
||
| 407 | */ |
||
| 408 | public function revert( $comment = '', $revertto = null ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Rotate the image clockwise a certain degree. |
||
| 454 | * |
||
| 455 | * @param integer $degree Degrees to rotate image clockwise |
||
| 456 | * @return boolean |
||
| 457 | */ |
||
| 458 | public function rotate( $degree = 90 ) { |
||
| 459 | $tokens = $this->wiki->get_tokens(); |
||
| 460 | |||
| 461 | $apiArray = array( |
||
| 462 | 'action' => 'imagerotate', |
||
| 463 | 'token' => $tokens['edit'], |
||
| 464 | 'titles' => $this->title |
||
| 465 | ); |
||
| 466 | pecho( "Rotating image(s) $degree degrees...\n\n", PECHO_NOTICE ); |
||
| 467 | |||
| 468 | try{ |
||
| 469 | $this->preEditChecks( "Rotate" ); |
||
| 470 | } catch( EditError $e ){ |
||
| 471 | pecho( "Error: $e\n\n", PECHO_FATAL ); |
||
| 472 | return false; |
||
| 473 | } |
||
| 474 | |||
| 475 | $result = $this->wiki->apiQuery( $apiArray, true ); |
||
| 476 | |||
| 477 | if( isset( $result['imagerotate'] ) ) { |
||
| 478 | if( isset( $result['imagerotate']['result'] ) && $result['imagerotate']['result'] == "Success" ) { |
||
| 479 | $this->__construct( $this->wiki, $this->title ); |
||
| 480 | return true; |
||
| 481 | } else { |
||
| 482 | pecho( "Rotate error...\n\n" . print_r( $result['imagerotate'], true ) . "\n\n", PECHO_FATAL ); |
||
| 483 | return false; |
||
| 484 | } |
||
| 485 | } else { |
||
| 486 | pecho( "Rotate error...\n\n" . print_r( $result, true ), PECHO_FATAL ); |
||
| 487 | return false; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Upload an image to the wiki |
||
| 493 | * |
||
| 494 | * @access public |
||
| 495 | * @param string $file Identifier of a file. Flexible format (local path, URL) |
||
| 496 | * @param string $text Text on the image file page (default: '') |
||
| 497 | * @param string $comment Comment for inthe upload in logs (default: '') |
||
| 498 | * @param bool $watch Should the upload be added to the watchlist (default: false) |
||
| 499 | * @param bool $ignorewarnings Ignore warnings about the upload (default: true) |
||
| 500 | * @param bool $async Make potentially large file operations asynchronous when possible. Default false. |
||
| 501 | * @throws BadEntryError |
||
| 502 | * @return boolean |
||
| 503 | */ |
||
| 504 | public function upload( $file, $text = '', $comment = '', $watch = null, $ignorewarnings = true, $async = false ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Upload an image to the wiki using api.php |
||
| 553 | * |
||
| 554 | * @access public |
||
| 555 | * @param mixed $file Absolute path to the image, a URL, or an array containing file chunks for a chunk upload. |
||
| 556 | * @param string $text Text on the image file page (default: '') |
||
| 557 | * @param string $comment Comment for inthe upload in logs (default: '') |
||
| 558 | * @param bool $watch Should the upload be added to the watchlist (default: false) |
||
| 559 | * @param bool $ignorewarnings Ignore warnings about the upload (default: true) |
||
| 560 | * @param bool $async Make potentially large file operations asynchronous when possible. Default false. |
||
| 561 | * @param string $filekey Key that identifies a previous upload that was stashed temporarily. Default null. |
||
| 562 | * @notice This feature is not yet fully developed. Manual stashing is not allowed at this time. This will be corrected during the final release of Peachy 2. |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | public function api_upload( $file, $text = '', $comment = '', $watch = null, $ignorewarnings = true, $async = false, $filekey = null ) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Downloads an image to the local disk |
||
| 655 | * |
||
| 656 | * @param string $localname Filename to store image as. Default null. |
||
| 657 | * @param int $width Width of image to download. Default -1. |
||
| 658 | * @param int $height Height of image to download. Default -1. |
||
| 659 | * @return void |
||
| 660 | */ |
||
| 661 | public function download( $localname = null, $width = -1, $height = -1 ) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Resize an image |
||
| 695 | * |
||
| 696 | * @access public |
||
| 697 | * @param int $width Width of resized image. Default null |
||
| 698 | * @param int $height Height of resized image. Default null. |
||
| 699 | * @param bool $reupload Whether or not to automatically upload the image again. Default false |
||
| 700 | * @param string $text Text to use for the image name |
||
| 701 | * @param string $comment Upload comment. |
||
| 702 | * @param bool $watch Whether or not to watch the image on uploading |
||
| 703 | * @param bool $ignorewarnings Whether or not to ignore upload warnings |
||
| 704 | * @throws DependencyError Relies on GD PHP plugin |
||
| 705 | * @throws BadEntryError |
||
| 706 | * @return boolean|void False on failure |
||
| 707 | */ |
||
| 708 | public function resize( $width = null, $height = null, $reupload = false, $text = '', $comment = '', $watch = null, $ignorewarnings = true ) { |
||
| 709 | global $pgIP, $pgNotag, $pgTag; |
||
| 710 | |||
| 711 | try{ |
||
| 712 | $this->preEditChecks( "Resize" ); |
||
| 713 | } catch( EditError $e ){ |
||
| 714 | pecho( "Error: $e\n\n", PECHO_FATAL ); |
||
| 715 | return false; |
||
| 716 | } |
||
| 717 | |||
| 718 | if( !$pgNotag ) $comment .= $pgTag; |
||
| 719 | if( !function_exists( 'ImageCreateTrueColor' ) ) { |
||
| 720 | throw new DependencyError( "GD", "http://us2.php.net/manual/en/book.image.php" ); |
||
| 721 | } |
||
| 722 | echo "1\n"; |
||
| 723 | if( !is_null( $width ) && !is_null( $height ) ) { |
||
| 724 | $this->download(); |
||
| 725 | |||
| 726 | $type = substr( strrchr( $this->mime, '/' ), 1 ); |
||
| 727 | |||
| 728 | switch( $type ){ |
||
| 729 | case 'jpeg': |
||
| 730 | $image_create_func = 'ImageCreateFromJPEG'; |
||
| 731 | $image_save_func = 'ImageJPEG'; |
||
| 732 | break; |
||
| 733 | |||
| 734 | case 'png': |
||
| 735 | $image_create_func = 'ImageCreateFromPNG'; |
||
| 736 | $image_save_func = 'ImagePNG'; |
||
| 737 | break; |
||
| 738 | |||
| 739 | case 'bmp': |
||
| 740 | $image_create_func = 'ImageCreateFromBMP'; |
||
| 741 | $image_save_func = 'ImageBMP'; |
||
| 742 | break; |
||
| 743 | |||
| 744 | case 'gif': |
||
| 745 | $image_create_func = 'ImageCreateFromGIF'; |
||
| 746 | $image_save_func = 'ImageGIF'; |
||
| 747 | break; |
||
| 748 | |||
| 749 | case 'vnd.wap.wbmp': |
||
| 750 | $image_create_func = 'ImageCreateFromWBMP'; |
||
| 751 | $image_save_func = 'ImageWBMP'; |
||
| 752 | break; |
||
| 753 | |||
| 754 | case 'xbm': |
||
| 755 | $image_create_func = 'ImageCreateFromXBM'; |
||
| 756 | $image_save_func = 'ImageXBM'; |
||
| 757 | break; |
||
| 758 | |||
| 759 | default: |
||
| 760 | $image_create_func = 'ImageCreateFromJPEG'; |
||
| 761 | $image_save_func = 'ImageJPEG'; |
||
| 762 | } |
||
| 763 | echo "2\n"; |
||
| 764 | $image = imagecreatetruecolor( $width, $height ); |
||
| 765 | echo "3\n"; |
||
| 766 | $new_image = $image_create_func( $pgIP . 'Images/' . $this->localname ); |
||
| 767 | |||
| 768 | $info = getimagesize( $pgIP . 'Images/' . $this->localname ); |
||
| 769 | |||
| 770 | imagecopyresampled( $image, $new_image, 0, 0, 0, 0, $width, $height, $info[0], $info[1] ); |
||
| 771 | |||
| 772 | $image_save_func( $image, $pgIP . 'Images/' . $this->localname ); |
||
| 773 | |||
| 774 | } elseif( !is_null( $width ) ) { |
||
| 775 | $this->download( null, $width ); |
||
| 776 | } elseif( !is_null( $height ) ) { |
||
| 777 | $this->download( null, $height + 100000, $height ); |
||
| 778 | } else { |
||
| 779 | throw new BadEntryError( "NoParams", "No parameters given" ); |
||
| 780 | } |
||
| 781 | |||
| 782 | if( $reupload ) { |
||
| 783 | return $this->upload( null, $text, $comment, $watch, $ignorewarnings ); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Returns the normalized image name |
||
| 789 | * |
||
| 790 | * @param bool $namespace Whether or not to include the File: part of the name. Default true. |
||
| 791 | * @return string |
||
| 792 | */ |
||
| 793 | public function get_title( $namespace = true ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Deletes the image and page. |
||
| 803 | * |
||
| 804 | * @param string $reason A reason for the deletion. Defaults to null (blank). |
||
| 805 | * @param string|bool $watch Unconditionally add or remove the page from your watchlist, use preferences or do not change watch. Default preferences. |
||
| 806 | * @param string $oldimage The name of the old image to delete as provided by iiprop=archivename |
||
| 807 | * @return bool True on success |
||
| 808 | */ |
||
| 809 | public function delete( $reason = null, $watch = null, $oldimage = null ) { |
||
| 812 | |||
| 813 | /* |
||
| 814 | * Performs new message checking, etc |
||
| 815 | * |
||
| 816 | * @access public |
||
| 817 | * @return void |
||
| 818 | */ |
||
| 819 | protected function preEditChecks( $action = "Edit" ) { |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Returns the sanitized local disk name |
||
| 825 | * |
||
| 826 | * @return string |
||
| 827 | */ |
||
| 828 | public function get_localname() { |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Whether or not the image exists |
||
| 834 | * |
||
| 835 | * @return bool |
||
| 836 | */ |
||
| 837 | public function get_exists() { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Returns a page class for the image |
||
| 843 | * |
||
| 844 | * @return Page |
||
| 845 | */ |
||
| 846 | public function &get_page() { |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Returns the MIME type of the image |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | public function get_mime() { |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Returns the bitdepth of the image |
||
| 861 | * |
||
| 862 | * @return int |
||
| 863 | */ |
||
| 864 | public function get_bitdepth() { |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Returns the SHA1 hash of the image |
||
| 870 | * |
||
| 871 | * @return string |
||
| 872 | */ |
||
| 873 | public function get_hash() { |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Returns the size of the image, in bytes |
||
| 879 | * |
||
| 880 | * @return int |
||
| 881 | */ |
||
| 882 | public function get_size() { |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Returns the metadata of the image |
||
| 888 | * |
||
| 889 | * @return array |
||
| 890 | */ |
||
| 891 | public function get_metadata() { |
||
| 894 | |||
| 895 | /** |
||
| 896 | * Returns the direct URL of the image |
||
| 897 | * |
||
| 898 | * @return string |
||
| 899 | */ |
||
| 900 | public function get_url() { |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Returns the timestamp that of the most recent upload |
||
| 906 | * |
||
| 907 | * @return string |
||
| 908 | */ |
||
| 909 | public function get_timestamp() { |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Returns the username of the most recent uploader |
||
| 915 | * |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | public function get_user() { |
||
| 921 | |||
| 922 | /** |
||
| 923 | * Returns the width of the image |
||
| 924 | * |
||
| 925 | * @return int |
||
| 926 | */ |
||
| 927 | public function get_width() { |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Returns the height of the image |
||
| 933 | * |
||
| 934 | * @return int |
||
| 935 | */ |
||
| 936 | public function get_height() { |
||
| 939 | |||
| 940 | } |
||
| 941 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.