Complex classes like TClientScriptManager 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 TClientScriptManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class TClientScriptManager extends \Prado\TApplicationComponent |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * file containing javascript packages and their cross dependencies |
||
| 36 | */ |
||
| 37 | const PACKAGES_FILE='Web/Javascripts/packages.php'; |
||
| 38 | /** |
||
| 39 | * file containing css packages and their cross dependencies |
||
| 40 | */ |
||
| 41 | const CSS_PACKAGES_FILE='Web/Javascripts/css-packages.php'; |
||
| 42 | /** |
||
| 43 | * @var TPage page who owns this manager |
||
| 44 | */ |
||
| 45 | private $_page; |
||
| 46 | /** |
||
| 47 | * @var array registered hidden fields, indexed by hidden field names |
||
| 48 | */ |
||
| 49 | private $_hiddenFields=array(); |
||
| 50 | /** |
||
| 51 | * @var array javascript blocks to be rendered at the beginning of the form |
||
| 52 | */ |
||
| 53 | private $_beginScripts=array(); |
||
| 54 | /** |
||
| 55 | * @var array javascript blocks to be rendered at the end of the form |
||
| 56 | */ |
||
| 57 | private $_endScripts=array(); |
||
| 58 | /** |
||
| 59 | * @var array javascript files to be rendered in the form |
||
| 60 | */ |
||
| 61 | private $_scriptFiles=array(); |
||
| 62 | /** |
||
| 63 | * @var array javascript files to be rendered in page head section |
||
| 64 | */ |
||
| 65 | private $_headScriptFiles=array(); |
||
| 66 | /** |
||
| 67 | * @var array javascript blocks to be rendered in page head section |
||
| 68 | */ |
||
| 69 | private $_headScripts=array(); |
||
| 70 | /** |
||
| 71 | * @var array CSS files |
||
| 72 | */ |
||
| 73 | private $_styleSheetFiles=array(); |
||
| 74 | /** |
||
| 75 | * @var array CSS declarations |
||
| 76 | */ |
||
| 77 | private $_styleSheets=array(); |
||
| 78 | /** |
||
| 79 | * @var array registered PRADO script libraries |
||
| 80 | */ |
||
| 81 | private $_registeredScripts=array(); |
||
| 82 | /** |
||
| 83 | * Client-side javascript library dependencies, loads from PACKAGES_FILE; |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private static $_scripts; |
||
| 87 | /** |
||
| 88 | * Client-side javascript library packages, loads from PACKAGES_FILE; |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private static $_scriptsPackages; |
||
| 92 | /** |
||
| 93 | * Client-side javascript library source folders, loads from PACKAGES_FILE; |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private static $_scriptsFolders; |
||
| 97 | /** |
||
| 98 | * @var array registered PRADO style libraries |
||
| 99 | */ |
||
| 100 | private $_registeredStyles=array(); |
||
| 101 | /** |
||
| 102 | * Client-side style library dependencies, loads from CSS_PACKAGES_FILE; |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private static $_styles; |
||
| 106 | /** |
||
| 107 | * Client-side style library packages, loads from CSS_PACKAGES_FILE; |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private static $_stylesPackages; |
||
| 111 | /** |
||
| 112 | * Client-side style library folders, loads from CSS_PACKAGES_FILE; |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private static $_stylesFolders; |
||
| 116 | |||
| 117 | private $_renderedHiddenFields; |
||
| 118 | |||
| 119 | private $_renderedScriptFiles=array(); |
||
| 120 | |||
| 121 | private $_expandedScripts; |
||
| 122 | private $_expandedStyles; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Constructor. |
||
| 126 | * @param TPage page that owns this client script manager |
||
| 127 | */ |
||
| 128 | public function __construct(TPage $owner) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return boolean whether THead is required in order to render CSS and js within head |
||
| 135 | * @since 3.1.1 |
||
| 136 | */ |
||
| 137 | public function getRequiresHead() |
||
| 142 | |||
| 143 | public static function getPradoPackages() |
||
| 147 | |||
| 148 | public static function getPradoScripts() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Registers Prado javascript by library name. See "Web/Javascripts/packages.php" |
||
| 155 | * for library names. |
||
| 156 | * @param string script library name. |
||
| 157 | */ |
||
| 158 | public function registerPradoScript($name) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Registers a Prado javascript library to be loaded. |
||
| 167 | */ |
||
| 168 | protected function registerPradoScriptInternal($name) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string Prado javascript library base asset url. |
||
| 233 | */ |
||
| 234 | public function getPradoScriptAssetUrl($script='prado') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return string Prado javascript library base asset path in local filesystem. |
||
| 246 | */ |
||
| 247 | public function getPradoScriptAssetPath($script='prado') |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns the URLs of all script files referenced on the page |
||
| 259 | * @return array Combined list of all script urls used in the page |
||
| 260 | */ |
||
| 261 | public function getScriptUrls() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string javascript or css package path. |
||
| 272 | * @return array tuple($path,$url). |
||
|
|
|||
| 273 | */ |
||
| 274 | protected function getPackagePathUrl($base) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string javascript package source folder path. |
||
| 292 | * @return array tuple($basepath,$subpath). |
||
| 293 | */ |
||
| 294 | protected function getScriptPackageFolder($script) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string css package source folder path. |
||
| 306 | * @return array tuple($basepath,$subpath). |
||
| 307 | */ |
||
| 308 | protected function getStylePackageFolder($script) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns javascript statement that create a new callback request object. |
||
| 320 | * @param ICallbackEventHandler callback response handler |
||
| 321 | * @param array additional callback options |
||
| 322 | * @return string javascript statement that creates a new callback request. |
||
| 323 | */ |
||
| 324 | public function getCallbackReference(ICallbackEventHandler $callbackHandler, $options=null) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Registers callback javascript for a control. |
||
| 338 | * @param string javascript class responsible for the control being registered for callback |
||
| 339 | * @param array callback options |
||
| 340 | */ |
||
| 341 | public function registerCallbackControl($class, $options) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Registers postback javascript for a control. A null class parameter will prevent |
||
| 354 | * the javascript code registration. |
||
| 355 | * @param string javascript class responsible for the control being registered for postback |
||
| 356 | * @param array postback options |
||
| 357 | */ |
||
| 358 | public function registerPostBackControl($class,$options) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Register a default button to panel. When the $panel is in focus and |
||
| 377 | * the 'enter' key is pressed, the $button will be clicked. |
||
| 378 | * @param TControl|string panel (or its unique ID) to register the default button action |
||
| 379 | * @param TControl|string button (or its unique ID) to trigger a postback |
||
| 380 | */ |
||
| 381 | public function registerDefaultButton($panel, $button) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param string the unique ID of the container control |
||
| 404 | * @param string the unique ID of the button control |
||
| 405 | * @return array default button options. |
||
| 406 | */ |
||
| 407 | protected function getDefaultButtonOptions($panelID, $buttonID) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Registers the control to receive default focus. |
||
| 419 | * @param string the client ID of the control to receive default focus |
||
| 420 | */ |
||
| 421 | public function registerFocusControl($target) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Registers Prado style by library name. See "Web/Javascripts/packages.php" |
||
| 434 | * for library names. |
||
| 435 | * @param string style library name. |
||
| 436 | */ |
||
| 437 | public function registerPradoStyle($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Registers a Prado style library to be loaded. |
||
| 446 | */ |
||
| 447 | protected function registerPradoStyleInternal($name) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Registers a CSS file to be rendered in the page head |
||
| 499 | * |
||
| 500 | * The CSS files in themes are registered in {@link OnPreRenderComplete onPreRenderComplete} if you want to override |
||
| 501 | * CSS styles in themes you need to register it after this event is completed. |
||
| 502 | * |
||
| 503 | * Example: |
||
| 504 | * <code> |
||
| 505 | * <?php |
||
| 506 | * class BasePage extends TPage { |
||
| 507 | * public function onPreRenderComplete($param) { |
||
| 508 | * parent::onPreRenderComplete($param); |
||
| 509 | * $url = 'path/to/your/stylesheet.css'; |
||
| 510 | * $this->Page->ClientScript->registerStyleSheetFile($url, $url); |
||
| 511 | * } |
||
| 512 | * } |
||
| 513 | * </code> |
||
| 514 | * |
||
| 515 | * @param string a unique key identifying the file |
||
| 516 | * @param string URL to the CSS file |
||
| 517 | * @param string media type of the CSS (such as 'print', 'screen', etc.). Defaults to empty, meaning the CSS applies to all media types. |
||
| 518 | */ |
||
| 519 | public function registerStyleSheetFile($key,$url,$media='') |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Registers a CSS block to be rendered in the page head |
||
| 532 | * @param string a unique key identifying the CSS block |
||
| 533 | * @param string CSS block |
||
| 534 | */ |
||
| 535 | public function registerStyleSheet($key,$css,$media='') |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Returns the URLs of all stylesheet files referenced on the page |
||
| 545 | * @return array List of all stylesheet urls used in the page |
||
| 546 | */ |
||
| 547 | public function getStyleSheetUrls() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Returns all the stylesheet code snippets referenced on the page |
||
| 566 | * @return array List of all stylesheet snippets used in the page |
||
| 567 | */ |
||
| 568 | public function getStyleSheetCodes() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Registers a javascript file in the page head |
||
| 575 | * @param string a unique key identifying the file |
||
| 576 | * @param string URL to the javascript file |
||
| 577 | */ |
||
| 578 | public function registerHeadScriptFile($key,$url) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Registers a javascript block in the page head. |
||
| 589 | * @param string a unique key identifying the script block |
||
| 590 | * @param string javascript block |
||
| 591 | */ |
||
| 592 | public function registerHeadScript($key,$script) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Registers a javascript file to be rendered within the form |
||
| 603 | * @param string a unique key identifying the file |
||
| 604 | * @param string URL to the javascript file to be rendered |
||
| 605 | */ |
||
| 606 | public function registerScriptFile($key, $url) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Registers a javascript script block at the beginning of the form |
||
| 616 | * @param string a unique key identifying the script block |
||
| 617 | * @param string javascript block |
||
| 618 | */ |
||
| 619 | public function registerBeginScript($key,$script) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Registers a javascript script block at the end of the form |
||
| 630 | * @param string a unique key identifying the script block |
||
| 631 | * @param string javascript block |
||
| 632 | */ |
||
| 633 | public function registerEndScript($key,$script) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Registers a hidden field to be rendered in the form. |
||
| 643 | * @param string a unique key identifying the hidden field |
||
| 644 | * @param string|array hidden field value, if the value is an array, every element |
||
| 645 | * in the array will be rendered as a hidden field value. |
||
| 646 | */ |
||
| 647 | public function registerHiddenField($name,$value) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param string a unique key |
||
| 657 | * @return boolean whether there is a CSS file registered with the specified key |
||
| 658 | */ |
||
| 659 | public function isStyleSheetFileRegistered($key) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param string a unique key |
||
| 666 | * @return boolean whether there is a CSS block registered with the specified key |
||
| 667 | */ |
||
| 668 | public function isStyleSheetRegistered($key) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param string a unique key |
||
| 675 | * @return boolean whether there is a head javascript file registered with the specified key |
||
| 676 | */ |
||
| 677 | public function isHeadScriptFileRegistered($key) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param string a unique key |
||
| 684 | * @return boolean whether there is a head javascript block registered with the specified key |
||
| 685 | */ |
||
| 686 | public function isHeadScriptRegistered($key) |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @param string a unique key |
||
| 693 | * @return boolean whether there is a javascript file registered with the specified key |
||
| 694 | */ |
||
| 695 | public function isScriptFileRegistered($key) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param string a unique key |
||
| 702 | * @return boolean whether there is a beginning javascript block registered with the specified key |
||
| 703 | */ |
||
| 704 | public function isBeginScriptRegistered($key) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @param string a unique key |
||
| 711 | * @return boolean whether there is an ending javascript block registered with the specified key |
||
| 712 | */ |
||
| 713 | public function isEndScriptRegistered($key) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @return boolean true if any end scripts are registered. |
||
| 720 | */ |
||
| 721 | public function hasEndScripts() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return boolean true if any begin scripts are registered. |
||
| 728 | */ |
||
| 729 | public function hasBeginScripts() |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param string a unique key |
||
| 736 | * @return boolean whether there is a hidden field registered with the specified key |
||
| 737 | */ |
||
| 738 | public function isHiddenFieldRegistered($key) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @param THtmlWriter writer for the rendering purpose |
||
| 745 | */ |
||
| 746 | public function renderStyleSheetFiles($writer) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @param THtmlWriter writer for the rendering purpose |
||
| 761 | */ |
||
| 762 | public function renderStyleSheets($writer) |
||
| 767 | |||
| 768 | /** |
||
| 769 | * @param THtmlWriter writer for the rendering purpose |
||
| 770 | */ |
||
| 771 | public function renderHeadScriptFiles($writer) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @param THtmlWriter writer for the rendering purpose |
||
| 778 | */ |
||
| 779 | public function renderHeadScripts($writer) |
||
| 783 | |||
| 784 | public function renderScriptFilesBegin($writer) |
||
| 788 | |||
| 789 | public function renderScriptFilesEnd($writer) |
||
| 793 | |||
| 794 | public function markScriptFileAsRendered($url) |
||
| 800 | |||
| 801 | protected function renderScriptFiles($writer, Array $scripts) |
||
| 809 | |||
| 810 | protected function getRenderedScriptFiles() |
||
| 814 | |||
| 815 | /** |
||
| 816 | * @param THtmlWriter writer for the rendering purpose |
||
| 817 | */ |
||
| 818 | public function renderAllPendingScriptFiles($writer) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @param THtmlWriter writer for the rendering purpose |
||
| 829 | */ |
||
| 830 | public function renderBeginScripts($writer) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @param THtmlWriter writer for the rendering purpose |
||
| 837 | */ |
||
| 838 | public function renderEndScripts($writer) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * @param THtmlWriter writer for the rendering purpose |
||
| 845 | */ |
||
| 846 | public function renderBeginScriptsCallback($writer) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * @param THtmlWriter writer for the rendering purpose |
||
| 853 | */ |
||
| 854 | public function renderEndScriptsCallback($writer) |
||
| 858 | |||
| 859 | public function renderHiddenFieldsBegin($writer) |
||
| 863 | |||
| 864 | public function renderHiddenFieldsEnd($writer) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Flushes all pending script registrations |
||
| 871 | * @param THtmlWriter writer for the rendering purpose |
||
| 872 | * @param TControl the control forcing the flush (used only in error messages) |
||
| 873 | */ |
||
| 874 | public function flushScriptFiles($writer, $control=null) |
||
| 882 | |||
| 883 | /** |
||
| 884 | * @param THtmlWriter writer for the rendering purpose |
||
| 885 | */ |
||
| 886 | protected function renderHiddenFieldsInt($writer, $initial) |
||
| 908 | |||
| 909 | public function getHiddenFields() |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Checks whether page rendering has not begun yet |
||
| 916 | */ |
||
| 917 | protected function checkIfNotInRender() |
||
| 922 | } |
||
| 923 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.