diff --git a/src/phpbrowscap/Browscap.php b/src/phpbrowscap/Browscap.php index 071db06..8160317 100644 --- a/src/phpbrowscap/Browscap.php +++ b/src/phpbrowscap/Browscap.php @@ -31,7 +31,7 @@ * * @package Browscap * @author Jonathan Stoppani - * @author Vítor Brandão + * @author Vítor Brandão * @author Mikołaj Misiurewicz * @copyright Copyright (c) 2006-2012 Jonathan Stoppani * @version 1.0 @@ -44,7 +44,7 @@ class Browscap * Current version of the class. */ const VERSION = '2.0'; - + const CACHE_FILE_VERSION = '2.0b'; /** @@ -68,7 +68,7 @@ class Browscap */ const REGEX_DELIMITER = '@'; const REGEX_MODIFIERS = 'i'; - + const COMPRESSION_PATTERN_START = '@'; const COMPRESSION_PATTERN_DELIMITER = '|'; @@ -76,7 +76,7 @@ class Browscap * The values to quote in the ini file */ const VALUES_TO_QUOTE = 'Browser|Parent'; - + const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version'; /** @@ -97,10 +97,12 @@ class Browscap * $doAutoUpdate: Flag to disable the automatic interval based update. * $updateMethod: The method to use to update the file, has to be a value of * an UPDATE_* constant, null or false. - * - * The default source file type is changed from normal to full. The performance difference - * is MINIMAL, so there is no reason to use the standard file whatsoever. Either go for light, - * which is blazing fast, or get the full one. (note: light version doesn't work, a fix is on its way) + * + * The default source file type is changed from normal to full. The + * performance difference is MINIMAL, so there is no reason to use the + * standard file whatsoever. Either go for light, which is blazing fast, or + * get the full one. (note: light version doesn't work, a fix is on its + * way). */ public $remoteIniUrl = 'http://browscap.org/stream?q=Full_PHP_BrowsCapINI'; public $remoteVerUrl = 'http://browscap.org/version'; @@ -209,40 +211,41 @@ class Browscap * Constructor class, checks for the existence of (and loads) the cache and * if needed updated the definitions * - * @param string $cache_dir + * @param string $cacheDir * @throws Exception */ - public function __construct($cache_dir) + public function __construct($cacheDir) { - // has to be set to reach E_STRICT compatibility, does not affect system/app settings + // Has to be set to reach E_STRICT compatibility, does not affect + // system/app settings date_default_timezone_set(date_default_timezone_get()); - if (!isset($cache_dir)) { + if (!isset($cacheDir)) { throw new Exception( 'You have to provide a path to read/store the browscap cache file' ); } - $old_cache_dir = $cache_dir; - $cache_dir = realpath($cache_dir); + $oldCacheDir = $cacheDir; + $cacheDir = realpath($cacheDir); - if (false === $cache_dir) { + if (false === $cacheDir) { throw new Exception( - sprintf('The cache path %s is invalid. Are you sure that it exists and that you have permission to access it?', $old_cache_dir) + sprintf('The cache path %s is invalid. Are you sure that it exists and that you have permission to access it?', $oldCacheDir) ); } // Is the cache dir really the directory or is it directly the file? - if (substr($cache_dir, -4) === '.php') { - $this->cacheFilename = basename($cache_dir); - $this->cacheDir = dirname($cache_dir); + if (substr($cacheDir, -4) === '.php') { + $this->cacheFilename = basename($cacheDir); + $this->cacheDir = dirname($cacheDir); } else { - $this->cacheDir = $cache_dir; + $this->cacheDir = $cacheDir; } $this->cacheDir .= DIRECTORY_SEPARATOR; } - + public function getSourceVersion() { return $this->_source_version; @@ -250,14 +253,14 @@ public function getSourceVersion() /** * XXX parse - * + * * Gets the information about the browser by User Agent * - * @param string $user_agent the user agent string - * @param bool $return_array whether return an array or an object + * @param string $user_agent the user agent string + * @param bool $return_array whether return an array or an object * @throws Exception - * @return stdClass|array the object containing the browsers details. Array if - * $return_array is set to true. + * @return stdClass|array the object containing the browsers details. Array + * if $return_array is set to true. */ public function getBrowser($user_agent = null, $return_array = false) { @@ -272,17 +275,15 @@ public function getBrowser($user_agent = null, $return_array = false) } else { $interval = 0; } - + $update_cache = true; - - if (file_exists($cache_file) && file_exists($ini_file) && ($interval <= $this->updateInterval)) - { - if ($this->_loadCache($cache_file)) - { + + if (file_exists($cache_file) && file_exists($ini_file) && ($interval <= $this->updateInterval)) { + if ($this->_loadCache($cache_file)) { $update_cache = false; } } - + if ($update_cache) { try { $this->updateCache(); @@ -299,13 +300,12 @@ public function getBrowser($user_agent = null, $return_array = false) throw $e; } } - - if (!$this->_loadCache($cache_file)) - { + + if (!$this->_loadCache($cache_file)) { throw new Exception("Cannot load this cache version - the cache format is not compatible."); } } - + } // Automatically detect the useragent @@ -321,26 +321,26 @@ public function getBrowser($user_agent = null, $return_array = false) foreach ($this->_patterns as $pattern => $pattern_data) { if (preg_match($pattern . 'i', $user_agent, $matches)) { if (1 == count($matches)) { - // standard match - $key = $pattern_data; + // standard match + $key = $pattern_data; - $simple_match = true; + $simple_match = true; } else { - $pattern_data = unserialize($pattern_data); + $pattern_data = unserialize($pattern_data); - // match with numeric replacements - array_shift($matches); + // match with numeric replacements + array_shift($matches); - $match_string = self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, $matches); + $match_string = self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, $matches); - if (!isset($pattern_data[$match_string])) { - // partial match - numbers are not present, but everything else is ok - continue; - } + if (!isset($pattern_data[$match_string])) { + // partial match - numbers are not present, but everything else is ok + continue; + } - $key = $pattern_data[$match_string]; + $key = $pattern_data[$match_string]; - $simple_match = false; + $simple_match = false; } $browser = array( @@ -401,11 +401,11 @@ public function autodetectProxySettings() /** * Add proxy settings to the stream context array. * - * @param string $server Proxy server/host - * @param int $port Port - * @param string $wrapper Wrapper: "http", "https", "ftp", others... - * @param string $username Username (when requiring authentication) - * @param string $password Password (when requiring authentication) + * @param string $server Proxy server/host + * @param int $port Port + * @param string $wrapper Wrapper: "http", "https", "ftp", others... + * @param string $username Username (when requiring authentication) + * @param string $password Password (when requiring authentication) * * @return Browscap */ @@ -433,6 +433,7 @@ public function addProxySettings($server, $port = 3128, $wrapper = 'http', $user * addProxySettings('https')-> * addProxySettings('ftp'); */ + return $this; } @@ -453,7 +454,7 @@ public function clearProxySettings($wrapper = null) // remove wrapper options related to proxy settings if (isset($this->_streamContextOptions[$wrapper]['proxy'])) { - foreach ($options as $option){ + foreach ($options as $option) { unset($this->_streamContextOptions[$wrapper][$option]); } @@ -481,7 +482,7 @@ public function getStreamContextOptions() /** * XXX save - * + * * Parses the ini file and updates the cache files * * @return bool whether the file was correctly written to the disk @@ -505,14 +506,14 @@ public function updateCache() } else { $browsers = parse_ini_file($ini_path, true); } - + $this->_source_version = $browsers[self::BROWSCAP_VERSION_KEY]['Version']; unset($browsers[self::BROWSCAP_VERSION_KEY]); - + unset($browsers['DefaultProperties']['RenderingEngine_Description']); $this->_properties = array_keys($browsers['DefaultProperties']); - + array_unshift( $this->_properties, 'browser_name', @@ -520,9 +521,9 @@ public function updateCache() 'browser_name_pattern', 'Parent' ); - + $tmp_user_agents = array_keys($browsers); - + usort($tmp_user_agents, array($this, 'compareBcStrings')); @@ -532,24 +533,23 @@ public function updateCache() $tmp_patterns = array(); foreach ($tmp_user_agents as $i => $user_agent) { - - if (empty($browsers[$user_agent]['Comment']) || strpos($user_agent, '*') !== false || strpos($user_agent, '?') !== false) - { - $pattern = $this->_pregQuote($user_agent); - - $matches_count = preg_match_all('@\d@', $pattern, $matches); - - if (!$matches_count) { - $tmp_patterns[$pattern] = $i; - } else { - $compressed_pattern = preg_replace('@\d@', '(\d)', $pattern); - - if (!isset($tmp_patterns[$compressed_pattern])) { - $tmp_patterns[$compressed_pattern] = array('first' => $pattern); + + if (empty($browsers[$user_agent]['Comment']) || strpos($user_agent, '*') !== false || strpos($user_agent, '?') !== false) { + $pattern = $this->_pregQuote($user_agent); + + $matches_count = preg_match_all('@\d@', $pattern, $matches); + + if (!$matches_count) { + $tmp_patterns[$pattern] = $i; + } else { + $compressed_pattern = preg_replace('@\d@', '(\d)', $pattern); + + if (!isset($tmp_patterns[$compressed_pattern])) { + $tmp_patterns[$compressed_pattern] = array('first' => $pattern); + } + + $tmp_patterns[$compressed_pattern][$i] = $matches[0]; } - - $tmp_patterns[$compressed_pattern][$i] = $matches[0]; - } } if (!empty($browsers[$user_agent]['Parent'])) { @@ -561,34 +561,33 @@ public function updateCache() $browser = array(); foreach ($browsers[$user_agent] as $key => $value) { - if (!isset($properties_keys[$key])) - { - continue; + if (!isset($properties_keys[$key])) { + continue; } - + $key = $properties_keys[$key]; $browser[$key] = $value; } - + $this->_browsers[] = $browser; } foreach ($tmp_patterns as $pattern => $pattern_data) { - if (is_int($pattern_data)) { - $this->_patterns[$pattern] = $pattern_data; - } elseif (2 == count($pattern_data)) { - end($pattern_data); - $this->_patterns[$pattern_data['first']] = key($pattern_data); - } else { - unset($pattern_data['first']); - - $pattern_data = $this->deduplicateCompressionPattern($pattern_data, $pattern); - - $this->_patterns[$pattern] = $pattern_data; - } + if (is_int($pattern_data)) { + $this->_patterns[$pattern] = $pattern_data; + } elseif (2 == count($pattern_data)) { + end($pattern_data); + $this->_patterns[$pattern_data['first']] = key($pattern_data); + } else { + unset($pattern_data['first']); + + $pattern_data = $this->deduplicateCompressionPattern($pattern_data, $pattern); + + $this->_patterns[$pattern] = $pattern_data; + } } - + // Save the keys lowercased if needed if ($this->lowercase) { $this->_properties = array_map('strtolower', $this->_properties); @@ -600,136 +599,138 @@ public function updateCache() // Save and return return (bool) file_put_contents($cache_path, $cache, LOCK_EX); } - + protected function compareBcStrings($a, $b) { - $a_len = strlen($a); - $b_len = strlen($b); - - if ($a_len > $b_len) return -1; - if ($a_len < $b_len) return 1; - - $a_len = strlen(str_replace(array('*', '?'), '', $a)); - $b_len = strlen(str_replace(array('*', '?'), '', $b)); - - if ($a_len > $b_len) return -1; - if ($a_len < $b_len) return 1; - - return 0; + $a_len = strlen($a); + $b_len = strlen($b); + + if ($a_len > $b_len) { + return -1; + } + if ($a_len < $b_len) { + return 1; + } + + $a_len = strlen(str_replace(array('*', '?'), '', $a)); + $b_len = strlen(str_replace(array('*', '?'), '', $b)); + + if ($a_len > $b_len) { + return -1; + } + if ($a_len < $b_len) { + return 1; + } + + return 0; } - + /** * That looks complicated... - * - * All numbers are taken out into $matches, so we check if any of those numbers are identical - * in all the $matches and if they are we restore them to the $pattern, removing from the $matches. - * This gives us patterns with "(\d)" only in places that differ for some matches. - * - * @param array $matches + * + * All numbers are taken out into $matches, so we check if any of those + * numbers are identical in all the $matches and if they are we restore them + * to the $pattern, removing from the $matches. This gives us patterns with + * "(\d)" only in places that differ for some matches. + * + * @param array $matches * @param string $pattern - * + * * @return array of $matches */ protected function deduplicateCompressionPattern($matches, &$pattern) { - $tmp_matches = $matches; - - $first_match = array_shift($tmp_matches); - - $differences = array(); - - foreach ($tmp_matches as $some_match) - { - $differences += array_diff_assoc($first_match, $some_match); - } - - $identical = array_diff_key($first_match, $differences); - - $prepared_matches = array(); - - foreach ($matches as $i => $some_match) - { - $prepared_matches[self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, array_diff_assoc($some_match, $identical))] = $i; - } - - $pattern_parts = explode('(\d)', $pattern); - - foreach ($identical as $position => $value) - { - $pattern_parts[$position + 1] = $pattern_parts[$position] . $value . $pattern_parts[$position + 1]; - unset($pattern_parts[$position]); - } - - $pattern = implode('(\d)', $pattern_parts); - - return $prepared_matches; + $tmp_matches = $matches; + + $first_match = array_shift($tmp_matches); + + $differences = array(); + + foreach ($tmp_matches as $some_match) { + $differences += array_diff_assoc($first_match, $some_match); + } + + $identical = array_diff_key($first_match, $differences); + + $prepared_matches = array(); + + foreach ($matches as $i => $some_match) { + $prepared_matches[self::COMPRESSION_PATTERN_START . implode(self::COMPRESSION_PATTERN_DELIMITER, array_diff_assoc($some_match, $identical))] = $i; + } + + $pattern_parts = explode('(\d)', $pattern); + + foreach ($identical as $position => $value) { + $pattern_parts[$position + 1] = $pattern_parts[$position] . $value . $pattern_parts[$position + 1]; + unset($pattern_parts[$position]); + } + + $pattern = implode('(\d)', $pattern_parts); + + return $prepared_matches; } - + /** * Converts browscap match patterns into preg match patterns. - * + * * @param string $user_agent - * + * * @return string */ protected function _pregQuote($user_agent) { - $pattern = preg_quote($user_agent, self::REGEX_DELIMITER); - - // the \\x replacement is a fix for "Der gro\xdfe BilderSauger 2.00u" user agent match - - return self::REGEX_DELIMITER - . '^' - . str_replace(array('\*', '\?', '\\x'), array('.*', '.', '\\\\x'), $pattern) - . '$' - . self::REGEX_DELIMITER; + $pattern = preg_quote($user_agent, self::REGEX_DELIMITER); + + // the \\x replacement is a fix for "Der gro\xdfe BilderSauger 2.00u" user agent match + return self::REGEX_DELIMITER + . '^' + . str_replace(array('\*', '\?', '\\x'), array('.*', '.', '\\\\x'), $pattern) + . '$' + . self::REGEX_DELIMITER; } - + /** * Converts preg match patterns back to browscap match patterns. - * + * * @param string $pattern - * @param array $matches - * + * @param array $matches + * * @return string */ protected function _pregUnQuote($pattern, $matches) { - // list of escaped characters: http://www.php.net/manual/en/function.preg-quote.php - // to properly unescape '?' which was changed to '.', I replace '\.' (real dot) with '\?', then change '.' to '?' and then '\?' to '.'. - $search = array('\\' . self::REGEX_DELIMITER, '\\.', '\\\\', '\\+', '\\[', '\\^', '\\]', '\\$', '\\(', '\\)', '\\{', '\\}', '\\=', '\\!', '\\<', '\\>', '\\|', '\\:', '\\-', '.*', '.', '\\?'); - $replace = array(self::REGEX_DELIMITER, '\\?', '\\', '+', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '*', '?', '.'); - - $result = substr(str_replace($search, $replace, $pattern), 2, -2); - - if ($matches) - { - foreach ($matches as $one_match) - { - $num_pos = strpos($result, '(\d)'); - $result = substr_replace($result, $one_match, $num_pos, 4); + // list of escaped characters: http://www.php.net/manual/en/function.preg-quote.php + // to properly unescape '?' which was changed to '.', I replace '\.' (real dot) with '\?', then change '.' to '?' and then '\?' to '.'. + $search = array('\\' . self::REGEX_DELIMITER, '\\.', '\\\\', '\\+', '\\[', '\\^', '\\]', '\\$', '\\(', '\\)', '\\{', '\\}', '\\=', '\\!', '\\<', '\\>', '\\|', '\\:', '\\-', '.*', '.', '\\?'); + $replace = array(self::REGEX_DELIMITER, '\\?', '\\', '+', '[', '^', ']', '$', '(', ')', '{', '}', '=', '!', '<', '>', '|', ':', '-', '*', '?', '.'); + + $result = substr(str_replace($search, $replace, $pattern), 2, -2); + + if ($matches) { + foreach ($matches as $one_match) { + $num_pos = strpos($result, '(\d)'); + $result = substr_replace($result, $one_match, $num_pos, 4); + } } - } - - return $result; + + return $result; } /** * Loads the cache into object's properties * * @param $cache_file - * + * * @return boolean */ protected function _loadCache($cache_file) { require $cache_file; - - if (!isset($cache_version) || $cache_version != self::CACHE_FILE_VERSION) - { + + if (!isset($cache_version) || $cache_version != self::CACHE_FILE_VERSION) { return false; } - + $this->_source_version = $source_version; $this->_browsers = $browsers; $this->_userAgents = $userAgents; @@ -737,7 +738,7 @@ protected function _loadCache($cache_file) $this->_properties = $properties; $this->_cacheLoaded = true; - + return true; } @@ -768,9 +769,9 @@ protected function _buildCache() /** * Lazy getter for the stream context resource. - * + * * @param bool $recreate - * + * * @return resource */ protected function _getStreamContext($recreate = false) @@ -786,10 +787,10 @@ protected function _getStreamContext($recreate = false) * Updates the local copy of the ini file (by version checking) and adapts * his syntax to the PHP ini parser * - * @param string $url the url of the remote server - * @param string $path the path of the ini file to update + * @param string $url the url of the remote server + * @param string $path the path of the ini file to update * @throws Exception - * @return bool if the ini file was updated + * @return bool if the ini file was updated */ protected function _getRemoteIniFile($url, $path) { @@ -814,7 +815,6 @@ protected function _getRemoteIniFile($url, $path) // Get updated .ini file $browscap = $this->_getRemoteData($url); - $browscap = explode("\n", $browscap); $pattern = self::REGEX_DELIMITER @@ -823,7 +823,6 @@ protected function _getRemoteIniFile($url, $path) . ')="?([^"]*)"?$' . self::REGEX_DELIMITER; - // Ok, lets read the file $content = ''; foreach ($browscap as $subject) { @@ -844,7 +843,7 @@ protected function _getRemoteIniFile($url, $path) * Gets the remote ini file update timestamp * * @throws Exception - * @return int the remote modification timestamp + * @return int the remote modification timestamp */ protected function _getRemoteMTime() { @@ -862,7 +861,7 @@ protected function _getRemoteMTime() * Gets the local ini file update timestamp * * @throws Exception - * @return int the local modification timestamp + * @return int the local modification timestamp */ protected function _getLocalMTime() { @@ -879,7 +878,7 @@ protected function _getLocalMTime() * var_export one as the internal PHP function does not strip whitespace or * convert strings to numbers. * - * @param array $array the array to parse and convert + * @param array $array the array to parse and convert * @return string the array parsed into a PHP string */ protected function _array2string($array) @@ -905,7 +904,7 @@ protected function _array2string($array) $strings[] = $key . $value; } - + return "array(\n" . implode(",\n", $strings) . "\n)"; } @@ -938,9 +937,9 @@ protected function _getUpdateMethod() /** * Retrieve the data identified by the URL * - * @param string $url the url of the data + * @param string $url the url of the data * @throws Exception - * @return string the retrieved data + * @return string the retrieved data */ protected function _getRemoteData($url) { @@ -1018,7 +1017,7 @@ protected function _getRemoteData($url) case false: throw new Exception('Your server can\'t connect to external resources. Please update the file manually.'); } - + return ''; } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 531b5d9..0c15a7f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -26,7 +26,7 @@ * THE SOFTWARE. * * @package Browscap - * @author Vítor Brandão + * @author Vítor Brandão * @copyright Copyright (c) 2006-2012 Jonathan Stoppani * @version 1.0 * @license http://www.opensource.org/licenses/MIT MIT License @@ -35,11 +35,11 @@ require_once __DIR__.'/phpbrowscap/TestCase.php'; -spl_autoload_register(function($class) -{ +spl_autoload_register(function ($class) { $file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php'; if (file_exists($file)) { require $file; + return true; } }); diff --git a/tests/compare/compareWithOriginal.php b/tests/compare/compareWithOriginal.php index 044a0ad..a7b2d9c 100644 --- a/tests/compare/compareWithOriginal.php +++ b/tests/compare/compareWithOriginal.php @@ -15,12 +15,12 @@ class compareWithOriginal { public static $base_dir; - + /** * @var Browscap */ protected $browscap; - + protected $warnings = array( 'Mozilla/5.0 (compatible; MSIE 7.0; MSIE 6.0; ScanAlert; +http://www.scanalert.com/bot.jsp) Firefox/2.0.0.3', 'Automated Browscap.ini Updater. To report issues contact us at+http://www.skycomp.ca', @@ -60,14 +60,14 @@ class compareWithOriginal 'SosospiderZ(+http://help.soso.com/webspider.htm)', 'UniversalFeedParser/4. +http://feedparser.org/', 'UniversalFeedParser/4.XY +http://feedparser.org/', - + 'Gigabot', 'Lycos', 'Nutch', 'Research Projects', 'BlackBerry', 'Sleipnir', - + 'DefaultProperties', 'Ask', 'Baidu', @@ -452,109 +452,99 @@ class compareWithOriginal 'IE 8.0', 'IE 9.0' ); - + protected $browscap_ini_path; - + protected $user_agents = array(); - + protected $properties = array(); - + public function __construct() { $this->browscap = new Browscap(self::$base_dir . 'cache/'); - + $this->browscap_ini_path = ini_get('browscap'); - + $this->browscap->localFile = $this->browscap_ini_path; $this->browscap->updateMethod = Browscap::UPDATE_LOCAL; - + $this->getUserAgents(); - + $this->checkProperties(); - + $this->runTest(); } - + protected function runTest() { echo "\n"; - + $errors_count = 0; $warnings_count = 0; $lib_time = 0; $lib_max_time = 0; $bc_time = 0; $bc_max_time = 0; - - foreach ($this->user_agents as $i => $user_agent) - { + + foreach ($this->user_agents as $i => $user_agent) { $t = microtime(true); $lib_result = get_browser($user_agent); $ct = microtime(true) - $t; $lib_time += $ct; $lib_max_time = max($lib_max_time, $ct); - + $t = microtime(true); $bc_result = $this->browscap->getBrowser($user_agent); $ct = microtime(true) - $t; $bc_time += $ct; $bc_max_time = max($bc_max_time, $ct); - + $errors = array(); - - if ($user_agent == Browscap::BROWSCAP_VERSION_KEY) - { - if ($this->browscap->getSourceVersion() != $lib_result->version) - { + + if ($user_agent == Browscap::BROWSCAP_VERSION_KEY) { + if ($this->browscap->getSourceVersion() != $lib_result->version) { $errors[] = "Source file version incorrect: {$lib_result->version} != {$this->browscap->getSourceVersion()}"; } - } - else foreach ($this->properties as $bc_prop => $lib_prop) - { + } else { foreach ($this->properties as $bc_prop => $lib_prop) $lib_value = $lib_result->{$lib_prop}; - + $bc_value = $bc_result->{$bc_prop}; - - if ($lib_value != $bc_value) - { + + if ($lib_value != $bc_value) { $errors[] = "$bc_prop: $lib_value != $bc_value"; } } - - if ($errors && in_array($user_agent, $this->warnings)) - { + + if ($errors && in_array($user_agent, $this->warnings)) { $warnings_count++; - + echo "get_browser() error fixed for '$user_agent'\n\n"; - } - elseif ($errors) - { + } elseif ($errors) { $errors_count++; - + $errors[] = "regex: '{$lib_result->browser_name_regex}' vs '{$bc_result->browser_name_regex}'"; - + echo "Errors for '$user_agent'\n" . implode("\n", $errors) . "\n\n"; } - - if ($i % 500 == 0 && $i != 0) - { + + if ($i % 500 == 0 && $i != 0) { $this->printReport($i, $errors_count, $warnings_count, $lib_time, $lib_max_time, $bc_time, $bc_max_time); } } - + $this->printReport($i, $errors_count, $warnings_count, $lib_time, $lib_max_time, $bc_time, $bc_max_time); } - + protected function printReport($i, $errors_count, $warnings_count, $lib_time, $lib_max_time, $bc_time, $bc_max_time) { $lt = number_format($lib_time, 2) . ' sec'; $ltpp = number_format($lib_time / $i * 1000, 1) . ' ms / item'; $ltm = number_format($lib_max_time * 1000, 1) . ' ms'; - + $bt = number_format($bc_time, 2) . ' sec'; $btpp = number_format($bc_time / $i * 1000, 1) . ' ms / item'; $btm = number_format($bc_max_time * 1000, 1) . ' ms'; - + echo "$i: report\n"; echo "$errors_count errors\n"; echo "$warnings_count get_browser() errors fixed\n"; @@ -562,79 +552,66 @@ protected function printReport($i, $errors_count, $warnings_count, $lib_time, $l echo "bc time: $bt ($btpp, max $btm)\n"; echo "\n"; } - + protected function checkProperties() { $lib_properties = get_object_vars(get_browser('x')); - + $bc_properties = get_object_vars($this->browscap->getBrowser('x')); - - foreach (array_keys($bc_properties) as $bc_prop) - { - if ('browser_name' == $bc_prop) - { + + foreach (array_keys($bc_properties) as $bc_prop) { + if ('browser_name' == $bc_prop) { continue; } - - if (!isset($lib_properties[strtolower($bc_prop)])) - { + + if (!isset($lib_properties[strtolower($bc_prop)])) { throw new Exception("Property `$bc_prop` from Browscap doesn't match anything in get_browser."); } - - if ('browser_name_regex' != $bc_prop) - { + + if ('browser_name_regex' != $bc_prop) { $this->properties[$bc_prop] = strtolower($bc_prop); } - + unset($lib_properties[strtolower($bc_prop)]); } - + unset($lib_properties['renderingengine_description']); - - if (!empty($lib_properties)) - { + + if (!empty($lib_properties)) { throw new Exception('There are ' . count($lib_properties) . '(' . implode(', ', array_keys($lib_properties)) . ') properties in get_browser that do not match those in Browscap.'); } } - + protected function getUserAgents() { - if (empty($this->browscap_ini_path)) - { + if (empty($this->browscap_ini_path)) { throw new Exception("You have to have php.ini 'browscap' directive set to run this test."); } - - if (!is_file($this->browscap_ini_path)) - { + + if (!is_file($this->browscap_ini_path)) { throw new Exception("There is no browscap file at {$this->browscap_ini_path} location."); } - - if (version_compare(PHP_VERSION, '5.3.0', '>=')) - { + + if (version_compare(PHP_VERSION, '5.3.0', '>=')) { $browscap_data = parse_ini_file($this->browscap_ini_path, true, INI_SCANNER_RAW); - } - else - { + } else { $browscap_data = parse_ini_file($this->browscap_ini_path, true); } - + $browscap_data = array_keys($browscap_data); - + $this->user_agents = explode("\n", file_get_contents('user-agent-examples.txt')); - + $this->user_agents[] = uniqid('Fake User Agent ', true); - - foreach ($browscap_data as $pattern) - { + + foreach ($browscap_data as $pattern) { $this->user_agents[] = str_replace(array('?', '*'), array('Z', 'XY'), $pattern); - - if (false !== strpos($pattern, '*')) - { + + if (false !== strpos($pattern, '*')) { $this->user_agents[] = str_replace(array('?', '*'), array('Z', ''), $pattern); } } - + echo number_format(count($this->user_agents)) . " possible user agents\n"; } } - diff --git a/tests/phpbrowscap/BrowscapTest.php b/tests/phpbrowscap/BrowscapTest.php index 279c273..b9bafbc 100644 --- a/tests/phpbrowscap/BrowscapTest.php +++ b/tests/phpbrowscap/BrowscapTest.php @@ -2,8 +2,6 @@ namespace phpbrowscap; -use phpbrowscap\Browscap; - /** * Browscap.ini parsing class with caching and update capabilities * @@ -30,7 +28,7 @@ * THE SOFTWARE. * * @package Browscap - * @author Vítor Brandão + * @author Vítor Brandão * @copyright Copyright (c) 2006-2012 Jonathan Stoppani * @version 1.0 * @license http://www.opensource.org/licenses/MIT MIT License @@ -106,4 +104,4 @@ public function getStreamContext($recreate = false) { return $this->_getStreamContext($recreate); } -} \ No newline at end of file +} diff --git a/tests/phpbrowscap/TestCase.php b/tests/phpbrowscap/TestCase.php index 3d5e358..c5c84eb 100644 --- a/tests/phpbrowscap/TestCase.php +++ b/tests/phpbrowscap/TestCase.php @@ -2,8 +2,6 @@ namespace phpbrowscap; -use phpbrowscap\Browscap; - /** * Browscap.ini parsing class with caching and update capabilities * @@ -30,7 +28,7 @@ * THE SOFTWARE. * * @package Browscap - * @author Vítor Brandão + * @author Vítor Brandão * @copyright Copyright (c) 2006-2012 Jonathan Stoppani * @version 1.0 * @license http://www.opensource.org/licenses/MIT MIT License