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 Director 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 Director, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Director implements TemplateGlobalProvider |
||
30 | { |
||
31 | use Configurable; |
||
32 | |||
33 | /** |
||
34 | * Specifies this url is relative to the base. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const BASE = 'BASE'; |
||
39 | |||
40 | /** |
||
41 | * Specifies this url is relative to the site root. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | const ROOT = 'ROOT'; |
||
46 | |||
47 | /** |
||
48 | * specifies this url is relative to the current request. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | const REQUEST = 'REQUEST'; |
||
53 | |||
54 | /** |
||
55 | * @config |
||
56 | * @var array |
||
57 | */ |
||
58 | private static $rules = array(); |
||
59 | |||
60 | /** |
||
61 | * Set current page |
||
62 | * |
||
63 | * @internal |
||
64 | * @var SiteTree |
||
65 | */ |
||
66 | private static $current_page; |
||
67 | |||
68 | /** |
||
69 | * @config |
||
70 | * @var string |
||
71 | */ |
||
72 | private static $alternate_base_folder; |
||
73 | |||
74 | /** |
||
75 | * Force the base_url to a specific value. |
||
76 | * If assigned, default_base_url and the value in the $_SERVER |
||
77 | * global is ignored. |
||
78 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
79 | * |
||
80 | * @config |
||
81 | * @var string |
||
82 | */ |
||
83 | private static $alternate_base_url; |
||
84 | |||
85 | /** |
||
86 | * Base url to populate if cannot be determined otherwise. |
||
87 | * Supports back-ticked vars; E.g. '`SS_BASE_URL`' |
||
88 | * |
||
89 | * @config |
||
90 | * @var string |
||
91 | */ |
||
92 | private static $default_base_url = '`SS_BASE_URL`'; |
||
93 | |||
94 | /** |
||
95 | * Assigned environment type |
||
96 | * |
||
97 | * @internal |
||
98 | * @var string |
||
99 | */ |
||
100 | protected static $environment_type; |
||
101 | |||
102 | /** |
||
103 | * Test a URL request, returning a response object. This method is a wrapper around |
||
104 | * Director::handleRequest() to assist with functional testing. It will execute the URL given, and |
||
105 | * return the result as an HTTPResponse object. |
||
106 | * |
||
107 | * @param string $url The URL to visit. |
||
108 | * @param array $postVars The $_POST & $_FILES variables. |
||
109 | * @param array|Session $session The {@link Session} object representing the current session. |
||
110 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
111 | * session. |
||
112 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
113 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
114 | * @param string $body The HTTP body. |
||
115 | * @param array $headers HTTP headers with key-value pairs. |
||
116 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
117 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
118 | * |
||
119 | * @return HTTPResponse |
||
120 | * |
||
121 | * @throws HTTPResponse_Exception |
||
122 | */ |
||
123 | public static function test( |
||
147 | |||
148 | /** |
||
149 | * Mock a request, passing this to the given callback, before resetting. |
||
150 | * |
||
151 | * @param callable $callback Action to pass the HTTPRequst object |
||
152 | * @param string $url The URL to build |
||
153 | * @param array $postVars The $_POST & $_FILES variables. |
||
154 | * @param array|Session $session The {@link Session} object representing the current session. |
||
155 | * By passing the same object to multiple calls of Director::test(), you can simulate a persisted |
||
156 | * session. |
||
157 | * @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if |
||
158 | * postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present. |
||
159 | * @param string $body The HTTP body. |
||
160 | * @param array $headers HTTP headers with key-value pairs. |
||
161 | * @param array|Cookie_Backend $cookies to populate $_COOKIE. |
||
162 | * @param HTTPRequest $request The {@see SS_HTTP_Request} object generated as a part of this request. |
||
163 | * @return mixed Result of callback |
||
164 | */ |
||
165 | public static function mockRequest( |
||
286 | |||
287 | /** |
||
288 | * Process the given URL, creating the appropriate controller and executing it. |
||
289 | * |
||
290 | * Request processing is handled as follows: |
||
291 | * - Director::handleRequest($request) checks each of the Director rules and identifies a controller |
||
292 | * to handle this request. |
||
293 | * - Controller::handleRequest($request) is then called. This will find a rule to handle the URL, |
||
294 | * and call the rule handling method. |
||
295 | * - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method |
||
296 | * returns a RequestHandler object. |
||
297 | * |
||
298 | * In addition to request processing, Director will manage the session, and perform the output of |
||
299 | * the actual response to the browser. |
||
300 | * |
||
301 | * @uses handleRequest() rule-lookup logic is handled by this. |
||
302 | * @param HTTPRequest $request |
||
303 | * @return HTTPResponse |
||
304 | * @throws HTTPResponse_Exception |
||
305 | * @param HTTPRequest $request |
||
306 | * @return HTTPResponse |
||
307 | */ |
||
308 | public static function handleRequest(HTTPRequest $request) |
||
396 | |||
397 | /** |
||
398 | * Call the given request handler with the given middlewares |
||
399 | * Middlewares are specified as Injector service names |
||
400 | * |
||
401 | * @param $request The request to pass to the handler |
||
402 | * @param $middlewareNames The services names of the middlewares to apply |
||
403 | * @param $handler The request handler |
||
404 | */ |
||
405 | protected static function callWithMiddlewares(HTTPRequest $request, array $middlewareNames, callable $handler) |
||
428 | |||
429 | /** |
||
430 | * Return the {@link SiteTree} object that is currently being viewed. If there is no SiteTree |
||
431 | * object to return, then this will return the current controller. |
||
432 | * |
||
433 | * @return SiteTree|Controller |
||
434 | */ |
||
435 | public static function get_current_page() |
||
439 | |||
440 | /** |
||
441 | * Set the currently active {@link SiteTree} object that is being used to respond to the request. |
||
442 | * |
||
443 | * @param SiteTree $page |
||
444 | */ |
||
445 | public static function set_current_page($page) |
||
449 | |||
450 | /** |
||
451 | * Turns the given URL into an absolute URL. By default non-site root relative urls will be |
||
452 | * evaluated relative to the current base_url. |
||
453 | * |
||
454 | * @param string $url URL To transform to absolute. |
||
455 | * @param string $relativeParent Method to use for evaluating relative urls. |
||
456 | * Either one of BASE (baseurl), ROOT (site root), or REQUEST (requested page). |
||
457 | * Defaults to BASE, which is the same behaviour as template url resolution. |
||
458 | * Ignored if the url is absolute or site root. |
||
459 | * |
||
460 | * @return string |
||
461 | */ |
||
462 | public static function absoluteURL($url, $relativeParent = self::BASE) |
||
502 | |||
503 | /** |
||
504 | * A helper to determine the current hostname used to access the site. |
||
505 | * The following are used to determine the host (in order) |
||
506 | * - Director.alternate_base_url (if it contains a domain name) |
||
507 | * - Trusted proxy headers |
||
508 | * - HTTP Host header |
||
509 | * - SS_BASE_URL env var |
||
510 | * - SERVER_NAME |
||
511 | * - gethostname() |
||
512 | * |
||
513 | * @return string |
||
514 | */ |
||
515 | public static function host() |
||
548 | |||
549 | /** |
||
550 | * Returns the domain part of the URL 'http://www.mysite.com'. Returns FALSE is this environment |
||
551 | * variable isn't set. |
||
552 | * |
||
553 | * @return bool|string |
||
554 | */ |
||
555 | public static function protocolAndHost() |
||
559 | |||
560 | /** |
||
561 | * Return the current protocol that the site is running under. |
||
562 | * |
||
563 | * @return string |
||
564 | */ |
||
565 | public static function protocol() |
||
569 | |||
570 | /** |
||
571 | * Return whether the site is running as under HTTPS. |
||
572 | * |
||
573 | * @return bool |
||
574 | */ |
||
575 | public static function is_https() |
||
603 | |||
604 | /** |
||
605 | * Return the root-relative url for the baseurl |
||
606 | * |
||
607 | * @return string Root-relative url with trailing slash. |
||
608 | */ |
||
609 | public static function baseURL() |
||
629 | |||
630 | /** |
||
631 | * Returns the root filesystem folder for the site. It will be automatically calculated unless |
||
632 | * it is overridden with {@link setBaseFolder()}. |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | public static function baseFolder() |
||
641 | |||
642 | /** |
||
643 | * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful |
||
644 | * when turning a URL into a filesystem reference, or vice versa. |
||
645 | * |
||
646 | * @param string $url Accepts both a URL or a filesystem path. |
||
647 | * |
||
648 | * @return string |
||
649 | */ |
||
650 | public static function makeRelative($url) |
||
698 | |||
699 | /** |
||
700 | * Returns true if a given path is absolute. Works under both *nix and windows systems. |
||
701 | * |
||
702 | * @param string $path |
||
703 | * |
||
704 | * @return bool |
||
705 | */ |
||
706 | public static function is_absolute($path) |
||
716 | |||
717 | /** |
||
718 | * Determine if the url is root relative (i.e. starts with /, but not with //) SilverStripe |
||
719 | * considers root relative urls as a subset of relative urls. |
||
720 | * |
||
721 | * @param string $url |
||
722 | * |
||
723 | * @return bool |
||
724 | */ |
||
725 | public static function is_root_relative_url($url) |
||
729 | |||
730 | /** |
||
731 | * Checks if a given URL is absolute (e.g. starts with 'http://' etc.). URLs beginning with "//" |
||
732 | * are treated as absolute, as browsers take this to mean the same protocol as currently being used. |
||
733 | * |
||
734 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
735 | * and avoid phishing attacks by redirecting to an attackers server. |
||
736 | * |
||
737 | * Note: Can't solely rely on PHP's parse_url() , since it is not intended to work with relative URLs |
||
738 | * or for security purposes. filter_var($url, FILTER_VALIDATE_URL) has similar problems. |
||
739 | * |
||
740 | * @param string $url |
||
741 | * |
||
742 | * @return bool |
||
743 | */ |
||
744 | public static function is_absolute_url($url) |
||
770 | |||
771 | /** |
||
772 | * Checks if a given URL is relative (or root relative) by checking {@link is_absolute_url()}. |
||
773 | * |
||
774 | * @param string $url |
||
775 | * |
||
776 | * @return bool |
||
777 | */ |
||
778 | public static function is_relative_url($url) |
||
782 | |||
783 | /** |
||
784 | * Checks if the given URL is belonging to this "site" (not an external link). That's the case if |
||
785 | * the URL is relative, as defined by {@link is_relative_url()}, or if the host matches |
||
786 | * {@link protocolAndHost()}. |
||
787 | * |
||
788 | * Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, |
||
789 | * and avoid phishing attacks by redirecting to an attackers server. |
||
790 | * |
||
791 | * @param string $url |
||
792 | * |
||
793 | * @return bool |
||
794 | */ |
||
795 | public static function is_site_url($url) |
||
805 | |||
806 | /** |
||
807 | * Given a filesystem reference relative to the site root, return the full file-system path. |
||
808 | * |
||
809 | * @param string $file |
||
810 | * |
||
811 | * @return string |
||
812 | */ |
||
813 | public static function getAbsFile($file) |
||
817 | |||
818 | /** |
||
819 | * Returns true if the given file exists. Filename should be relative to the site root. |
||
820 | * |
||
821 | * @param $file |
||
822 | * |
||
823 | * @return bool |
||
824 | */ |
||
825 | public static function fileExists($file) |
||
831 | |||
832 | /** |
||
833 | * Returns the Absolute URL of the site root. |
||
834 | * |
||
835 | * @return string |
||
836 | */ |
||
837 | public static function absoluteBaseURL() |
||
844 | |||
845 | /** |
||
846 | * Returns the Absolute URL of the site root, embedding the current basic-auth credentials into |
||
847 | * the URL. |
||
848 | * |
||
849 | * @return string |
||
850 | */ |
||
851 | public static function absoluteBaseURLWithAuth() |
||
861 | |||
862 | /** |
||
863 | * Skip any further processing and immediately respond with a redirect to the passed URL. |
||
864 | * |
||
865 | * @param string $destURL |
||
866 | * @throws HTTPResponse_Exception |
||
867 | */ |
||
868 | protected static function force_redirect($destURL) |
||
876 | |||
877 | /** |
||
878 | * Force the site to run on SSL. |
||
879 | * |
||
880 | * To use, call from _config.php. For example: |
||
881 | * <code> |
||
882 | * if (Director::isLive()) Director::forceSSL(); |
||
883 | * </code> |
||
884 | * |
||
885 | * If you don't want your entire site to be on SSL, you can pass an array of PCRE regular expression |
||
886 | * patterns for matching relative URLs. For example: |
||
887 | * <code> |
||
888 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/')); |
||
889 | * </code> |
||
890 | * |
||
891 | * If you want certain parts of your site protected under a different domain, you can specify |
||
892 | * the domain as an argument: |
||
893 | * <code> |
||
894 | * if (Director::isLive()) Director::forceSSL(array('/^admin/', '/^Security/'), 'secure.mysite.com'); |
||
895 | * </code> |
||
896 | * |
||
897 | * Note that the session data will be lost when moving from HTTP to HTTPS. It is your responsibility |
||
898 | * to ensure that this won't cause usability problems. |
||
899 | * |
||
900 | * CAUTION: This does not respect the site environment mode. You should check this |
||
901 | * as per the above examples using Director::isLive() or Director::isTest() for example. |
||
902 | * |
||
903 | * @param array $patterns Array of regex patterns to match URLs that should be HTTPS. |
||
904 | * @param string $secureDomain Secure domain to redirect to. Defaults to the current domain. |
||
905 | * @return bool true if already on SSL, false if doesn't match patterns (or cannot redirect) |
||
906 | * @throws HTTPResponse_Exception Throws exception with redirect, if successful |
||
907 | */ |
||
908 | public static function forceSSL($patterns = null, $secureDomain = null) |
||
946 | |||
947 | /** |
||
948 | * Force a redirect to a domain starting with "www." |
||
949 | */ |
||
950 | public static function forceWWW() |
||
962 | |||
963 | /** |
||
964 | * Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by |
||
965 | * jQuery or whether a manually set request-parameter 'ajax' is present. |
||
966 | * |
||
967 | * @return bool |
||
968 | */ |
||
969 | public static function is_ajax() |
||
980 | |||
981 | /** |
||
982 | * Returns true if this script is being run from the command line rather than the web server. |
||
983 | * |
||
984 | * @return bool |
||
985 | */ |
||
986 | public static function is_cli() |
||
990 | |||
991 | /** |
||
992 | * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and |
||
993 | * {@link Director::isLive()}. |
||
994 | * |
||
995 | * @return bool |
||
996 | */ |
||
997 | public static function get_environment_type() |
||
1003 | |||
1004 | /** |
||
1005 | * This function will return true if the site is in a live environment. For information about |
||
1006 | * environment types, see {@link Director::set_environment_type()}. |
||
1007 | * |
||
1008 | * @return bool |
||
1009 | */ |
||
1010 | public static function isLive() |
||
1014 | |||
1015 | /** |
||
1016 | * This function will return true if the site is in a development environment. For information about |
||
1017 | * environment types, see {@link Director::set_environment_type()}. |
||
1018 | * |
||
1019 | * @return bool |
||
1020 | */ |
||
1021 | public static function isDev() |
||
1025 | |||
1026 | /** |
||
1027 | * This function will return true if the site is in a test environment. For information about |
||
1028 | * environment types, see {@link Director::set_environment_type()}. |
||
1029 | * |
||
1030 | * @return bool |
||
1031 | */ |
||
1032 | public static function isTest() |
||
1036 | |||
1037 | /** |
||
1038 | * Returns an array of strings of the method names of methods on the call that should be exposed |
||
1039 | * as global variables in the templates. |
||
1040 | * |
||
1041 | * @return array |
||
1042 | */ |
||
1043 | public static function get_template_global_variables() |
||
1053 | } |
||
1054 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.