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 | * Returns the URLs of all script files referenced on the page |
||
| 246 | * @return array Combined list of all script urls used in the page |
||
| 247 | */ |
||
| 248 | public function getScriptUrls() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string javascript or css package path. |
||
| 259 | * @return array tuple($path,$url). |
||
|
|
|||
| 260 | */ |
||
| 261 | protected function getPackagePathUrl($base) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string javascript package source folder path. |
||
| 279 | * @return array tuple($basepath,$subpath). |
||
| 280 | */ |
||
| 281 | protected function getScriptPackageFolder($script) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string css package source folder path. |
||
| 293 | * @return array tuple($basepath,$subpath). |
||
| 294 | */ |
||
| 295 | protected function getStylePackageFolder($script) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns javascript statement that create a new callback request object. |
||
| 307 | * @param ICallbackEventHandler callback response handler |
||
| 308 | * @param array additional callback options |
||
| 309 | * @return string javascript statement that creates a new callback request. |
||
| 310 | */ |
||
| 311 | public function getCallbackReference(ICallbackEventHandler $callbackHandler, $options=null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Registers callback javascript for a control. |
||
| 325 | * @param string javascript class responsible for the control being registered for callback |
||
| 326 | * @param array callback options |
||
| 327 | */ |
||
| 328 | public function registerCallbackControl($class, $options) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Registers postback javascript for a control. A null class parameter will prevent |
||
| 341 | * the javascript code registration. |
||
| 342 | * @param string javascript class responsible for the control being registered for postback |
||
| 343 | * @param array postback options |
||
| 344 | */ |
||
| 345 | public function registerPostBackControl($class,$options) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Register a default button to panel. When the $panel is in focus and |
||
| 364 | * the 'enter' key is pressed, the $button will be clicked. |
||
| 365 | * @param TControl|string panel (or its unique ID) to register the default button action |
||
| 366 | * @param TControl|string button (or its unique ID) to trigger a postback |
||
| 367 | */ |
||
| 368 | public function registerDefaultButton($panel, $button) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string the unique ID of the container control |
||
| 391 | * @param string the unique ID of the button control |
||
| 392 | * @return array default button options. |
||
| 393 | */ |
||
| 394 | protected function getDefaultButtonOptions($panelID, $buttonID) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Registers the control to receive default focus. |
||
| 406 | * @param string the client ID of the control to receive default focus |
||
| 407 | */ |
||
| 408 | public function registerFocusControl($target) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Registers Prado style by library name. See "Web/Javascripts/packages.php" |
||
| 421 | * for library names. |
||
| 422 | * @param string style library name. |
||
| 423 | */ |
||
| 424 | public function registerPradoStyle($name) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Registers a Prado style library to be loaded. |
||
| 433 | */ |
||
| 434 | protected function registerPradoStyleInternal($name) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Registers a CSS file to be rendered in the page head |
||
| 486 | * |
||
| 487 | * The CSS files in themes are registered in {@link OnPreRenderComplete onPreRenderComplete} if you want to override |
||
| 488 | * CSS styles in themes you need to register it after this event is completed. |
||
| 489 | * |
||
| 490 | * Example: |
||
| 491 | * <code> |
||
| 492 | * <?php |
||
| 493 | * class BasePage extends TPage { |
||
| 494 | * public function onPreRenderComplete($param) { |
||
| 495 | * parent::onPreRenderComplete($param); |
||
| 496 | * $url = 'path/to/your/stylesheet.css'; |
||
| 497 | * $this->Page->ClientScript->registerStyleSheetFile($url, $url); |
||
| 498 | * } |
||
| 499 | * } |
||
| 500 | * </code> |
||
| 501 | * |
||
| 502 | * @param string a unique key identifying the file |
||
| 503 | * @param string URL to the CSS file |
||
| 504 | * @param string media type of the CSS (such as 'print', 'screen', etc.). Defaults to empty, meaning the CSS applies to all media types. |
||
| 505 | */ |
||
| 506 | public function registerStyleSheetFile($key,$url,$media='') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Registers a CSS block to be rendered in the page head |
||
| 519 | * @param string a unique key identifying the CSS block |
||
| 520 | * @param string CSS block |
||
| 521 | */ |
||
| 522 | public function registerStyleSheet($key,$css,$media='') |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Returns the URLs of all stylesheet files referenced on the page |
||
| 532 | * @return array List of all stylesheet urls used in the page |
||
| 533 | */ |
||
| 534 | public function getStyleSheetUrls() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns all the stylesheet code snippets referenced on the page |
||
| 553 | * @return array List of all stylesheet snippets used in the page |
||
| 554 | */ |
||
| 555 | public function getStyleSheetCodes() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Registers a javascript file in the page head |
||
| 562 | * @param string a unique key identifying the file |
||
| 563 | * @param string URL to the javascript file |
||
| 564 | */ |
||
| 565 | public function registerHeadScriptFile($key,$url) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Registers a javascript block in the page head. |
||
| 576 | * @param string a unique key identifying the script block |
||
| 577 | * @param string javascript block |
||
| 578 | */ |
||
| 579 | public function registerHeadScript($key,$script) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Registers a javascript file to be rendered within the form |
||
| 590 | * @param string a unique key identifying the file |
||
| 591 | * @param string URL to the javascript file to be rendered |
||
| 592 | */ |
||
| 593 | public function registerScriptFile($key, $url) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Registers a javascript script block at the beginning of the form |
||
| 603 | * @param string a unique key identifying the script block |
||
| 604 | * @param string javascript block |
||
| 605 | */ |
||
| 606 | public function registerBeginScript($key,$script) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Registers a javascript script block at the end of the form |
||
| 617 | * @param string a unique key identifying the script block |
||
| 618 | * @param string javascript block |
||
| 619 | */ |
||
| 620 | public function registerEndScript($key,$script) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Registers a hidden field to be rendered in the form. |
||
| 630 | * @param string a unique key identifying the hidden field |
||
| 631 | * @param string|array hidden field value, if the value is an array, every element |
||
| 632 | * in the array will be rendered as a hidden field value. |
||
| 633 | */ |
||
| 634 | public function registerHiddenField($name,$value) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @param string a unique key |
||
| 644 | * @return boolean whether there is a CSS file registered with the specified key |
||
| 645 | */ |
||
| 646 | public function isStyleSheetFileRegistered($key) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @param string a unique key |
||
| 653 | * @return boolean whether there is a CSS block registered with the specified key |
||
| 654 | */ |
||
| 655 | public function isStyleSheetRegistered($key) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @param string a unique key |
||
| 662 | * @return boolean whether there is a head javascript file registered with the specified key |
||
| 663 | */ |
||
| 664 | public function isHeadScriptFileRegistered($key) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param string a unique key |
||
| 671 | * @return boolean whether there is a head javascript block registered with the specified key |
||
| 672 | */ |
||
| 673 | public function isHeadScriptRegistered($key) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @param string a unique key |
||
| 680 | * @return boolean whether there is a javascript file registered with the specified key |
||
| 681 | */ |
||
| 682 | public function isScriptFileRegistered($key) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param string a unique key |
||
| 689 | * @return boolean whether there is a beginning javascript block registered with the specified key |
||
| 690 | */ |
||
| 691 | public function isBeginScriptRegistered($key) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param string a unique key |
||
| 698 | * @return boolean whether there is an ending javascript block registered with the specified key |
||
| 699 | */ |
||
| 700 | public function isEndScriptRegistered($key) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @return boolean true if any end scripts are registered. |
||
| 707 | */ |
||
| 708 | public function hasEndScripts() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @return boolean true if any begin scripts are registered. |
||
| 715 | */ |
||
| 716 | public function hasBeginScripts() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @param string a unique key |
||
| 723 | * @return boolean whether there is a hidden field registered with the specified key |
||
| 724 | */ |
||
| 725 | public function isHiddenFieldRegistered($key) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @param THtmlWriter writer for the rendering purpose |
||
| 732 | */ |
||
| 733 | public function renderStyleSheetFiles($writer) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @param THtmlWriter writer for the rendering purpose |
||
| 748 | */ |
||
| 749 | public function renderStyleSheets($writer) |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @param THtmlWriter writer for the rendering purpose |
||
| 757 | */ |
||
| 758 | public function renderHeadScriptFiles($writer) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param THtmlWriter writer for the rendering purpose |
||
| 765 | */ |
||
| 766 | public function renderHeadScripts($writer) |
||
| 770 | |||
| 771 | public function renderScriptFilesBegin($writer) |
||
| 775 | |||
| 776 | public function renderScriptFilesEnd($writer) |
||
| 780 | |||
| 781 | public function markScriptFileAsRendered($url) |
||
| 787 | |||
| 788 | protected function renderScriptFiles($writer, Array $scripts) |
||
| 796 | |||
| 797 | protected function getRenderedScriptFiles() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @param THtmlWriter writer for the rendering purpose |
||
| 804 | */ |
||
| 805 | public function renderAllPendingScriptFiles($writer) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @param THtmlWriter writer for the rendering purpose |
||
| 816 | */ |
||
| 817 | public function renderBeginScripts($writer) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @param THtmlWriter writer for the rendering purpose |
||
| 824 | */ |
||
| 825 | public function renderEndScripts($writer) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param THtmlWriter writer for the rendering purpose |
||
| 832 | */ |
||
| 833 | public function renderBeginScriptsCallback($writer) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @param THtmlWriter writer for the rendering purpose |
||
| 840 | */ |
||
| 841 | public function renderEndScriptsCallback($writer) |
||
| 845 | |||
| 846 | public function renderHiddenFieldsBegin($writer) |
||
| 850 | |||
| 851 | public function renderHiddenFieldsEnd($writer) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Flushes all pending script registrations |
||
| 858 | * @param THtmlWriter writer for the rendering purpose |
||
| 859 | * @param TControl the control forcing the flush (used only in error messages) |
||
| 860 | */ |
||
| 861 | public function flushScriptFiles($writer, $control=null) |
||
| 869 | |||
| 870 | /** |
||
| 871 | * @param THtmlWriter writer for the rendering purpose |
||
| 872 | */ |
||
| 873 | protected function renderHiddenFieldsInt($writer, $initial) |
||
| 895 | |||
| 896 | public function getHiddenFields() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Checks whether page rendering has not begun yet |
||
| 903 | */ |
||
| 904 | protected function checkIfNotInRender() |
||
| 909 | } |
||
| 910 |
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.