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 |
||
| 15 | class helper_plugin_extension_extension extends DokuWiki_Plugin |
||
| 16 | { |
||
| 17 | private $id; |
||
| 18 | private $base; |
||
| 19 | private $is_template = false; |
||
| 20 | private $localInfo; |
||
| 21 | private $remoteInfo; |
||
| 22 | private $managerData; |
||
| 23 | /** @var helper_plugin_extension_repository $repository */ |
||
| 24 | private $repository = null; |
||
| 25 | |||
| 26 | /** @var array list of temporary directories */ |
||
| 27 | private $temporary = array(); |
||
| 28 | |||
| 29 | /** @var string where templates are installed to */ |
||
| 30 | private $tpllib = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * helper_plugin_extension_extension constructor. |
||
| 34 | */ |
||
| 35 | public function __construct() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Destructor |
||
| 42 | * |
||
| 43 | * deletes any dangling temporary directories |
||
| 44 | */ |
||
| 45 | public function __destruct() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return bool false, this component is not a singleton |
||
| 54 | */ |
||
| 55 | public function isSingleton() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set the name of the extension this instance shall represents, triggers loading the local and remote data |
||
| 62 | * |
||
| 63 | * @param string $id The id of the extension (prefixed with template: for templates) |
||
| 64 | * @return bool If some (local or remote) data was found |
||
| 65 | */ |
||
| 66 | public function setExtension($id) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * If the extension is installed locally |
||
| 99 | * |
||
| 100 | * @return bool If the extension is installed locally |
||
| 101 | */ |
||
| 102 | public function isInstalled() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * If the extension is under git control |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isGitControlled() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * If the extension is bundled |
||
| 120 | * |
||
| 121 | * @return bool If the extension is bundled |
||
| 122 | */ |
||
| 123 | public function isBundled() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * If the extension is protected against any modification (disable/uninstall) |
||
| 139 | * |
||
| 140 | * @return bool if the extension is protected |
||
| 141 | */ |
||
| 142 | public function isProtected() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * If the extension is installed in the correct directory |
||
| 156 | * |
||
| 157 | * @return bool If the extension is installed in the correct directory |
||
| 158 | */ |
||
| 159 | public function isInWrongFolder() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * If the extension is enabled |
||
| 166 | * |
||
| 167 | * @return bool If the extension is enabled |
||
| 168 | */ |
||
| 169 | public function isEnabled() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * If the extension should be updated, i.e. if an updated version is available |
||
| 183 | * |
||
| 184 | * @return bool If an update is available |
||
| 185 | */ |
||
| 186 | public function updateAvailable() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * If the extension is a template |
||
| 199 | * |
||
| 200 | * @return bool If this extension is a template |
||
| 201 | */ |
||
| 202 | public function isTemplate() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the ID of the extension |
||
| 209 | * |
||
| 210 | * This is the same as getName() for plugins, for templates it's getName() prefixed with 'template:' |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getID() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the name of the installation directory |
||
| 221 | * |
||
| 222 | * @return string The name of the installation directory |
||
| 223 | */ |
||
| 224 | public function getInstallName() |
||
| 228 | |||
| 229 | // Data from plugin.info.txt/template.info.txt or the repo when not available locally |
||
| 230 | /** |
||
| 231 | * Get the basename of the extension |
||
| 232 | * |
||
| 233 | * @return string The basename |
||
| 234 | */ |
||
| 235 | public function getBase() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the display name of the extension |
||
| 243 | * |
||
| 244 | * @return string The display name |
||
| 245 | */ |
||
| 246 | public function getDisplayName() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the author name of the extension |
||
| 255 | * |
||
| 256 | * @return string|bool The name of the author or false if there is none |
||
| 257 | */ |
||
| 258 | public function getAuthor() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the email of the author of the extension if there is any |
||
| 267 | * |
||
| 268 | * @return string|bool The email address or false if there is none |
||
| 269 | */ |
||
| 270 | public function getEmail() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get the email id, i.e. the md5sum of the email |
||
| 279 | * |
||
| 280 | * @return string|bool The md5sum of the email if there is any, false otherwise |
||
| 281 | */ |
||
| 282 | public function getEmailID() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the description of the extension |
||
| 291 | * |
||
| 292 | * @return string The description |
||
| 293 | */ |
||
| 294 | public function getDescription() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the URL of the extension, usually a page on dokuwiki.org |
||
| 303 | * |
||
| 304 | * @return string The URL |
||
| 305 | */ |
||
| 306 | public function getURL() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get the installed version of the extension |
||
| 315 | * |
||
| 316 | * @return string|bool The version, usually in the form yyyy-mm-dd if there is any |
||
| 317 | */ |
||
| 318 | public function getInstalledVersion() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the install date of the current version |
||
| 327 | * |
||
| 328 | * @return string|bool The date of the last update or false if not available |
||
| 329 | */ |
||
| 330 | public function getUpdateDate() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the date of the installation of the plugin |
||
| 338 | * |
||
| 339 | * @return string|bool The date of the installation or false if not available |
||
| 340 | */ |
||
| 341 | public function getInstallDate() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the names of the dependencies of this extension |
||
| 349 | * |
||
| 350 | * @return array The base names of the dependencies |
||
| 351 | */ |
||
| 352 | public function getDependencies() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Get the names of the missing dependencies |
||
| 360 | * |
||
| 361 | * @return array The base names of the missing dependencies |
||
| 362 | */ |
||
| 363 | public function getMissingDependencies() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the names of all conflicting extensions |
||
| 379 | * |
||
| 380 | * @return array The names of the conflicting extensions |
||
| 381 | */ |
||
| 382 | public function getConflicts() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the names of similar extensions |
||
| 390 | * |
||
| 391 | * @return array The names of similar extensions |
||
| 392 | */ |
||
| 393 | public function getSimilarExtensions() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the names of the tags of the extension |
||
| 401 | * |
||
| 402 | * @return array The names of the tags of the extension |
||
| 403 | */ |
||
| 404 | public function getTags() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the popularity information as floating point number [0,1] |
||
| 412 | * |
||
| 413 | * @return float|bool The popularity information or false if it isn't available |
||
| 414 | */ |
||
| 415 | public function getPopularity() |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the text of the security warning if there is any |
||
| 424 | * |
||
| 425 | * @return string|bool The security warning if there is any, false otherwise |
||
| 426 | */ |
||
| 427 | public function getSecurityWarning() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the text of the security issue if there is any |
||
| 435 | * |
||
| 436 | * @return string|bool The security issue if there is any, false otherwise |
||
| 437 | */ |
||
| 438 | public function getSecurityIssue() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get the URL of the screenshot of the extension if there is any |
||
| 446 | * |
||
| 447 | * @return string|bool The screenshot URL if there is any, false otherwise |
||
| 448 | */ |
||
| 449 | public function getScreenshotURL() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the URL of the thumbnail of the extension if there is any |
||
| 457 | * |
||
| 458 | * @return string|bool The thumbnail URL if there is any, false otherwise |
||
| 459 | */ |
||
| 460 | public function getThumbnailURL() |
||
| 465 | /** |
||
| 466 | * Get the last used download URL of the extension if there is any |
||
| 467 | * |
||
| 468 | * @return string|bool The previously used download URL, false if the extension has been installed manually |
||
| 469 | */ |
||
| 470 | public function getLastDownloadURL() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the download URL of the extension if there is any |
||
| 478 | * |
||
| 479 | * @return string|bool The download URL if there is any, false otherwise |
||
| 480 | */ |
||
| 481 | public function getDownloadURL() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * If the download URL has changed since the last download |
||
| 489 | * |
||
| 490 | * @return bool If the download URL has changed |
||
| 491 | */ |
||
| 492 | public function hasDownloadURLChanged() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the bug tracker URL of the extension if there is any |
||
| 501 | * |
||
| 502 | * @return string|bool The bug tracker URL if there is any, false otherwise |
||
| 503 | */ |
||
| 504 | public function getBugtrackerURL() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the URL of the source repository if there is any |
||
| 512 | * |
||
| 513 | * @return string|bool The URL of the source repository if there is any, false otherwise |
||
| 514 | */ |
||
| 515 | public function getSourcerepoURL() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get the donation URL of the extension if there is any |
||
| 523 | * |
||
| 524 | * @return string|bool The donation URL if there is any, false otherwise |
||
| 525 | */ |
||
| 526 | public function getDonationURL() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get the extension type(s) |
||
| 534 | * |
||
| 535 | * @return array The type(s) as array of strings |
||
| 536 | */ |
||
| 537 | public function getTypes() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Get a list of all DokuWiki versions this extension is compatible with |
||
| 546 | * |
||
| 547 | * @return array The versions in the form yyyy-mm-dd => ('label' => label, 'implicit' => implicit) |
||
| 548 | */ |
||
| 549 | public function getCompatibleVersions() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Get the date of the last available update |
||
| 557 | * |
||
| 558 | * @return string|bool The last available update in the form yyyy-mm-dd if there is any, false otherwise |
||
| 559 | */ |
||
| 560 | public function getLastUpdate() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the base path of the extension |
||
| 568 | * |
||
| 569 | * @return string The base path of the extension |
||
| 570 | */ |
||
| 571 | public function getInstallDir() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * The type of extension installation |
||
| 582 | * |
||
| 583 | * @return string One of "none", "manual", "git" or "automatic" |
||
| 584 | */ |
||
| 585 | public function getInstallType() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * If the extension can probably be installed/updated or uninstalled |
||
| 595 | * |
||
| 596 | * @return bool|string True or error string |
||
| 597 | */ |
||
| 598 | public function canModify() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Install an extension from a user upload |
||
| 616 | * |
||
| 617 | * @param string $field name of the upload file |
||
| 618 | * @param boolean $overwrite overwrite folder if the extension name is the same |
||
| 619 | * @throws Exception when something goes wrong |
||
| 620 | * @return array The list of installed extensions |
||
| 621 | */ |
||
| 622 | public function installFromUpload($field, $overwrite = true) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Install an extension from a remote URL |
||
| 654 | * |
||
| 655 | * @param string $url |
||
| 656 | * @param boolean $overwrite overwrite folder if the extension name is the same |
||
| 657 | * @throws Exception when something goes wrong |
||
| 658 | * @return array The list of installed extensions |
||
| 659 | */ |
||
| 660 | public function installFromURL($url, $overwrite = true) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Install or update the extension |
||
| 678 | * |
||
| 679 | * @throws \Exception when something goes wrong |
||
| 680 | * @return array The list of installed extensions |
||
| 681 | */ |
||
| 682 | public function installOrUpdate() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Uninstall the extension |
||
| 701 | * |
||
| 702 | * @return bool If the plugin was sucessfully uninstalled |
||
| 703 | */ |
||
| 704 | public function uninstall() |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Enable the extension |
||
| 712 | * |
||
| 713 | * @return bool|string True or an error message |
||
| 714 | */ |
||
| 715 | public function enable() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Disable the extension |
||
| 733 | * |
||
| 734 | * @return bool|string True or an error message |
||
| 735 | */ |
||
| 736 | public function disable() |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Purge the cache by touching the main configuration file |
||
| 754 | */ |
||
| 755 | protected function purgeCache() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Read local extension data either from info.txt or getInfo() |
||
| 766 | */ |
||
| 767 | protected function readLocalData() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Save the given URL and current datetime in the manager.dat file of all installed extensions |
||
| 808 | * |
||
| 809 | * @param string $url Where the extension was downloaded from. (empty for manual installs via upload) |
||
| 810 | * @param array $installed Optional list of installed plugins |
||
| 811 | */ |
||
| 812 | protected function updateManagerData($url = '', $installed = null) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Read the manager.dat file |
||
| 840 | */ |
||
| 841 | protected function readManagerData() |
||
| 858 | |||
| 859 | /** |
||
| 860 | * Write the manager.data file |
||
| 861 | */ |
||
| 862 | protected function writeManagerData() |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Returns a temporary directory |
||
| 874 | * |
||
| 875 | * The directory is registered for cleanup when the class is destroyed |
||
| 876 | * |
||
| 877 | * @return false|string |
||
| 878 | */ |
||
| 879 | protected function mkTmpDir() |
||
| 886 | |||
| 887 | /** |
||
| 888 | * downloads a file from the net and saves it |
||
| 889 | * |
||
| 890 | * - $file is the directory where the file should be saved |
||
| 891 | * - if successful will return the name used for the saved file, false otherwise |
||
| 892 | * |
||
| 893 | * @author Andreas Gohr <[email protected]> |
||
| 894 | * @author Chris Smith <[email protected]> |
||
| 895 | * |
||
| 896 | * @param string $url url to download |
||
| 897 | * @param string $file path to file or directory where to save |
||
| 898 | * @param string $defaultName fallback for name of download |
||
| 899 | * @return bool|string if failed false, otherwise true or the name of the file in the given dir |
||
| 900 | */ |
||
| 901 | protected function downloadToFile($url, $file, $defaultName = '') |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Download an archive to a protected path |
||
| 943 | * |
||
| 944 | * @param string $url The url to get the archive from |
||
| 945 | * @throws Exception when something goes wrong |
||
| 946 | * @return string The path where the archive was saved |
||
| 947 | */ |
||
| 948 | public function download($url) |
||
| 978 | |||
| 979 | /** |
||
| 980 | * @param string $file The path to the archive that shall be installed |
||
| 981 | * @param bool $overwrite If an already installed plugin should be overwritten |
||
| 982 | * @param string $base The basename of the plugin if it's known |
||
| 983 | * @throws Exception when something went wrong |
||
| 984 | * @return array list of installed extensions |
||
| 985 | */ |
||
| 986 | public function installArchive($file, $overwrite = false, $base = '') |
||
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Find out what was in the extracted directory |
||
| 1080 | * |
||
| 1081 | * Correct folders are searched recursively using the "*.info.txt" configs |
||
| 1082 | * as indicator for a root folder. When such a file is found, it's base |
||
| 1083 | * setting is used (when set). All folders found by this method are stored |
||
| 1084 | * in the 'new' key of the $result array. |
||
| 1085 | * |
||
| 1086 | * For backwards compatibility all found top level folders are stored as |
||
| 1087 | * in the 'old' key of the $result array. |
||
| 1088 | * |
||
| 1089 | * When no items are found in 'new' the copy mechanism should fall back |
||
| 1090 | * the 'old' list. |
||
| 1091 | * |
||
| 1092 | * @author Andreas Gohr <[email protected]> |
||
| 1093 | * @param array $result - results are stored here |
||
| 1094 | * @param string $directory - the temp directory where the package was unpacked to |
||
| 1095 | * @param string $default_type - type used if no info.txt available |
||
| 1096 | * @param string $subdir - a subdirectory. do not set. used by recursion |
||
| 1097 | * @return bool - false on error |
||
| 1098 | */ |
||
| 1099 | protected function findFolders(&$result, $directory, $default_type = 'plugin', $subdir = '') |
||
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Decompress a given file to the given target directory |
||
| 1164 | * |
||
| 1165 | * Determines the compression type from the file extension |
||
| 1166 | * |
||
| 1167 | * @param string $file archive to extract |
||
| 1168 | * @param string $target directory to extract to |
||
| 1169 | * @throws Exception |
||
| 1170 | * @return bool |
||
| 1171 | */ |
||
| 1172 | private function decompress($file, $target) |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Determine the archive type of the given file |
||
| 1207 | * |
||
| 1208 | * Reads the first magic bytes of the given file for content type guessing, |
||
| 1209 | * if neither bz, gz or zip are recognized, tar is assumed. |
||
| 1210 | * |
||
| 1211 | * @author Andreas Gohr <[email protected]> |
||
| 1212 | * @param string $file The file to analyze |
||
| 1213 | * @return string|false false if the file can't be read, otherwise an "extension" |
||
| 1214 | */ |
||
| 1215 | private function guessArchiveType($file) |
||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * Copy with recursive sub-directory support |
||
| 1230 | * |
||
| 1231 | * @param string $src filename path to file |
||
| 1232 | * @param string $dst filename path to file |
||
| 1233 | * @return bool|int|string |
||
| 1234 | */ |
||
| 1235 | private function dircopy($src, $dst) |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Delete outdated files from updated plugins |
||
| 1264 | * |
||
| 1265 | * @param array $installed |
||
| 1266 | */ |
||
| 1267 | private function removeDeletedfiles($installed) |
||
| 1296 | } |
||
| 1297 | |||
| 1299 |
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: