Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Abstract 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 Abstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class Nexcessnet_Turpentine_Model_Varnish_Configurator_Abstract { |
||
|
|
|||
| 23 | |||
| 24 | const VCL_CUSTOM_C_CODE_FILE = 'uuid.c'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Get the correct version of a configurator from a socket |
||
| 28 | * |
||
| 29 | * @param Nexcessnet_Turpentine_Model_Varnish_Admin_Socket $socket |
||
| 30 | * @return Nexcessnet_Turpentine_Model_Varnish_Configurator_Abstract |
||
| 31 | */ |
||
| 32 | static public function getFromSocket($socket) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The socket this configurator is based on |
||
| 62 | * |
||
| 63 | * @var Nexcessnet_Turpentine_Model_Varnish_Admin_Socket |
||
| 64 | */ |
||
| 65 | protected $_socket = null; |
||
| 66 | /** |
||
| 67 | * options array |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $_options = array( |
||
| 72 | 'vcl_template' => null, |
||
| 73 | ); |
||
| 74 | |||
| 75 | public function __construct($options = array()) { |
||
| 78 | |||
| 79 | abstract public function generate($doClean = true); |
||
| 80 | // abstract protected function _getTemplateVars(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Save the generated config to the file specified in Magento config |
||
| 84 | * |
||
| 85 | * @param string $generatedConfig config generated by @generate |
||
| 86 | * @return null |
||
| 87 | */ |
||
| 88 | public function save($generatedConfig) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the full path for a given template filename |
||
| 108 | * |
||
| 109 | * @param string $baseFilename |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | protected function _getVclTemplateFilename($baseFilename) { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the name of the file to save the VCL to |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | protected function _getVclFilename() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the name of the custom include VCL file |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | protected function _getCustomIncludeFilename() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Format a template string, replacing {{keys}} with the appropriate values |
||
| 141 | * and remove unspecified keys |
||
| 142 | * |
||
| 143 | * @param string $template template string to operate on |
||
| 144 | * @param array $vars array of key => value replacements |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function _formatTemplate($template, array $vars) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Format a VCL subroutine call |
||
| 158 | * |
||
| 159 | * @param string $subroutine subroutine name |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | protected function _vcl_call($subroutine) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the Magento admin frontname |
||
| 168 | * |
||
| 169 | * This is just the plain string, not in URL format. ex: |
||
| 170 | * http://example.com/magento/admin -> admin |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | protected function _getAdminFrontname() { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the hostname for host normalization from Magento's base URL |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | protected function _getNormalizeHostTarget() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get hosts as regex |
||
| 205 | * |
||
| 206 | * ex: base_url: example.com |
||
| 207 | * path_regex: (example.com|example.net) |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getAllowedHostsRegex() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the Host normalization sub routine |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | protected function _vcl_sub_allowed_hosts_regex() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get the base url path regex |
||
| 241 | * |
||
| 242 | * ex: base_url: http://example.com/magento/ |
||
| 243 | * path_regex: /magento/(?:(?:index|litespeed)\.php/)? |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getBaseUrlPathRegex() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Get the path part of each store's base URL and static file URLs |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | protected function _getBaseUrlPaths() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Format the URL exclusions for insertion in a regex. Admin frontname and |
||
| 281 | * API are automatically added. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function _getUrlExcludes() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the default cache TTL from Magento config |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | protected function _getDefaultTtl() { |
||
| 297 | return Mage::helper('turpentine/varnish')->getDefaultTtl(); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the default backend configuration string |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | View Code Duplication | protected function _getDefaultBackend() { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the admin backend configuration string |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | View Code Duplication | protected function _getAdminBackend() { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the grace period for vcl_fetch |
||
| 344 | * |
||
| 345 | * This is curently hardcoded to 15 seconds, will be configurable at some |
||
| 346 | * point |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | protected function _getGracePeriod() { |
||
| 351 | return Mage::getStoreConfig('turpentine_vcl/ttls/grace_period'); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get whether debug headers should be enabled or not |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | protected function _getEnableDebugHeaders() { |
||
| 360 | return Mage::getStoreConfig('turpentine_varnish/general/varnish_debug') |
||
| 361 | ? 'true' : 'false'; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Format the GET variable excludes for insertion in a regex |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | protected function _getGetParamExcludes() { |
||
| 373 | |||
| 374 | protected function _getIgnoreGetParameters() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @return boolean |
||
| 384 | */ |
||
| 385 | protected function _sendUnModifiedUrlToBackend() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Get the Generate Session |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function _getGenerateSessionStart() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the Generate Session |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | protected function _getGenerateSessionEnd() { |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the Generate Session |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | */ |
||
| 416 | protected function _getGenerateSession() { |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the Generate Session Expires |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | protected function _getGenerateSessionExpires() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the Force Static Caching option |
||
| 434 | * |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | protected function _getForceCacheStatic() { |
||
| 438 | return Mage::getStoreConfig('turpentine_vcl/static/force_static') |
||
| 439 | ? 'true' : 'false'; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get the Force Static Caching option |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | protected function _getSimpleHashStatic() { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Format the list of static cache extensions |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | protected function _getStaticExtensions() { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Get the static caching TTL |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | protected function _getStaticTtl() { |
||
| 468 | return Mage::getStoreConfig('turpentine_vcl/ttls/static_ttl'); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Format the by-url TTL value list |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | protected function _getUrlTtls() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the Enable Caching value |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | protected function _getEnableCaching() { |
||
| 504 | return Mage::helper('turpentine/varnish')->getVarnishEnabled() ? |
||
| 505 | 'true' : 'false'; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get the list of allowed debug IPs |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | protected function _getDebugIps() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Get the list of crawler IPs |
||
| 520 | * |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | protected function _getCrawlerIps() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Get the regex formatted list of crawler user agents |
||
| 530 | * |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | protected function _getCrawlerUserAgents() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Get the time to increase a cached objects TTL on cache hit (in seconds). |
||
| 542 | * |
||
| 543 | * This should be set very low since it gets added to every hit. |
||
| 544 | * |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | protected function _getLruFactor() { |
||
| 548 | return Mage::getStoreConfig('turpentine_vcl/ttls/lru_factor'); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the advanced session validation restrictions |
||
| 553 | * |
||
| 554 | * Note that if User-Agent Normalization is on then the normalized user-agent |
||
| 555 | * is used for user-agent validation instead of the full user-agent |
||
| 556 | * |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | protected function _getAdvancedSessionValidationTargets() { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Remove empty and commented out lines from the generated VCL |
||
| 581 | * |
||
| 582 | * @param string $dirtyVcl generated vcl |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | protected function _cleanVcl($dirtyVcl) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Helper to filter out blank/commented lines for VCL cleaning |
||
| 597 | * |
||
| 598 | * @param string $line |
||
| 599 | * @return bool |
||
| 600 | */ |
||
| 601 | protected function _cleanVclHelper($line) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Format a VCL backend declaration |
||
| 610 | * |
||
| 611 | * @param string $name name of the backend |
||
| 612 | * @param string $host backend host |
||
| 613 | * @param string $port backend port |
||
| 614 | * @param array $options options |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | protected function _vcl_backend($name, $host, $port, $options = array()) { |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Format a VCL director declaration, for load balancing |
||
| 639 | * |
||
| 640 | * @param string $name name of the director, also used to select config settings |
||
| 641 | * @param array $backendOptions options for each backend |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | protected function _vcl_director($name, $backendOptions) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Format a VCL backend declaration to put inside director |
||
| 675 | * |
||
| 676 | * @param string $host backend host |
||
| 677 | * @param string $port backend port |
||
| 678 | * @param string $probeUrl URL to check if backend is up |
||
| 679 | * @param array $options extra options for backend |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | protected function _vcl_director_backend($host, $port, $probeUrl = '', $options = array()) { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Format a VCL probe declaration to put in backend which is in director |
||
| 712 | * |
||
| 713 | * @param string $probeUrl URL to check if backend is up |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | protected function _vcl_get_probe($probeUrl) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Format a VCL ACL declaration |
||
| 740 | * |
||
| 741 | * @param string $name ACL name |
||
| 742 | * @param array $hosts list of hosts to add to the ACL |
||
| 743 | * @return string |
||
| 744 | */ |
||
| 745 | protected function _vcl_acl($name, array $hosts) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Get the User-Agent normalization sub routine |
||
| 761 | * |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | protected function _vcl_sub_normalize_user_agent() { |
||
| 765 | /** |
||
| 766 | * Mobile regex from |
||
| 767 | * @link http://magebase.com/magento-tutorials/magento-design-exceptions-explained/ |
||
| 768 | */ |
||
| 769 | $tpl = <<<EOS |
||
| 770 | if (req.http.User-Agent ~ "iP(?:hone|ad|od)|BlackBerry|Palm|Googlebot-Mobile|Mobile|mobile|mobi|Windows Mobile|Safari Mobile|Android|Opera (?:Mini|Mobi)") { |
||
| 771 | set req.http.X-Normalized-User-Agent = "mobile"; |
||
| 772 | } else if (req.http.User-Agent ~ "MSIE") { |
||
| 773 | set req.http.X-Normalized-User-Agent = "msie"; |
||
| 774 | } else if (req.http.User-Agent ~ "Firefox") { |
||
| 775 | set req.http.X-Normalized-User-Agent = "firefox"; |
||
| 776 | } else if (req.http.User-Agent ~ "Chrome") { |
||
| 777 | set req.http.X-Normalized-User-Agent = "chrome"; |
||
| 778 | } else if (req.http.User-Agent ~ "Safari") { |
||
| 779 | set req.http.X-Normalized-User-Agent = "safari"; |
||
| 780 | } else if (req.http.User-Agent ~ "Opera") { |
||
| 781 | set req.http.X-Normalized-User-Agent = "opera"; |
||
| 782 | } else { |
||
| 783 | set req.http.X-Normalized-User-Agent = "other"; |
||
| 784 | } |
||
| 785 | |||
| 786 | EOS; |
||
| 787 | return $tpl; |
||
| 788 | } |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Get the Accept-Encoding normalization sub routine |
||
| 792 | * |
||
| 793 | * @return string |
||
| 794 | */ |
||
| 795 | protected function _vcl_sub_normalize_encoding() { |
||
| 796 | $tpl = <<<EOS |
||
| 797 | if (req.http.Accept-Encoding) { |
||
| 798 | if (req.http.Accept-Encoding ~ "gzip") { |
||
| 799 | set req.http.Accept-Encoding = "gzip"; |
||
| 800 | } else if (req.http.Accept-Encoding ~ "deflate") { |
||
| 801 | set req.http.Accept-Encoding = "deflate"; |
||
| 802 | } else { |
||
| 803 | # unknown algorithm |
||
| 804 | unset req.http.Accept-Encoding; |
||
| 805 | } |
||
| 806 | } |
||
| 807 | |||
| 808 | EOS; |
||
| 809 | return $tpl; |
||
| 810 | } |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get the Host normalization sub routine |
||
| 814 | * |
||
| 815 | * @return string |
||
| 816 | */ |
||
| 817 | protected function _vcl_sub_normalize_host() { |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get the hostname for cookie normalization |
||
| 828 | * |
||
| 829 | * @return string |
||
| 830 | */ |
||
| 831 | protected function _getNormalizeCookieTarget() { |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Get the regex for cookie normalization |
||
| 838 | * |
||
| 839 | * @return string |
||
| 840 | */ |
||
| 841 | protected function _getNormalizeCookieRegex() { |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Get the allowed IPs when in maintenance mode |
||
| 848 | * |
||
| 849 | * @return string |
||
| 850 | */ |
||
| 851 | View Code Duplication | protected function _vcl_sub_maintenance_allowed_ips() { |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Get the allowed IPs when in maintenance mode |
||
| 892 | * |
||
| 893 | * @return string |
||
| 894 | */ |
||
| 895 | View Code Duplication | protected function _vcl_sub_synth() |
|
| 929 | |||
| 930 | |||
| 931 | |||
| 932 | /** |
||
| 933 | * Build the list of template variables to apply to the VCL template |
||
| 934 | * |
||
| 935 | * @return array |
||
| 936 | */ |
||
| 937 | protected function _getTemplateVars() { |
||
| 1017 | } |
||
| 1018 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.