From 0cdd5ed60b7593c44c7fa0174eb645aea45d4c6c Mon Sep 17 00:00:00 2001 From: Mark Beech Date: Tue, 22 Nov 2016 22:05:38 +0000 Subject: [PATCH 1/4] Transition to PHP 7 --- .styleci.yml | 65 +++++++++++++++++++++++++++++++ .travis.yml | 5 --- composer.json | 2 +- src/CrawlerDetect.php | 53 +++++++++++++------------ src/Fixtures/AbstractProvider.php | 12 +++++- src/Fixtures/Crawlers.php | 4 +- src/Fixtures/Exclusions.php | 4 +- src/Fixtures/Headers.php | 7 ++-- 8 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..d9ab7b1 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1,65 @@ +preset: none +enabled: + - array_element_white_space_after_comma + - blankline_after_open_tag + - braces + - concat_without_spaces + - double_arrow_multiline_whitespaces + - duplicate_semicolon + - elseif + - empty_return + - encoding + - eof_ending + - function_call_space + - function_declaration + - include + - indentation + - join_function + - line_after_namespace + - linefeed + - list_commas + - logical_not_operators_with_successor_space + - lowercase_constants + - lowercase_keywords + - method_argument_space + - multiline_array_trailing_comma + - multiline_spaces_before_semicolon + - multiple_use + - namespace_no_leading_whitespace + - no_blank_lines_after_class_opening + - no_empty_lines_after_phpdocs + - no_extra_consecutive_blank_lines + - object_operator + - operators_spaces + - ordered_use + - parenthesis + - phpdoc_indent + - phpdoc_inline_tag + - phpdoc_no_access + - phpdoc_no_package + - phpdoc_scalar + - phpdoc_short_description + - phpdoc_to_comment + - phpdoc_trim + - phpdoc_type_to_var + - phpdoc_var_without_name + - remove_leading_slash_use + - return + - self_accessor + - short_array_syntax + - short_echo_tag + - short_tag + - single_array_no_trailing_comma + - single_blank_line_before_namespace + - single_line_after_imports + - single_quote + - spaces_before_semicolon + - spaces_cast + - standardize_not_equal + - ternary_spaces + - trailing_spaces + - trim_array_spaces + - unalign_equals + - unary_operators_spaces + - visibility + - whitespacy_lines \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a740bac..107dfd6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,13 +2,8 @@ language: php matrix: include: - - php: 5.3 - - php: 5.4 - - php: 5.5 - - php: 5.6 - php: 7.0 - php: 7.1 - - php: hhvm - php: nightly allow_failures: - php: nightly diff --git a/composer.json b/composer.json index 90948e0..5db0f53 100755 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ } ], "require": { - "php": ">=5.3.0" + "php": "^7.0" }, "require-dev": { "phpunit/phpunit": "*" diff --git a/src/CrawlerDetect.php b/src/CrawlerDetect.php index f4984f3..8d45dd7 100644 --- a/src/CrawlerDetect.php +++ b/src/CrawlerDetect.php @@ -11,9 +11,7 @@ namespace Jaybizzle\CrawlerDetect; -use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; -use Jaybizzle\CrawlerDetect\Fixtures\Exclusions; -use Jaybizzle\CrawlerDetect\Fixtures\Headers; +use Jaybizzle\CrawlerDetect\Fixtures\{Crawlers, Exclusions, Headers}; class CrawlerDetect { @@ -29,14 +27,14 @@ class CrawlerDetect * * @var array */ - protected $httpHeaders = array(); + protected $httpHeaders = []; /** * Store regex matches. * * @var array */ - protected $matches = array(); + protected $matches = []; /** * Crawlers object. @@ -60,9 +58,12 @@ class CrawlerDetect protected $uaHttpHeaders; /** - * Class constructor. + * Constuctor. + * + * @param array|null $headers + * @param string|null $userAgent */ - public function __construct(array $headers = null, $userAgent = null) + public function __construct(array $headers = null, string $userAgent = null) { $this->crawlers = new Crawlers(); $this->exclusions = new Exclusions(); @@ -77,16 +78,18 @@ public function __construct(array $headers = null, $userAgent = null) * * @param array $httpHeaders */ - public function setHttpHeaders($httpHeaders = null) + public function setHttpHeaders(array $httpHeaders = null) { - // use global _SERVER if $httpHeaders aren't defined + // Use global _SERVER if $httpHeaders aren't defined. if (!is_array($httpHeaders) || !count($httpHeaders)) { $httpHeaders = $_SERVER; } - // clear existing headers - $this->httpHeaders = array(); - // Only save HTTP headers. In PHP land, that means only _SERVER vars that - // start with HTTP_. + + // Clear existing headers. + $this->httpHeaders = []; + + // Only save HTTP headers. In PHP land, that means + // only _SERVER vars that start with HTTP_. foreach ($httpHeaders as $key => $value) { if (substr($key, 0, 5) === 'HTTP_') { $this->httpHeaders[$key] = $value; @@ -99,7 +102,7 @@ public function setHttpHeaders($httpHeaders = null) * * @return array */ - public function getUaHttpHeaders() + public function getUaHttpHeaders(): array { return $this->uaHttpHeaders->getAll(); } @@ -109,10 +112,10 @@ public function getUaHttpHeaders() * * @param string $userAgent */ - public function setUserAgent($userAgent = null) + public function setUserAgent(string $userAgent = null) { if (false === empty($userAgent)) { - return $this->userAgent = $userAgent; + $this->userAgent = $userAgent; } else { $this->userAgent = null; foreach ($this->getUaHttpHeaders() as $altHeader) { @@ -121,7 +124,7 @@ public function setUserAgent($userAgent = null) } } - return $this->userAgent = (!empty($this->userAgent) ? trim($this->userAgent) : null); + $this->userAgent = !empty($this->userAgent) ? trim($this->userAgent) : null; } } @@ -130,7 +133,7 @@ public function setUserAgent($userAgent = null) * * @return string */ - public function getRegex() + public function getRegex(): string { return '('.implode('|', $this->crawlers->getAll()).')'; } @@ -140,7 +143,7 @@ public function getRegex() * * @return string */ - public function getExclusions() + public function getExclusions(): string { return '('.implode('|', $this->exclusions->getAll()).')'; } @@ -152,18 +155,18 @@ public function getExclusions() * * @return bool */ - public function isCrawler($userAgent = null) + public function isCrawler(string $userAgent = null): bool { - $agent = is_null($userAgent) ? $this->userAgent : $userAgent; + $agent = $userAgent ?: $this->userAgent; $agent = preg_replace('/'.$this->getExclusions().'/i', '', $agent); if (strlen(trim($agent)) == 0) { return false; - } else { - $result = preg_match('/'.$this->getRegex().'/i', trim($agent), $matches); } + $result = preg_match('/'.$this->getRegex().'/i', trim($agent), $matches); + if ($matches) { $this->matches = $matches; } @@ -174,10 +177,10 @@ public function isCrawler($userAgent = null) /** * Return the matches. * - * @return string + * @return string|null */ public function getMatches() { - return isset($this->matches[0]) ? $this->matches[0] : null; + return $this->matches[0] ?? null; } } diff --git a/src/Fixtures/AbstractProvider.php b/src/Fixtures/AbstractProvider.php index 60d6324..3bfca10 100644 --- a/src/Fixtures/AbstractProvider.php +++ b/src/Fixtures/AbstractProvider.php @@ -13,9 +13,19 @@ abstract class AbstractProvider { + /** + * The data set. + * + * @var array + */ protected $data; - public function getAll() + /** + * Return the data set. + * + * @return array + */ + public function getAll(): array { return $this->data; } diff --git a/src/Fixtures/Crawlers.php b/src/Fixtures/Crawlers.php index 8ccf28b..1b9e9d5 100644 --- a/src/Fixtures/Crawlers.php +++ b/src/Fixtures/Crawlers.php @@ -18,7 +18,7 @@ class Crawlers extends AbstractProvider * * @var array */ - protected $data = array( + protected $data = [ '.*Java.*outbrain', '008\/', '192.comAgent', @@ -712,5 +712,5 @@ class Crawlers extends AbstractProvider 'ZnajdzFoto', 'ZyBorg', '[a-z0-9\-_]*((? Date: Tue, 22 Nov 2016 22:05:47 +0000 Subject: [PATCH 2/4] Applied fixes from StyleCI --- src/CrawlerDetect.php | 10 ++++++---- src/Fixtures/AbstractProvider.php | 10 +++++----- tests/bootstrap.php | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/CrawlerDetect.php b/src/CrawlerDetect.php index 8d45dd7..d99244e 100644 --- a/src/CrawlerDetect.php +++ b/src/CrawlerDetect.php @@ -11,7 +11,9 @@ namespace Jaybizzle\CrawlerDetect; -use Jaybizzle\CrawlerDetect\Fixtures\{Crawlers, Exclusions, Headers}; +use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; +use Jaybizzle\CrawlerDetect\Fixtures\Exclusions; +use Jaybizzle\CrawlerDetect\Fixtures\Headers; class CrawlerDetect { @@ -81,7 +83,7 @@ public function __construct(array $headers = null, string $userAgent = null) public function setHttpHeaders(array $httpHeaders = null) { // Use global _SERVER if $httpHeaders aren't defined. - if (!is_array($httpHeaders) || !count($httpHeaders)) { + if (! is_array($httpHeaders) || ! count($httpHeaders)) { $httpHeaders = $_SERVER; } @@ -115,7 +117,7 @@ public function getUaHttpHeaders(): array public function setUserAgent(string $userAgent = null) { if (false === empty($userAgent)) { - $this->userAgent = $userAgent; + $this->userAgent = $userAgent; } else { $this->userAgent = null; foreach ($this->getUaHttpHeaders() as $altHeader) { @@ -124,7 +126,7 @@ public function setUserAgent(string $userAgent = null) } } - $this->userAgent = !empty($this->userAgent) ? trim($this->userAgent) : null; + $this->userAgent = ! empty($this->userAgent) ? trim($this->userAgent) : null; } } diff --git a/src/Fixtures/AbstractProvider.php b/src/Fixtures/AbstractProvider.php index 3bfca10..30ded51 100644 --- a/src/Fixtures/AbstractProvider.php +++ b/src/Fixtures/AbstractProvider.php @@ -13,11 +13,11 @@ abstract class AbstractProvider { - /** - * The data set. - * - * @var array - */ + /** + * The data set. + * + * @var array + */ protected $data; /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index babed5b..27dcc71 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,7 +11,7 @@ $dot = dirname(__FILE__); -if (!file_exists($composer = dirname($dot).'/vendor/autoload.php')) { +if (! file_exists($composer = dirname($dot).'/vendor/autoload.php')) { throw new RuntimeException("Please run 'composer install' first to set up autoloading. $composer"); } /** @var \Composer\Autoload\ClassLoader $autoloader */ From 7ba3aab4bc38500ef49a0626830438a762afc137 Mon Sep 17 00:00:00 2001 From: Mark Beech Date: Tue, 22 Nov 2016 22:09:37 +0000 Subject: [PATCH 3/4] Aloow combined use statements --- .styleci.yml | 1 - src/CrawlerDetect.php | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index d9ab7b1..17990f2 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -24,7 +24,6 @@ enabled: - method_argument_space - multiline_array_trailing_comma - multiline_spaces_before_semicolon - - multiple_use - namespace_no_leading_whitespace - no_blank_lines_after_class_opening - no_empty_lines_after_phpdocs diff --git a/src/CrawlerDetect.php b/src/CrawlerDetect.php index d99244e..3a5fda8 100644 --- a/src/CrawlerDetect.php +++ b/src/CrawlerDetect.php @@ -11,9 +11,7 @@ namespace Jaybizzle\CrawlerDetect; -use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; -use Jaybizzle\CrawlerDetect\Fixtures\Exclusions; -use Jaybizzle\CrawlerDetect\Fixtures\Headers; +use Jaybizzle\CrawlerDetect\Fixtures\{Crawlers, Exclusions, Headers}; class CrawlerDetect { From a24b1a4c3657aae56b4658a745b03f43b2d0ed04 Mon Sep 17 00:00:00 2001 From: Mark Beech Date: Mon, 5 Dec 2016 19:50:10 +0000 Subject: [PATCH 4/4] News bots --- src/Fixtures/Crawlers.php | 4 ++++ tests/crawlers.txt | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/Fixtures/Crawlers.php b/src/Fixtures/Crawlers.php index a4fd474..49c42fd 100644 --- a/src/Fixtures/Crawlers.php +++ b/src/Fixtures/Crawlers.php @@ -211,6 +211,7 @@ class Crawlers extends AbstractProvider 'FeedChecker', 'Feedly', 'Feedspot', + 'Feedwind\/[0-9]', 'feeltiptop', 'Fetch API', 'Fetch\/[0-9]', @@ -329,6 +330,7 @@ class Crawlers extends AbstractProvider 'internetVista monitor', 'IODC', 'IOI', + 'IPS\/[0-9]', 'ips-agent', 'iqdb\/', 'Irokez', @@ -392,6 +394,7 @@ class Crawlers extends AbstractProvider 'Microsoft Windows Network Diagnostics', 'Mindjet', 'Miniflux', + 'mixdata dot com', 'Mnogosearch', 'mogimogi', 'Mojolicious (Perl)', @@ -448,6 +451,7 @@ class Crawlers extends AbstractProvider 'OrgProbe\/[0-9]', 'ow\.ly', 'ownCloud News', + 'OxfordCloudService\/[0-9]', 'Page Analyzer', 'Page Valet', 'page2rss', diff --git a/tests/crawlers.txt b/tests/crawlers.txt index 75139dd..91d2ffb 100644 --- a/tests/crawlers.txt +++ b/tests/crawlers.txt @@ -2404,3 +2404,8 @@ gvfs/1.20.3 request.js Microsoft URL Control - 6.01.9782 git/2.1.4 +IPS/1.0 +OxfordCloudService/1.0 +Mozilla/5.0 (compatible; informatique at mixdata dot com/0.6; +http://t.co/GSRLLKex24; informatique at mixdata dot com) +Mozilla/5.0 (compatible; Feedwind/2.0; +http://feed.mikle.com/support/description/) +Mozilla/5.0 (compatible; Feedwind/3.0; +http://feed.mikle.com/support/description/)