Complex classes like Browscap 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 Browscap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Browscap |
||
47 | { |
||
48 | /** |
||
49 | * Current version of the class. |
||
50 | */ |
||
51 | const VERSION = '1.0'; |
||
52 | |||
53 | /** |
||
54 | * Different ways to access remote and local files. |
||
55 | * |
||
56 | * UPDATE_FOPEN: Uses the fopen url wrapper (use file_get_contents). |
||
57 | * UPDATE_FSOCKOPEN: Uses the socket functions (fsockopen). |
||
58 | * UPDATE_CURL: Uses the cURL extension. |
||
59 | * UPDATE_LOCAL: Updates from a local file (file_get_contents). |
||
60 | */ |
||
61 | const UPDATE_FOPEN = 'URL-wrapper'; |
||
62 | const UPDATE_FSOCKOPEN = 'socket'; |
||
63 | const UPDATE_CURL = 'cURL'; |
||
64 | const UPDATE_LOCAL = 'local'; |
||
65 | |||
66 | /** |
||
67 | * Options for regex patterns. |
||
68 | * |
||
69 | * REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class. |
||
70 | * REGEX_MODIFIERS: Regex modifiers. |
||
71 | */ |
||
72 | const REGEX_DELIMITER = '@'; |
||
73 | const REGEX_MODIFIERS = 'i'; |
||
74 | |||
75 | /** |
||
76 | * The values to quote in the ini file |
||
77 | */ |
||
78 | const VALUES_TO_QUOTE = 'Browser|Parent'; |
||
79 | |||
80 | /** |
||
81 | * Definitions of the function used by the uasort() function to order the |
||
82 | * userAgents array. |
||
83 | * |
||
84 | * ORDER_FUNC_ARGS: Arguments that the function will take. |
||
85 | * ORDER_FUNC_LOGIC: Internal logic of the function. |
||
86 | */ |
||
87 | const ORDER_FUNC_ARGS = '$a, $b'; |
||
88 | const ORDER_FUNC_LOGIC = '$a=strlen($a);$b=strlen($b);return$a==$b?0:($a<$b?1:-1);'; |
||
89 | |||
90 | /** |
||
91 | * The headers to be sent for checking the version and requesting the file. |
||
92 | */ |
||
93 | const REQUEST_HEADERS = "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: Close\r\n\r\n"; |
||
94 | |||
95 | /** |
||
96 | * Options for auto update capabilities |
||
97 | * |
||
98 | * $remoteVerUrl: The location to use to check out if a new version of the |
||
99 | * browscap.ini file is available. |
||
100 | * $remoteIniUrl: The location from which download the ini file. |
||
101 | * The placeholder for the file should be represented by a %s. |
||
102 | * $timeout: The timeout for the requests. |
||
103 | * $updateInterval: The update interval in seconds. |
||
104 | * $errorInterval: The next update interval in seconds in case of an error. |
||
105 | * $doAutoUpdate: Flag to disable the automatic interval based update. |
||
106 | * $updateMethod: The method to use to update the file, has to be a value of |
||
107 | * an UPDATE_* constant, null or false. |
||
108 | */ |
||
109 | public $remoteIniUrl = 'http://tempdownloads.browserscap.com/stream.php?Full_PHP_BrowscapINI'; |
||
110 | public $remoteVerUrl = 'http://tempdownloads.browserscap.com/versions/version-date.php'; |
||
111 | public $timeout = 5; |
||
112 | public $updateInterval = 432000; // 5 days |
||
113 | public $errorInterval = 7200; // 2 hours |
||
114 | public $doAutoUpdate = true; |
||
115 | public $updateMethod = null; |
||
116 | |||
117 | /** |
||
118 | * The path of the local version of the browscap.ini file from which to |
||
119 | * update (to be set only if used). |
||
120 | * |
||
121 | * @var string |
||
122 | */ |
||
123 | public $localFile = null; |
||
124 | |||
125 | /** |
||
126 | * The useragent to include in the requests made by the class during the |
||
127 | * update process. |
||
128 | * |
||
129 | * @var string |
||
130 | */ |
||
131 | public $userAgent = 'Browser Capabilities Project - PHP Browscap/%v %m'; |
||
132 | |||
133 | /** |
||
134 | * Flag to enable only lowercase indexes in the result. |
||
135 | * The cache has to be rebuilt in order to apply this option. |
||
136 | * |
||
137 | * @var bool |
||
138 | */ |
||
139 | public $lowercase = false; |
||
140 | |||
141 | /** |
||
142 | * Flag to enable/disable silent error management. |
||
143 | * In case of an error during the update process the class returns an empty |
||
144 | * array/object if the update process can't take place and the browscap.ini |
||
145 | * file does not exist. |
||
146 | * |
||
147 | * @var bool |
||
148 | */ |
||
149 | public $silent = false; |
||
150 | |||
151 | /** |
||
152 | * Where to store the cached PHP arrays. |
||
153 | * |
||
154 | * @var string |
||
155 | */ |
||
156 | public $cacheFilename = 'cache.php'; |
||
157 | |||
158 | /** |
||
159 | * Where to store the downloaded ini file. |
||
160 | * |
||
161 | * @var string |
||
162 | */ |
||
163 | public $iniFilename = 'browscap.ini'; |
||
164 | |||
165 | /** |
||
166 | * Path to the cache directory |
||
167 | * |
||
168 | * @var string |
||
169 | */ |
||
170 | public $cacheDir = null; |
||
171 | |||
172 | /** |
||
173 | * Flag to be set to true after loading the cache |
||
174 | * |
||
175 | * @var bool |
||
176 | */ |
||
177 | protected $_cacheLoaded = false; |
||
178 | |||
179 | /** |
||
180 | * Where to store the value of the included PHP cache file |
||
181 | * |
||
182 | * @var array |
||
183 | */ |
||
184 | protected $_userAgents = array(); |
||
185 | protected $_browsers = array(); |
||
186 | protected $_patterns = array(); |
||
187 | protected $_properties = array(); |
||
188 | |||
189 | /** |
||
190 | * An associative array of associative arrays in the format |
||
191 | * `$arr['wrapper']['option'] = $value` passed to stream_context_create() |
||
192 | * when building a stream resource. |
||
193 | * |
||
194 | * Proxy settings are stored in this variable. |
||
195 | * |
||
196 | * @see http://www.php.net/manual/en/function.stream-context-create.php |
||
197 | * |
||
198 | * @var array |
||
199 | */ |
||
200 | protected $_streamContextOptions = array(); |
||
201 | |||
202 | /** |
||
203 | * A valid context resource created with stream_context_create(). |
||
204 | * |
||
205 | * @see http://www.php.net/manual/en/function.stream-context-create.php |
||
206 | * |
||
207 | * @var resource |
||
208 | */ |
||
209 | protected $_streamContext = null; |
||
210 | |||
211 | /** |
||
212 | * Constructor class, checks for the existence of (and loads) the cache and |
||
213 | * if needed updated the definitions |
||
214 | * |
||
215 | * @param string $cache_dir |
||
216 | */ |
||
217 | public function __construct($cache_dir) |
||
247 | |||
248 | /** |
||
249 | * Gets the information about the browser by User Agent |
||
250 | * |
||
251 | * @param string $user_agent the user agent string |
||
252 | * @param bool $return_array whether return an array or an object |
||
253 | * @throws Exception |
||
254 | * @return stdObject the object containing the browsers details. Array if |
||
255 | * $return_array is set to true. |
||
256 | */ |
||
257 | public function getBrowser($user_agent = null, $return_array = false) |
||
339 | |||
340 | /** |
||
341 | * Load (auto-set) proxy settings from environment variables. |
||
342 | */ |
||
343 | public function autodetectProxySettings() |
||
359 | |||
360 | /** |
||
361 | * Add proxy settings to the stream context array. |
||
362 | * |
||
363 | * @param string $server Proxy server/host |
||
364 | * @param int $port Port |
||
365 | * @param string $wrapper Wrapper: "http", "https", "ftp", others... |
||
366 | * @param string $username Username (when requiring authentication) |
||
367 | * @param string $password Password (when requiring authentication) |
||
368 | * |
||
369 | * @return Browscap |
||
370 | */ |
||
371 | public function addProxySettings($server, $port = 3128, $wrapper = 'http', $username = null, $password = null) |
||
397 | |||
398 | /** |
||
399 | * Clear proxy settings from the stream context options array. |
||
400 | * |
||
401 | * @param string $wrapper Remove settings from this wrapper only |
||
402 | * |
||
403 | * @return array Wrappers cleared |
||
404 | */ |
||
405 | public function clearProxySettings($wrapper = null) |
||
430 | |||
431 | /** |
||
432 | * Returns the array of stream context options. |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | public function getStreamContextOptions() |
||
440 | |||
441 | /** |
||
442 | * Parses the ini file and updates the cache files |
||
443 | * |
||
444 | * @return bool whether the file was correctly written to the disk |
||
445 | */ |
||
446 | public function updateCache() |
||
523 | |||
524 | /** |
||
525 | * Loads the cache into object's properties |
||
526 | * |
||
527 | * @return void |
||
528 | */ |
||
529 | protected function _loadCache($cache_file) |
||
540 | |||
541 | /** |
||
542 | * Parses the array to cache and creates the PHP string to write to disk |
||
543 | * |
||
544 | * @return string the PHP string to save into the cache file |
||
545 | */ |
||
546 | protected function _buildCache() |
||
563 | |||
564 | /** |
||
565 | * Lazy getter for the stream context resource. |
||
566 | * |
||
567 | * @return resource |
||
568 | */ |
||
569 | protected function _getStreamContext($recreate = false) |
||
577 | |||
578 | /** |
||
579 | * Updates the local copy of the ini file (by version checking) and adapts |
||
580 | * his syntax to the PHP ini parser |
||
581 | * |
||
582 | * @param string $url the url of the remote server |
||
583 | * @param string $path the path of the ini file to update |
||
584 | * @throws Exception |
||
585 | * @return bool if the ini file was updated |
||
586 | */ |
||
587 | protected function _getRemoteIniFile($url, $path) |
||
635 | |||
636 | /** |
||
637 | * Gets the remote ini file update timestamp |
||
638 | * |
||
639 | * @throws Exception |
||
640 | * @return int the remote modification timestamp |
||
641 | */ |
||
642 | protected function _getRemoteMTime() |
||
653 | |||
654 | /** |
||
655 | * Gets the local ini file update timestamp |
||
656 | * |
||
657 | * @throws Exception |
||
658 | * @return int the local modification timestamp |
||
659 | */ |
||
660 | protected function _getLocalMTime() |
||
668 | |||
669 | /** |
||
670 | * Converts the given array to the PHP string which represent it. |
||
671 | * This method optimizes the PHP code and the output differs form the |
||
672 | * var_export one as the internal PHP function does not strip whitespace or |
||
673 | * convert strings to numbers. |
||
674 | * |
||
675 | * @param array $array the array to parse and convert |
||
676 | * @return string the array parsed into a PHP string |
||
677 | */ |
||
678 | protected function _array2string($array) |
||
704 | |||
705 | /** |
||
706 | * Checks for the various possibilities offered by the current configuration |
||
707 | * of PHP to retrieve external HTTP data |
||
708 | * |
||
709 | * @return string the name of function to use to retrieve the file |
||
710 | */ |
||
711 | protected function _getUpdateMethod() |
||
730 | |||
731 | /** |
||
732 | * Retrieve the data identified by the URL |
||
733 | * |
||
734 | * @param string $url the url of the data |
||
735 | * @throws Exception |
||
736 | * @return string the retrieved data |
||
737 | */ |
||
738 | protected function _getRemoteData($url) |
||
815 | |||
816 | /** |
||
817 | * Format the useragent string to be used in the remote requests made by the |
||
818 | * class during the update process. |
||
819 | * |
||
820 | * @return string the formatted user agent |
||
821 | */ |
||
822 | protected function _getUserAgent() |
||
829 | } |
||
830 | |||
842 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.