Complex classes like SSViewer 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 SSViewer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class SSViewer implements Flushable |
||
| 41 | { |
||
| 42 | use Configurable; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Identifier for the default theme |
||
| 46 | */ |
||
| 47 | const DEFAULT_THEME = '$default'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @config |
||
| 51 | * @var boolean $source_file_comments |
||
| 52 | */ |
||
| 53 | private static $source_file_comments = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ignore |
||
| 57 | */ |
||
| 58 | private static $template_cache_flushed = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @ignore |
||
| 62 | */ |
||
| 63 | private static $cacheblock_cache_flushed = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set whether HTML comments indicating the source .SS file used to render this page should be |
||
| 67 | * included in the output. This is enabled by default |
||
| 68 | * |
||
| 69 | * @deprecated 4.0 Use the "SSViewer.source_file_comments" config setting instead |
||
| 70 | * @param boolean $val |
||
| 71 | */ |
||
| 72 | public static function set_source_file_comments($val) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @deprecated 4.0 Use the "SSViewer.source_file_comments" config setting instead |
||
| 80 | * @return boolean |
||
| 81 | */ |
||
| 82 | public static function get_source_file_comments() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array $templates List of templates to select from |
||
| 90 | */ |
||
| 91 | private $templates = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string $chosen Absolute path to chosen template file |
||
| 95 | */ |
||
| 96 | private $chosen = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array Templates to use when looking up 'Layout' or 'Content' |
||
| 100 | */ |
||
| 101 | private $subTemplates = null; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $rewriteHashlinks = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @config |
||
| 110 | * @var string A list (highest priority first) of themes to use |
||
| 111 | * Only used when {@link $theme_enabled} is set to TRUE. |
||
| 112 | */ |
||
| 113 | private static $themes = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @deprecated 4.0..5.0 |
||
| 117 | * @config |
||
| 118 | * @var string The used "theme", which usually consists of templates, images and stylesheets. |
||
| 119 | * Only used when {@link $theme_enabled} is set to TRUE, and $themes is empty |
||
| 120 | */ |
||
| 121 | private static $theme = null; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @config |
||
| 125 | * @var boolean Use the theme. Set to FALSE in order to disable themes, |
||
| 126 | * which can be useful for scenarios where theme overrides are temporarily undesired, |
||
| 127 | * such as an administrative interface separate from the website theme. |
||
| 128 | * It retains the theme settings to be re-enabled, for example when a website content |
||
| 129 | * needs to be rendered from within this administrative interface. |
||
| 130 | */ |
||
| 131 | private static $theme_enabled = true; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var boolean |
||
| 135 | */ |
||
| 136 | protected $includeRequirements = true; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var TemplateParser |
||
| 140 | */ |
||
| 141 | protected $parser; |
||
| 142 | |||
| 143 | /* |
||
| 144 | * Default prepended cache key for partial caching |
||
| 145 | * |
||
| 146 | * @var string |
||
| 147 | * @config |
||
| 148 | */ |
||
| 149 | private static $global_key = '$CurrentReadingMode, $CurrentUser.ID'; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Triggered early in the request when someone requests a flush. |
||
| 153 | */ |
||
| 154 | public static function flush() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Create a template from a string instead of a .ss file |
||
| 162 | * |
||
| 163 | * @param string $content The template content |
||
| 164 | * @param bool|void $cacheTemplate Whether or not to cache the template from string |
||
| 165 | * @return SSViewer |
||
| 166 | */ |
||
| 167 | public static function fromString($content, $cacheTemplate = null) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Assign the list of active themes to apply. |
||
| 178 | * If default themes should be included add $default as the last entry. |
||
| 179 | * |
||
| 180 | * @param array $themes |
||
| 181 | */ |
||
| 182 | public static function set_themes($themes = []) |
||
| 188 | |||
| 189 | public static function add_themes($themes = []) |
||
| 193 | |||
| 194 | public static function get_themes() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @deprecated 4.0 Use the "SSViewer.theme" config setting instead |
||
| 217 | * @param string $theme The "base theme" name (without underscores). |
||
| 218 | */ |
||
| 219 | public static function set_theme($theme) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Traverses the given the given class context looking for candidate template names |
||
| 227 | * which match each item in the class hierarchy. The resulting list of template candidates |
||
| 228 | * may or may not exist, but you can invoke {@see SSViewer::chooseTemplate} on any list |
||
| 229 | * to determine the best candidate based on the current themes. |
||
| 230 | * |
||
| 231 | * @param string|object $classOrObject Valid class name, or object |
||
| 232 | * @param string $suffix |
||
| 233 | * @param string $baseClass Class to halt ancestry search at |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public static function get_templates_by_class($classOrObject, $suffix = '', $baseClass = null) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string|array $templates If passed as a string with .ss extension, used as the "main" template. |
||
| 267 | * If passed as an array, it can be used for template inheritance (first found template "wins"). |
||
| 268 | * Usually the array values are PHP class names, which directly correlate to template names. |
||
| 269 | * <code> |
||
| 270 | * array('MySpecificPage', 'MyPage', 'Page') |
||
| 271 | * </code> |
||
| 272 | * @param TemplateParser $parser |
||
| 273 | */ |
||
| 274 | public function __construct($templates, TemplateParser $parser = null) |
||
| 296 | |||
| 297 | public function setTemplate($templates) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Find the template to use for a given list |
||
| 306 | * |
||
| 307 | * @param array|string $templates |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public static function chooseTemplate($templates) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Set the template parser that will be used in template generation |
||
| 317 | * |
||
| 318 | * @param TemplateParser $parser |
||
| 319 | */ |
||
| 320 | public function setParser(TemplateParser $parser) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns the parser that is set for template generation |
||
| 327 | * |
||
| 328 | * @return TemplateParser |
||
| 329 | */ |
||
| 330 | public function getParser() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Returns true if at least one of the listed templates exists. |
||
| 340 | * |
||
| 341 | * @param array $templates |
||
| 342 | * |
||
| 343 | * @return boolean |
||
| 344 | */ |
||
| 345 | public static function hasTemplate($templates) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set a global rendering option. |
||
| 352 | * |
||
| 353 | * The following options are available: |
||
| 354 | * - rewriteHashlinks: If true (the default), <a href="#..."> will be rewritten to contain the |
||
| 355 | * current URL. This lets it play nicely with our <base> tag. |
||
| 356 | * - If rewriteHashlinks = 'php' then, a piece of PHP script will be inserted before the hash |
||
| 357 | * links: "<?php echo $_SERVER['REQUEST_URI']; ?>". This is useful if you're generating a |
||
| 358 | * page that will be saved to a .php file and may be accessed from different URLs. |
||
| 359 | * |
||
| 360 | * @deprecated 4.0 Use the "SSViewer.rewrite_hash_links" config setting instead |
||
| 361 | * @param string $optionName |
||
| 362 | * @param mixed $optionVal |
||
| 363 | */ |
||
| 364 | public static function setOption($optionName, $optionVal) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @deprecated 4.0 Use the "SSViewer.rewrite_hash_links" config setting instead |
||
| 377 | * @param string |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | public static function getOption($optionName) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @config |
||
| 393 | * @var boolean |
||
| 394 | */ |
||
| 395 | private static $rewrite_hash_links = true; |
||
| 396 | |||
| 397 | protected static $topLevel = array(); |
||
| 398 | |||
| 399 | public static function topLevel() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Call this to disable rewriting of <a href="#xxx"> links. This is useful in Ajax applications. |
||
| 409 | * It returns the SSViewer objects, so that you can call new SSViewer("X")->dontRewriteHashlinks()->process(); |
||
| 410 | */ |
||
| 411 | public function dontRewriteHashlinks() |
||
| 417 | |||
| 418 | public function exists() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param string $identifier A template name without '.ss' extension or path |
||
| 425 | * @param string $type The template type, either "main", "Includes" or "Layout" |
||
| 426 | * |
||
| 427 | * @return string Full system path to a template file |
||
| 428 | */ |
||
| 429 | public static function getTemplateFileByType($identifier, $type = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Clears all parsed template files in the cache folder. |
||
| 436 | * |
||
| 437 | * Can only be called once per request (there may be multiple SSViewer instances). |
||
| 438 | * |
||
| 439 | * @param bool $force Set this to true to force a re-flush. If left to false, flushing |
||
| 440 | * may only be performed once a request. |
||
| 441 | */ |
||
| 442 | public static function flush_template_cache($force = false) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Clears all partial cache blocks. |
||
| 457 | * |
||
| 458 | * Can only be called once per request (there may be multiple SSViewer instances). |
||
| 459 | * |
||
| 460 | * @param bool $force Set this to true to force a re-flush. If left to false, flushing |
||
| 461 | * may only be performed once a request. |
||
| 462 | */ |
||
| 463 | public static function flush_cacheblock_cache($force = false) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @var CacheInterface |
||
| 476 | */ |
||
| 477 | protected $partialCacheStore = null; |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set the cache object to use when storing / retrieving partial cache blocks. |
||
| 481 | * |
||
| 482 | * @param CacheInterface $cache |
||
| 483 | */ |
||
| 484 | public function setPartialCacheStore($cache) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Get the cache object to use when storing / retrieving partial cache blocks. |
||
| 491 | * |
||
| 492 | * @return CacheInterface |
||
| 493 | */ |
||
| 494 | public function getPartialCacheStore() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Flag whether to include the requirements in this response. |
||
| 501 | * |
||
| 502 | * @param boolean |
||
| 503 | */ |
||
| 504 | public function includeRequirements($incl = true) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * An internal utility function to set up variables in preparation for including a compiled |
||
| 511 | * template, then do the include |
||
| 512 | * |
||
| 513 | * Effectively this is the common code that both SSViewer#process and SSViewer_FromString#process call |
||
| 514 | * |
||
| 515 | * @param string $cacheFile The path to the file that contains the template compiled to PHP |
||
| 516 | * @param ViewableData $item The item to use as the root scope for the template |
||
| 517 | * @param array $overlay Any variables to layer on top of the scope |
||
| 518 | * @param array $underlay Any variables to layer underneath the scope |
||
| 519 | * @param ViewableData $inheritedScope The current scope of a parent template including a sub-template |
||
| 520 | * @return string The result of executing the template |
||
| 521 | */ |
||
| 522 | protected function includeGeneratedTemplate($cacheFile, $item, $overlay, $underlay, $inheritedScope = null) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * The process() method handles the "meat" of the template processing. |
||
| 545 | * |
||
| 546 | * It takes care of caching the output (via {@link Cache}), as well as |
||
| 547 | * replacing the special "$Content" and "$Layout" placeholders with their |
||
| 548 | * respective subtemplates. |
||
| 549 | * |
||
| 550 | * The method injects extra HTML in the header via {@link Requirements::includeInHTML()}. |
||
| 551 | * |
||
| 552 | * Note: You can call this method indirectly by {@link ViewableData->renderWith()}. |
||
| 553 | * |
||
| 554 | * @param ViewableData $item |
||
| 555 | * @param array|null $arguments Arguments to an included template |
||
| 556 | * @param ViewableData $inheritedScope The current scope of a parent template including a sub-template |
||
| 557 | * @return DBHTMLText Parsed template output. |
||
| 558 | */ |
||
| 559 | public function process($item, $arguments = null, $inheritedScope = null) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Execute the given template, passing it the given data. |
||
| 633 | * Used by the <% include %> template tag to process templates. |
||
| 634 | * |
||
| 635 | * @param string $template Template name |
||
| 636 | * @param mixed $data Data context |
||
| 637 | * @param array $arguments Additional arguments |
||
| 638 | * @param Object $scope |
||
| 639 | * @return string Evaluated result |
||
| 640 | */ |
||
| 641 | public static function execute_template($template, $data, $arguments = null, $scope = null) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Execute the evaluated string, passing it the given data. |
||
| 651 | * Used by partial caching to evaluate custom cache keys expressed using |
||
| 652 | * template expressions |
||
| 653 | * |
||
| 654 | * @param string $content Input string |
||
| 655 | * @param mixed $data Data context |
||
| 656 | * @param array $arguments Additional arguments |
||
| 657 | * @return string Evaluated result |
||
| 658 | */ |
||
| 659 | public static function execute_string($content, $data, $arguments = null) |
||
| 666 | |||
| 667 | public function parseTemplateContent($content, $template = "") |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Returns the filenames of the template that will be rendered. It is a map that may contain |
||
| 678 | * 'Content' & 'Layout', and will have to contain 'main' |
||
| 679 | */ |
||
| 680 | public function templates() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param string $type "Layout" or "main" |
||
| 687 | * @param string $file Full system path to the template file |
||
| 688 | */ |
||
| 689 | public function setTemplateFile($type, $file) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Return an appropriate base tag for the given template. |
||
| 700 | * It will be closed on an XHTML document, and unclosed on an HTML document. |
||
| 701 | * |
||
| 702 | * @param string $contentGeneratedSoFar The content of the template generated so far; it should contain |
||
| 703 | * the DOCTYPE declaration. |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | public static function get_base_tag($contentGeneratedSoFar) |
||
| 717 | } |
||
| 718 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.