Complex classes like helper_plugin_extension_extension 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 helper_plugin_extension_extension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class helper_plugin_extension_extension extends DokuWiki_Plugin |
||
| 15 | { |
||
| 16 | private $id; |
||
| 17 | private $base; |
||
| 18 | private $is_template = false; |
||
| 19 | private $localInfo; |
||
| 20 | private $remoteInfo; |
||
| 21 | private $managerData; |
||
| 22 | /** @var helper_plugin_extension_repository $repository */ |
||
| 23 | private $repository = null; |
||
| 24 | |||
| 25 | /** @var array list of temporary directories */ |
||
| 26 | private $temporary = array(); |
||
| 27 | |||
| 28 | /** @var string where templates are installed to */ |
||
| 29 | private $tpllib = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * helper_plugin_extension_extension constructor. |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Destructor |
||
| 41 | * |
||
| 42 | * deletes any dangling temporary directories |
||
| 43 | */ |
||
| 44 | public function __destruct() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return bool false, this component is not a singleton |
||
| 53 | */ |
||
| 54 | public function isSingleton() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Set the name of the extension this instance shall represents, triggers loading the local and remote data |
||
| 61 | * |
||
| 62 | * @param string $id The id of the extension (prefixed with template: for templates) |
||
| 63 | * @return bool If some (local or remote) data was found |
||
| 64 | */ |
||
| 65 | public function setExtension($id) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * If the extension is installed locally |
||
| 98 | * |
||
| 99 | * @return bool If the extension is installed locally |
||
| 100 | */ |
||
| 101 | public function isInstalled() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * If the extension is under git control |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isGitControlled() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * If the extension is bundled |
||
| 119 | * |
||
| 120 | * @return bool If the extension is bundled |
||
| 121 | */ |
||
| 122 | public function isBundled() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * If the extension is protected against any modification (disable/uninstall) |
||
| 138 | * |
||
| 139 | * @return bool if the extension is protected |
||
| 140 | */ |
||
| 141 | public function isProtected() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * If the extension is installed in the correct directory |
||
| 155 | * |
||
| 156 | * @return bool If the extension is installed in the correct directory |
||
| 157 | */ |
||
| 158 | public function isInWrongFolder() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * If the extension is enabled |
||
| 165 | * |
||
| 166 | * @return bool If the extension is enabled |
||
| 167 | */ |
||
| 168 | public function isEnabled() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * If the extension should be updated, i.e. if an updated version is available |
||
| 182 | * |
||
| 183 | * @return bool If an update is available |
||
| 184 | */ |
||
| 185 | public function updateAvailable() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * If the extension is a template |
||
| 198 | * |
||
| 199 | * @return bool If this extension is a template |
||
| 200 | */ |
||
| 201 | public function isTemplate() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the ID of the extension |
||
| 208 | * |
||
| 209 | * This is the same as getName() for plugins, for templates it's getName() prefixed with 'template:' |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getID() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the name of the installation directory |
||
| 220 | * |
||
| 221 | * @return string The name of the installation directory |
||
| 222 | */ |
||
| 223 | public function getInstallName() |
||
| 227 | |||
| 228 | // Data from plugin.info.txt/template.info.txt or the repo when not available locally |
||
| 229 | /** |
||
| 230 | * Get the basename of the extension |
||
| 231 | * |
||
| 232 | * @return string The basename |
||
| 233 | */ |
||
| 234 | public function getBase() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the display name of the extension |
||
| 242 | * |
||
| 243 | * @return string The display name |
||
| 244 | */ |
||
| 245 | public function getDisplayName() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the author name of the extension |
||
| 254 | * |
||
| 255 | * @return string|bool The name of the author or false if there is none |
||
| 256 | */ |
||
| 257 | public function getAuthor() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get the email of the author of the extension if there is any |
||
| 266 | * |
||
| 267 | * @return string|bool The email address or false if there is none |
||
| 268 | */ |
||
| 269 | public function getEmail() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the email id, i.e. the md5sum of the email |
||
| 278 | * |
||
| 279 | * @return string|bool The md5sum of the email if there is any, false otherwise |
||
| 280 | */ |
||
| 281 | public function getEmailID() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the description of the extension |
||
| 290 | * |
||
| 291 | * @return string The description |
||
| 292 | */ |
||
| 293 | public function getDescription() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the URL of the extension, usually a page on dokuwiki.org |
||
| 302 | * |
||
| 303 | * @return string The URL |
||
| 304 | */ |
||
| 305 | public function getURL() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the installed version of the extension |
||
| 313 | * |
||
| 314 | * @return string|bool The version, usually in the form yyyy-mm-dd if there is any |
||
| 315 | */ |
||
| 316 | public function getInstalledVersion() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the install date of the current version |
||
| 325 | * |
||
| 326 | * @return string|bool The date of the last update or false if not available |
||
| 327 | */ |
||
| 328 | public function getUpdateDate() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the date of the installation of the plugin |
||
| 336 | * |
||
| 337 | * @return string|bool The date of the installation or false if not available |
||
| 338 | */ |
||
| 339 | public function getInstallDate() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get the names of the dependencies of this extension |
||
| 347 | * |
||
| 348 | * @return array The base names of the dependencies |
||
| 349 | */ |
||
| 350 | public function getDependencies() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the names of the missing dependencies |
||
| 358 | * |
||
| 359 | * @return array The base names of the missing dependencies |
||
| 360 | */ |
||
| 361 | public function getMissingDependencies() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get the names of all conflicting extensions |
||
| 377 | * |
||
| 378 | * @return array The names of the conflicting extensions |
||
| 379 | */ |
||
| 380 | public function getConflicts() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the names of similar extensions |
||
| 388 | * |
||
| 389 | * @return array The names of similar extensions |
||
| 390 | */ |
||
| 391 | public function getSimilarExtensions() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get the names of the tags of the extension |
||
| 399 | * |
||
| 400 | * @return array The names of the tags of the extension |
||
| 401 | */ |
||
| 402 | public function getTags() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Get the popularity information as floating point number [0,1] |
||
| 410 | * |
||
| 411 | * @return float|bool The popularity information or false if it isn't available |
||
| 412 | */ |
||
| 413 | public function getPopularity() |
||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * Get the text of the security warning if there is any |
||
| 422 | * |
||
| 423 | * @return string|bool The security warning if there is any, false otherwise |
||
| 424 | */ |
||
| 425 | public function getSecurityWarning() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the text of the security issue if there is any |
||
| 433 | * |
||
| 434 | * @return string|bool The security issue if there is any, false otherwise |
||
| 435 | */ |
||
| 436 | public function getSecurityIssue() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get the URL of the screenshot of the extension if there is any |
||
| 444 | * |
||
| 445 | * @return string|bool The screenshot URL if there is any, false otherwise |
||
| 446 | */ |
||
| 447 | public function getScreenshotURL() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the URL of the thumbnail of the extension if there is any |
||
| 455 | * |
||
| 456 | * @return string|bool The thumbnail URL if there is any, false otherwise |
||
| 457 | */ |
||
| 458 | public function getThumbnailURL() |
||
| 463 | /** |
||
| 464 | * Get the last used download URL of the extension if there is any |
||
| 465 | * |
||
| 466 | * @return string|bool The previously used download URL, false if the extension has been installed manually |
||
| 467 | */ |
||
| 468 | public function getLastDownloadURL() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Get the download URL of the extension if there is any |
||
| 476 | * |
||
| 477 | * @return string|bool The download URL if there is any, false otherwise |
||
| 478 | */ |
||
| 479 | public function getDownloadURL() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * If the download URL has changed since the last download |
||
| 487 | * |
||
| 488 | * @return bool If the download URL has changed |
||
| 489 | */ |
||
| 490 | public function hasDownloadURLChanged() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get the bug tracker URL of the extension if there is any |
||
| 499 | * |
||
| 500 | * @return string|bool The bug tracker URL if there is any, false otherwise |
||
| 501 | */ |
||
| 502 | public function getBugtrackerURL() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the URL of the source repository if there is any |
||
| 510 | * |
||
| 511 | * @return string|bool The URL of the source repository if there is any, false otherwise |
||
| 512 | */ |
||
| 513 | public function getSourcerepoURL() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get the donation URL of the extension if there is any |
||
| 521 | * |
||
| 522 | * @return string|bool The donation URL if there is any, false otherwise |
||
| 523 | */ |
||
| 524 | public function getDonationURL() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get the extension type(s) |
||
| 532 | * |
||
| 533 | * @return array The type(s) as array of strings |
||
| 534 | */ |
||
| 535 | public function getTypes() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get a list of all DokuWiki versions this extension is compatible with |
||
| 544 | * |
||
| 545 | * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit) |
||
| 546 | */ |
||
| 547 | public function getCompatibleVersions() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Get the date of the last available update |
||
| 555 | * |
||
| 556 | * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise |
||
| 557 | */ |
||
| 558 | public function getLastUpdate() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Get the base path of the extension |
||
| 566 | * |
||
| 567 | * @return string The base path of the extension |
||
| 568 | */ |
||
| 569 | public function getInstallDir() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * The type of extension installation |
||
| 580 | * |
||
| 581 | * @return string One of "none", "manual", "git" or "automatic" |
||
| 582 | */ |
||
| 583 | public function getInstallType() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * If the extension can probably be installed/updated or uninstalled |
||
| 593 | * |
||
| 594 | * @return bool|string True or error string |
||
| 595 | */ |
||
| 596 | public function canModify() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Install an extension from a user upload |
||
| 614 | * |
||
| 615 | * @param string $field name of the upload file |
||
| 616 | * @throws Exception when something goes wrong |
||
| 617 | * @return array The list of installed extensions |
||
| 618 | */ |
||
| 619 | public function installFromUpload($field) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Install an extension from a remote URL |
||
| 651 | * |
||
| 652 | * @param string $url |
||
| 653 | * @throws Exception when something goes wrong |
||
| 654 | * @return array The list of installed extensions |
||
| 655 | */ |
||
| 656 | public function installFromURL($url) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Install or update the extension |
||
| 674 | * |
||
| 675 | * @throws \Exception when something goes wrong |
||
| 676 | * @return array The list of installed extensions |
||
| 677 | */ |
||
| 678 | public function installOrUpdate() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Uninstall the extension |
||
| 697 | * |
||
| 698 | * @return bool If the plugin was sucessfully uninstalled |
||
| 699 | */ |
||
| 700 | public function uninstall() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Enable the extension |
||
| 708 | * |
||
| 709 | * @return bool|string True or an error message |
||
| 710 | */ |
||
| 711 | public function enable() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Disable the extension |
||
| 729 | * |
||
| 730 | * @return bool|string True or an error message |
||
| 731 | */ |
||
| 732 | public function disable() |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Purge the cache by touching the main configuration file |
||
| 750 | */ |
||
| 751 | protected function purgeCache() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Read local extension data either from info.txt or getInfo() |
||
| 762 | */ |
||
| 763 | protected function readLocalData() |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Save the given URL and current datetime in the manager.dat file of all installed extensions |
||
| 805 | * |
||
| 806 | * @param string $url Where the extension was downloaded from. (empty for manual installs via upload) |
||
| 807 | * @param array $installed Optional list of installed plugins |
||
| 808 | */ |
||
| 809 | protected function updateManagerData($url = '', $installed = null) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Read the manager.dat file |
||
| 837 | */ |
||
| 838 | protected function readManagerData() |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Write the manager.data file |
||
| 858 | */ |
||
| 859 | protected function writeManagerData() |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Returns a temporary directory |
||
| 871 | * |
||
| 872 | * The directory is registered for cleanup when the class is destroyed |
||
| 873 | * |
||
| 874 | * @return false|string |
||
| 875 | */ |
||
| 876 | protected function mkTmpDir() |
||
| 883 | |||
| 884 | /** |
||
| 885 | * downloads a file from the net and saves it |
||
| 886 | * |
||
| 887 | * - $file is the directory where the file should be saved |
||
| 888 | * - if successful will return the name used for the saved file, false otherwise |
||
| 889 | * |
||
| 890 | * @author Andreas Gohr <[email protected]> |
||
| 891 | * @author Chris Smith <[email protected]> |
||
| 892 | * |
||
| 893 | * @param string $url url to download |
||
| 894 | * @param string $file path to file or directory where to save |
||
| 895 | * @param string $defaultName fallback for name of download |
||
| 896 | * @return bool|string if failed false, otherwise true or the name of the file in the given dir |
||
| 897 | */ |
||
| 898 | protected function downloadToFile($url,$file,$defaultName=''){ |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Download an archive to a protected path |
||
| 939 | * |
||
| 940 | * @param string $url The url to get the archive from |
||
| 941 | * @throws Exception when something goes wrong |
||
| 942 | * @return string The path where the archive was saved |
||
| 943 | */ |
||
| 944 | public function download($url) |
||
| 972 | |||
| 973 | /** |
||
| 974 | * @param string $file The path to the archive that shall be installed |
||
| 975 | * @param bool $overwrite If an already installed plugin should be overwritten |
||
| 976 | * @param string $base The basename of the plugin if it's known |
||
| 977 | * @throws Exception when something went wrong |
||
| 978 | * @return array list of installed extensions |
||
| 979 | */ |
||
| 980 | public function installArchive($file, $overwrite = false, $base = '') |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Find out what was in the extracted directory |
||
| 1070 | * |
||
| 1071 | * Correct folders are searched recursively using the "*.info.txt" configs |
||
| 1072 | * as indicator for a root folder. When such a file is found, it's base |
||
| 1073 | * setting is used (when set). All folders found by this method are stored |
||
| 1074 | * in the 'new' key of the $result array. |
||
| 1075 | * |
||
| 1076 | * For backwards compatibility all found top level folders are stored as |
||
| 1077 | * in the 'old' key of the $result array. |
||
| 1078 | * |
||
| 1079 | * When no items are found in 'new' the copy mechanism should fall back |
||
| 1080 | * the 'old' list. |
||
| 1081 | * |
||
| 1082 | * @author Andreas Gohr <[email protected]> |
||
| 1083 | * @param array $result - results are stored here |
||
| 1084 | * @param string $directory - the temp directory where the package was unpacked to |
||
| 1085 | * @param string $default_type - type used if no info.txt available |
||
| 1086 | * @param string $subdir - a subdirectory. do not set. used by recursion |
||
| 1087 | * @return bool - false on error |
||
| 1088 | */ |
||
| 1089 | protected function findFolders(&$result, $directory, $default_type = 'plugin', $subdir = '') |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Decompress a given file to the given target directory |
||
| 1154 | * |
||
| 1155 | * Determines the compression type from the file extension |
||
| 1156 | * |
||
| 1157 | * @param string $file archive to extract |
||
| 1158 | * @param string $target directory to extract to |
||
| 1159 | * @throws Exception |
||
| 1160 | * @return bool |
||
| 1161 | */ |
||
| 1162 | private function decompress($file, $target) |
||
| 1193 | |||
| 1194 | /** |
||
| 1195 | * Determine the archive type of the given file |
||
| 1196 | * |
||
| 1197 | * Reads the first magic bytes of the given file for content type guessing, |
||
| 1198 | * if neither bz, gz or zip are recognized, tar is assumed. |
||
| 1199 | * |
||
| 1200 | * @author Andreas Gohr <[email protected]> |
||
| 1201 | * @param string $file The file to analyze |
||
| 1202 | * @return string|false false if the file can't be read, otherwise an "extension" |
||
| 1203 | */ |
||
| 1204 | private function guessArchiveType($file) |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Copy with recursive sub-directory support |
||
| 1219 | * |
||
| 1220 | * @param string $src filename path to file |
||
| 1221 | * @param string $dst filename path to file |
||
| 1222 | * @return bool|int|string |
||
| 1223 | */ |
||
| 1224 | private function dircopy($src, $dst) |
||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * Delete outdated files from updated plugins |
||
| 1253 | * |
||
| 1254 | * @param array $installed |
||
| 1255 | */ |
||
| 1256 | private function removeDeletedfiles($installed) |
||
| 1285 | } |
||
| 1286 | |||
| 1288 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: