Complex classes like Symphony 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 Symphony, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class Symphony implements Singleton |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * An instance of the Cookies class |
||
| 18 | * |
||
| 19 | * @var Cookies |
||
| 20 | */ |
||
| 21 | public static $Cookies = null; |
||
| 22 | /** |
||
| 23 | * An instance of the Session class |
||
| 24 | * |
||
| 25 | * @var Session |
||
| 26 | */ |
||
| 27 | public static $Session = null; |
||
| 28 | /** |
||
| 29 | * An instance of the SessionFlash class |
||
| 30 | * |
||
| 31 | * @var Session |
||
| 32 | */ |
||
| 33 | public static $Flash = null; |
||
| 34 | /** |
||
| 35 | * An instance of the currently logged in Author |
||
| 36 | * |
||
| 37 | * @var Author |
||
| 38 | */ |
||
| 39 | public static $Author = null; |
||
| 40 | /** |
||
| 41 | * An instance of the Symphony class, either `Administration` or `Frontend`. |
||
| 42 | * |
||
| 43 | * @var Symphony |
||
| 44 | */ |
||
| 45 | protected static $_instance = null; |
||
| 46 | /** |
||
| 47 | * An instance of the Profiler class |
||
| 48 | * |
||
| 49 | * @var Profiler |
||
| 50 | */ |
||
| 51 | protected static $Profiler = null; |
||
| 52 | /** |
||
| 53 | * An instance of the `Configuration` class |
||
| 54 | * |
||
| 55 | * @var Configuration |
||
| 56 | */ |
||
| 57 | private static $Configuration = null; |
||
| 58 | /** |
||
| 59 | * An instance of the `Database` class |
||
| 60 | * |
||
| 61 | * @var MySQL |
||
| 62 | */ |
||
| 63 | private static $Database = null; |
||
| 64 | /** |
||
| 65 | * An instance of the `ExtensionManager` class |
||
| 66 | * |
||
| 67 | * @var ExtensionManager |
||
| 68 | */ |
||
| 69 | private static $ExtensionManager = null; |
||
| 70 | /** |
||
| 71 | * An instance of the `Log` class |
||
| 72 | * |
||
| 73 | * @var Log |
||
| 74 | */ |
||
| 75 | private static $Log = null; |
||
| 76 | /** |
||
| 77 | * The current page namespace, used for translations |
||
| 78 | * |
||
| 79 | * @since Symphony 2.3 |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private static $namespace = false; |
||
| 83 | /** |
||
| 84 | * A previous exception that has been fired. Defaults to null. |
||
| 85 | * |
||
| 86 | * @since Symphony 2.3.2 |
||
| 87 | * @var Exception |
||
| 88 | */ |
||
| 89 | private static $exception = null; |
||
| 90 | /** |
||
| 91 | * The version of the available Symphony upgrade, if available. |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | private static $migrationVersion = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The Symphony constructor initialises the class variables of Symphony. At present |
||
| 98 | * constructor has a couple of responsibilities: |
||
| 99 | * - Start a profiler instance |
||
| 100 | * - If magic quotes are enabled, clean `$_SERVER`, `$_COOKIE`, `$_GET` and `$_POST` arrays |
||
| 101 | * - Initialise the correct Language for the currently logged in Author. |
||
| 102 | * - Start the session and adjust the error handling if the user is logged in |
||
| 103 | */ |
||
| 104 | protected function __construct() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Setter for `$Log`. This function uses the configuration |
||
| 135 | * settings in the 'log' group in the Configuration to create an instance. Date |
||
| 136 | * formatting options are also retrieved from the configuration. |
||
| 137 | * |
||
| 138 | * @param string $filename (optional) |
||
| 139 | * The file to write the log to, if omitted this will default to `ACTIVITY_LOG` |
||
| 140 | * @throws Exception |
||
| 141 | * @return bool|void |
||
| 142 | */ |
||
| 143 | public static function initialiseLog($filename = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Accessor for the current `Configuration` instance. This contains |
||
| 190 | * representation of the the Symphony config file. |
||
| 191 | * |
||
| 192 | * @return Configuration |
||
| 193 | */ |
||
| 194 | public static function Configuration() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Accessor for the current `Log` instance |
||
| 201 | * |
||
| 202 | * @since Symphony 2.3 |
||
| 203 | * @return Log |
||
| 204 | */ |
||
| 205 | public static function Log() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * This will initialise the Database class and attempt to create a connection |
||
| 212 | * using the connection details provided in the Symphony configuration. If any |
||
| 213 | * errors occur whilst doing so, a Symphony Error Page is displayed. |
||
| 214 | * |
||
| 215 | * @throws SymphonyErrorPage |
||
| 216 | * @return boolean |
||
| 217 | * This function will return true if the `$Database` was |
||
| 218 | * initialised successfully. |
||
| 219 | */ |
||
| 220 | public static function initialiseDatabase() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Setter for `$Database`, accepts a Database object. If `$database` |
||
| 272 | * is omitted, this function will set `$Database` to be of the `MySQL` |
||
| 273 | * class. |
||
| 274 | * |
||
| 275 | * @since Symphony 2.3 |
||
| 276 | * @param stdClass $database (optional) |
||
| 277 | * The class to handle all Database operations, if omitted this function |
||
| 278 | * will set `self::$Database` to be an instance of the `MySQL` class. |
||
| 279 | * @return boolean |
||
| 280 | * This function will always return true |
||
| 281 | */ |
||
| 282 | public static function setDatabase(stdClass $database = null) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Accessor for the current `$Database` instance. |
||
| 295 | * |
||
| 296 | * @return MySQL |
||
| 297 | */ |
||
| 298 | public static function Database() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * A wrapper for throwing a new Symphony Error page. |
||
| 305 | * |
||
| 306 | * This methods sets the `GenericExceptionHandler::$enabled` value to `true`. |
||
| 307 | * |
||
| 308 | * @see core.SymphonyErrorPage |
||
| 309 | * @param string|XMLElement $message |
||
| 310 | * A description for this error, which can be provided as a string |
||
| 311 | * or as an XMLElement. |
||
| 312 | * @param string $heading |
||
| 313 | * A heading for the error page |
||
| 314 | * @param integer $status |
||
| 315 | * Properly sets the HTTP status code for the response. Defaults to |
||
| 316 | * `Page::HTTP_STATUS_ERROR`. Use `Page::HTTP_STATUS_XXX` to set this value. |
||
| 317 | * @param string $template |
||
| 318 | * A string for the error page template to use, defaults to 'generic'. This |
||
| 319 | * can be the name of any template file in the `TEMPLATES` directory. |
||
| 320 | * A template using the naming convention of `tpl.*.php`. |
||
| 321 | * @param array $additional |
||
| 322 | * Allows custom information to be passed to the Symphony Error Page |
||
| 323 | * that the template may want to expose, such as custom Headers etc. |
||
| 324 | * @throws SymphonyErrorPage |
||
| 325 | */ |
||
| 326 | public static function throwCustomError( |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Setter for `$ExtensionManager` using the current |
||
| 339 | * Symphony instance as the parent. If for some reason this fails, |
||
| 340 | * a Symphony Error page will be thrown |
||
| 341 | * |
||
| 342 | * @param boolean $force (optional) |
||
| 343 | * When set to true, this function will always create a new |
||
| 344 | * instance of ExtensionManager, replacing self::$ExtensionManager. |
||
| 345 | * @return void |
||
| 346 | */ |
||
| 347 | public static function initialiseExtensionManager($force = false) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Setter for `$Session`. This will use PHP's parse_url |
||
| 362 | * function on the current URL to set a session using the `session_name` |
||
| 363 | * defined in the Symphony configuration. The is either admin or public. |
||
| 364 | * The session will last for the time defined in configuration. |
||
| 365 | * |
||
| 366 | * @since Symphony 3.0.0 |
||
| 367 | */ |
||
| 368 | public function initialiseSessionAndCookies() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets the configuerd session timeout as seconds, based on the environment instance |
||
| 424 | * |
||
| 425 | * @return int |
||
| 426 | * The seconds |
||
| 427 | */ |
||
| 428 | private function getSessionTimeout() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Accessor for the current `$Session` instance. |
||
| 447 | * |
||
| 448 | * @since 3.0.0 |
||
| 449 | * @return Session |
||
| 450 | */ |
||
| 451 | public static function Session() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Accessor for the current `$Cookies` instance. |
||
| 458 | * |
||
| 459 | * @since 2.0.0 |
||
| 460 | * @return Cookies |
||
| 461 | */ |
||
| 462 | public static function Cookies() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * This function determines whether an there is a currently logged in |
||
| 469 | * Author for Symphony by using the `$Session`'s username |
||
| 470 | * and password. If an Author is found, they will be logged in, otherwise |
||
| 471 | * the `$Session` will be destroyed. |
||
| 472 | * |
||
| 473 | * @see login() |
||
| 474 | * @return boolean |
||
| 475 | */ |
||
| 476 | public static function isLoggedIn() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Attempts to log an Author in given a username and password. |
||
| 489 | * If the password is not hashed, it will be hashed using the sha1 |
||
| 490 | * algorithm. The username and password will be sanitized before |
||
| 491 | * being used to query the Database. If an Author is found, they |
||
| 492 | * will be logged in and the sanitized username and password (also hashed) |
||
| 493 | * will be saved as values in the `$Session`. |
||
| 494 | * |
||
| 495 | * @see toolkit.Cryptography#hash() |
||
| 496 | * @throws DatabaseException |
||
| 497 | * @param string $username |
||
| 498 | * The Author's username. This will be sanitized before use. |
||
| 499 | * @param string $password |
||
| 500 | * The Author's password. This will be sanitized and then hashed before use |
||
| 501 | * @param boolean $isHash |
||
| 502 | * If the password provided is already hashed, setting this parameter to |
||
| 503 | * true will stop it becoming rehashed. By default it is false. |
||
| 504 | * @return boolean |
||
| 505 | * True if the Author was logged in, false otherwise |
||
| 506 | */ |
||
| 507 | public static function login($username, $password, $isHash = false) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Checks if an update is available and applicable for the current installation. |
||
| 555 | * |
||
| 556 | * @since Symphony 2.3.1 |
||
| 557 | * @return boolean |
||
| 558 | */ |
||
| 559 | public static function isUpgradeAvailable() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Checks if the installer/upgrader is available. |
||
| 573 | * |
||
| 574 | * @since Symphony 2.3.1 |
||
| 575 | * @return boolean |
||
| 576 | */ |
||
| 577 | public static function isInstallerAvailable() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns the most recent version found in the `/install/migrations` folder. |
||
| 584 | * Returns a version string to be used in `version_compare()` if an updater |
||
| 585 | * has been found. Returns `FALSE` otherwise. |
||
| 586 | * |
||
| 587 | * @since Symphony 2.3.1 |
||
| 588 | * @return string|boolean |
||
| 589 | */ |
||
| 590 | public static function getMigrationVersion() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Setter for the Symphony Log and Error Handling system |
||
| 609 | * |
||
| 610 | * @since Symphony 2.6.0 |
||
| 611 | */ |
||
| 612 | public static function initialiseErrorHandler() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Accessor for the Symphony instance, whether it be Frontend |
||
| 622 | * or Administration |
||
| 623 | * |
||
| 624 | * @since Symphony 2.2 |
||
| 625 | * @throws Exception |
||
| 626 | * @return Symphony |
||
| 627 | */ |
||
| 628 | public static function Engine() |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Setter for `$Configuration`. This function initialise the configuration |
||
| 641 | * object and populate its properties based on the given `$array`. Since |
||
| 642 | * Symphony 2.6.5, it will also set Symphony's date constants. |
||
| 643 | * |
||
| 644 | * @since Symphony 2.3 |
||
| 645 | * @param array $data |
||
| 646 | * An array of settings to be stored into the Configuration object |
||
| 647 | */ |
||
| 648 | public static function initialiseConfiguration(array $data = array()) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Is XSRF enabled for this Symphony install? |
||
| 671 | * |
||
| 672 | * @since Symphony 2.4 |
||
| 673 | * @return boolean |
||
| 674 | */ |
||
| 675 | public static function isXSRFEnabled() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Accessor for the current `Profiler` instance. |
||
| 682 | * |
||
| 683 | * @since Symphony 2.3 |
||
| 684 | * @return Profiler |
||
| 685 | */ |
||
| 686 | public static function Profiler() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Accessor for the current `$Flash` instance. |
||
| 693 | * |
||
| 694 | * @since 3.0.0 |
||
| 695 | * @return SessionFlash |
||
| 696 | */ |
||
| 697 | public static function Flash() |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Accessor for the current `$ExtensionManager` instance. |
||
| 704 | * |
||
| 705 | * @since Symphony 2.2 |
||
| 706 | * @return ExtensionManager |
||
| 707 | */ |
||
| 708 | public static function ExtensionManager() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Accessor for the current `$Author` instance. |
||
| 715 | * |
||
| 716 | * @since Symphony 2.5.0 |
||
| 717 | * @return Author |
||
| 718 | */ |
||
| 719 | public static function Author() |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Symphony allows Authors to login via the use of tokens instead of |
||
| 726 | * a username and password. A token is derived from concatenating the |
||
| 727 | * Author's username and password and applying the sha1 hash to |
||
| 728 | * it, from this, a portion of the hash is used as the token. This is a useful |
||
| 729 | * feature often used when setting up other Authors accounts or if an |
||
| 730 | * Author forgets their password. |
||
| 731 | * |
||
| 732 | * @param string $token |
||
| 733 | * The Author token, which is a portion of the hashed string concatenation |
||
| 734 | * of the Author's username and password |
||
| 735 | * @throws DatabaseException |
||
| 736 | * @return boolean |
||
| 737 | * True if the Author is logged in, false otherwise |
||
| 738 | */ |
||
| 739 | public static function loginFromToken($token) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * This function will destroy the currently logged in `$Author` |
||
| 793 | * session, essentially logging them out. |
||
| 794 | * |
||
| 795 | * @see core.Session#expire() |
||
| 796 | */ |
||
| 797 | public static function logout() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Accessor for `self::$exception`. |
||
| 804 | * |
||
| 805 | * @since Symphony 2.3.2 |
||
| 806 | * @return Exception|null |
||
| 807 | */ |
||
| 808 | public static function getException() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Setter accepts a previous Exception. Useful for determining the context |
||
| 815 | * of a current exception (ie. detecting recursion). |
||
| 816 | * |
||
| 817 | * @since Symphony 2.3.2 |
||
| 818 | * @param Exception $ex |
||
| 819 | */ |
||
| 820 | public static function setException(Exception $ex) |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Returns the page namespace based on the current URL. |
||
| 827 | * A few examples: |
||
| 828 | * |
||
| 829 | * /login |
||
| 830 | * /publish |
||
| 831 | * /blueprints/datasources |
||
| 832 | * [...] |
||
| 833 | * /extension/$extension_name/$page_name |
||
| 834 | * |
||
| 835 | * This method is especially useful in couple with the translation function. |
||
| 836 | * |
||
| 837 | * @see toolkit#__() |
||
| 838 | * @return string |
||
| 839 | * The page namespace, without any action string (e.g. "new", "saved") or |
||
| 840 | * any value that depends upon the single setup (e.g. the section handle in |
||
| 841 | * /publish/$handle) |
||
| 842 | */ |
||
| 843 | public static function getPageNamespace() |
||
| 873 | } |
||
| 874 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: