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() { |
||
186 | |||
187 | /** |
||
188 | * Get the hostname for host normalization from Magento's base URL |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | protected function _getNormalizeHostTarget() { |
||
206 | |||
207 | /** |
||
208 | * Get hosts as regex |
||
209 | * |
||
210 | * ex: base_url: example.com |
||
211 | * path_regex: (example.com|example.net) |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getAllowedHostsRegex() { |
||
226 | |||
227 | /** |
||
228 | * Get the Host normalization sub routine |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | protected function _vcl_sub_allowed_hosts_regex() { |
||
242 | |||
243 | /** |
||
244 | * Get the base url path regex |
||
245 | * |
||
246 | * ex: base_url: http://example.com/magento/ |
||
247 | * path_regex: /magento/(?:(?:index|litespeed)\.php/)? |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getBaseUrlPathRegex() { |
||
257 | |||
258 | /** |
||
259 | * Get the path part of each store's base URL and static file URLs |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | protected function _getBaseUrlPaths() { |
||
282 | |||
283 | /** |
||
284 | * Format the URL exclusions for insertion in a regex. Admin frontname and |
||
285 | * API are automatically added. |
||
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | protected function _getUrlExcludes() { |
||
294 | |||
295 | /** |
||
296 | * Get the default cache TTL from Magento config |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | protected function _getDefaultTtl() { |
||
303 | |||
304 | /** |
||
305 | * Get the default backend configuration string |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | View Code Duplication | protected function _getDefaultBackend() { |
|
324 | |||
325 | /** |
||
326 | * Get the admin backend configuration string |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | View Code Duplication | protected function _getAdminBackend() { |
|
345 | |||
346 | /** |
||
347 | * Get the grace period for vcl_fetch |
||
348 | * |
||
349 | * This is curently hardcoded to 15 seconds, will be configurable at some |
||
350 | * point |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | protected function _getGracePeriod() { |
||
357 | |||
358 | /** |
||
359 | * Get whether debug headers should be enabled or not |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | protected function _getEnableDebugHeaders() { |
||
367 | |||
368 | /** |
||
369 | * Format the GET variable excludes for insertion in a regex |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | protected function _getGetParamExcludes() { |
||
377 | |||
378 | protected function _getIgnoreGetParameters() |
||
385 | |||
386 | /** |
||
387 | * @return boolean |
||
388 | */ |
||
389 | protected function _sendUnModifiedUrlToBackend() |
||
393 | |||
394 | /** |
||
395 | * Get the Generate Session |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function _getGenerateSessionStart() { |
||
403 | |||
404 | /** |
||
405 | * Get the Generate Session |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | protected function _getGenerateSessionEnd() { |
||
413 | |||
414 | |||
415 | /** |
||
416 | * Get the Generate Session |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | protected function _getGenerateSession() { |
||
424 | |||
425 | |||
426 | /** |
||
427 | * Get the Generate Session Expires |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | protected function _getGenerateSessionExpires() { |
||
435 | |||
436 | /** |
||
437 | * Get the Force Static Caching option |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | protected function _getForceCacheStatic() { |
||
445 | |||
446 | /** |
||
447 | * Get the Force Static Caching option |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | protected function _getSimpleHashStatic() { |
||
455 | |||
456 | /** |
||
457 | * Format the list of static cache extensions |
||
458 | * |
||
459 | * @return string |
||
460 | */ |
||
461 | protected function _getStaticExtensions() { |
||
465 | |||
466 | /** |
||
467 | * Get the static caching TTL |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | protected function _getStaticTtl() { |
||
474 | |||
475 | /** |
||
476 | * Format the by-url TTL value list |
||
477 | * |
||
478 | * @return string |
||
479 | */ |
||
480 | protected function _getUrlTtls() { |
||
501 | |||
502 | /** |
||
503 | * Get the Enable Caching value |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | protected function _getEnableCaching() { |
||
511 | |||
512 | /** |
||
513 | * Get the list of allowed debug IPs |
||
514 | * |
||
515 | * @return array |
||
516 | */ |
||
517 | protected function _getDebugIps() { |
||
521 | |||
522 | /** |
||
523 | * Get the list of crawler IPs |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | protected function _getCrawlerIps() { |
||
531 | |||
532 | /** |
||
533 | * Get the regex formatted list of crawler user agents |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | protected function _getCrawlerUserAgents() { |
||
543 | |||
544 | /** |
||
545 | * Get the time to increase a cached objects TTL on cache hit (in seconds). |
||
546 | * |
||
547 | * This should be set very low since it gets added to every hit. |
||
548 | * |
||
549 | * @return string |
||
550 | */ |
||
551 | protected function _getLruFactor() { |
||
554 | |||
555 | /** |
||
556 | * Get the advanced session validation restrictions |
||
557 | * |
||
558 | * Note that if User-Agent Normalization is on then the normalized user-agent |
||
559 | * is used for user-agent validation instead of the full user-agent |
||
560 | * |
||
561 | * @return string |
||
562 | */ |
||
563 | protected function _getAdvancedSessionValidationTargets() { |
||
582 | |||
583 | /** |
||
584 | * Remove empty and commented out lines from the generated VCL |
||
585 | * |
||
586 | * @param string $dirtyVcl generated vcl |
||
587 | * @return string |
||
588 | */ |
||
589 | protected function _cleanVcl($dirtyVcl) { |
||
598 | |||
599 | /** |
||
600 | * Helper to filter out blank/commented lines for VCL cleaning |
||
601 | * |
||
602 | * @param string $line |
||
603 | * @return bool |
||
604 | */ |
||
605 | protected function _cleanVclHelper($line) { |
||
611 | |||
612 | /** |
||
613 | * Format a VCL backend declaration |
||
614 | * |
||
615 | * @param string $name name of the backend |
||
616 | * @param string $host backend host |
||
617 | * @param string $port backend port |
||
618 | * @param array $options options |
||
619 | * @return string |
||
620 | */ |
||
621 | protected function _vcl_backend($name, $host, $port, $options = array()) { |
||
640 | |||
641 | /** |
||
642 | * Format a VCL director declaration, for load balancing |
||
643 | * |
||
644 | * @param string $name name of the director, also used to select config settings |
||
645 | * @param array $backendOptions options for each backend |
||
646 | * @return string |
||
647 | */ |
||
648 | protected function _vcl_director($name, $backendOptions) { |
||
676 | |||
677 | /** |
||
678 | * Format a VCL backend declaration to put inside director |
||
679 | * |
||
680 | * @param string $host backend host |
||
681 | * @param string $port backend port |
||
682 | * @param string $probeUrl URL to check if backend is up |
||
683 | * @param array $options extra options for backend |
||
684 | * @return string |
||
685 | */ |
||
686 | View Code Duplication | protected function _vcl_director_backend($host, $port, $probeUrl = '', $options = array()) { |
|
713 | |||
714 | /** |
||
715 | * Format a VCL probe declaration to put in backend which is in director |
||
716 | * |
||
717 | * @param string $probeUrl URL to check if backend is up |
||
718 | * @return string |
||
719 | */ |
||
720 | protected function _vcl_get_probe($probeUrl) { |
||
741 | |||
742 | /** |
||
743 | * Format a VCL ACL declaration |
||
744 | * |
||
745 | * @param string $name ACL name |
||
746 | * @param array $hosts list of hosts to add to the ACL |
||
747 | * @return string |
||
748 | */ |
||
749 | protected function _vcl_acl($name, array $hosts) { |
||
762 | |||
763 | /** |
||
764 | * Get the User-Agent normalization sub routine |
||
765 | * |
||
766 | * @return string |
||
767 | */ |
||
768 | protected function _vcl_sub_normalize_user_agent() { |
||
793 | |||
794 | /** |
||
795 | * Get the Accept-Encoding normalization sub routine |
||
796 | * |
||
797 | * @return string |
||
798 | */ |
||
799 | protected function _vcl_sub_normalize_encoding() { |
||
815 | |||
816 | /** |
||
817 | * Get the Host normalization sub routine |
||
818 | * |
||
819 | * @return string |
||
820 | */ |
||
821 | protected function _vcl_sub_normalize_host() { |
||
829 | |||
830 | /** |
||
831 | * Get the hostname for cookie normalization |
||
832 | * |
||
833 | * @return string |
||
834 | */ |
||
835 | protected function _getNormalizeCookieTarget() { |
||
839 | |||
840 | /** |
||
841 | * Get the regex for cookie normalization |
||
842 | * |
||
843 | * @return string |
||
844 | */ |
||
845 | protected function _getNormalizeCookieRegex() { |
||
849 | |||
850 | /** |
||
851 | * Get the allowed IPs when in maintenance mode |
||
852 | * |
||
853 | * @return string |
||
854 | */ |
||
855 | View Code Duplication | protected function _vcl_sub_maintenance_allowed_ips() { |
|
893 | |||
894 | /** |
||
895 | * Get the allowed IPs when in maintenance mode |
||
896 | * |
||
897 | * @return string |
||
898 | */ |
||
899 | View Code Duplication | protected function _vcl_sub_synth() |
|
933 | |||
934 | |||
935 | |||
936 | /** |
||
937 | * Build the list of template variables to apply to the VCL template |
||
938 | * |
||
939 | * @return array |
||
940 | */ |
||
941 | protected function _getTemplateVars() { |
||
1021 | } |
||
1022 |
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.