From ad8d13bdac97e6dc276ddf150b7e203dee302251 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Fri, 26 Jul 2019 19:10:04 +0200 Subject: [PATCH] more codestyle improvements --- Cache/{Cache.php => CacheInterface.php} | 20 +- Cache/DoctrineBridge.php | 14 +- Cache/PSR16Bridge.php | 20 +- Cache/PSR6Bridge.php | 12 +- Cache/StaticCache.php | 27 +- DeviceDetector.php | 505 ++++++++++-------- ...rserAbstract.php => AbstractBotParser.php} | 6 +- ...{ParserAbstract.php => AbstractParser.php} | 140 ++--- Parser/Bot.php | 4 +- ...rAbstract.php => AbstractClientParser.php} | 19 +- Parser/Client/Browser.php | 48 +- Parser/Client/Browser/Engine.php | 22 +- Parser/Client/Browser/Engine/Version.php | 15 +- Parser/Client/FeedReader.php | 13 +- Parser/Client/Library.php | 15 +- Parser/Client/MediaPlayer.php | 13 +- Parser/Client/MobileApp.php | 13 +- Parser/Client/PIM.php | 13 +- ...rAbstract.php => AbstractDeviceParser.php} | 77 ++- Parser/Device/Camera.php | 16 +- Parser/Device/CarBrowser.php | 16 +- Parser/Device/Console.php | 18 +- Parser/Device/HbbTv.php | 17 +- Parser/Device/Mobile.php | 13 +- Parser/Device/PortableMediaPlayer.php | 15 +- Parser/OperatingSystem.php | 74 +-- Parser/VendorFragment.php | 28 +- Tests/Cache/PSR16CacheTest.php | 11 +- Tests/Cache/PSR6CacheTest.php | 11 +- Tests/Cache/StaticCacheTest.php | 2 +- Tests/DeviceDetectorTest.php | 22 +- Tests/Parser/BotTest.php | 2 +- Tests/Parser/Client/BrowserTest.php | 2 +- Tests/Parser/Client/FeedReaderTest.php | 2 +- Tests/Parser/Client/LibraryTest.php | 2 +- Tests/Parser/Client/MediaPlayerTest.php | 2 +- Tests/Parser/Client/MobileAppTest.php | 2 +- Tests/Parser/Client/PIMTest.php | 2 +- Tests/Parser/Device/CameraTest.php | 2 +- Tests/Parser/Device/CarBrowserTest.php | 2 +- Tests/Parser/Device/ConsoleTest.php | 2 +- .../Device/DeviceParserAbstractTest.php | 14 +- Tests/Parser/Device/HbbTvTest.php | 2 +- Tests/Parser/OperatingSystemTest.php | 2 +- Tests/Parser/VendorFragmentTest.php | 2 +- Yaml/{Parser.php => ParserInterface.php} | 5 +- Yaml/Pecl.php | 6 +- Yaml/Spyc.php | 5 +- Yaml/Symfony.php | 5 +- misc/file-test.php | 4 +- misc/readme-report.php | 4 +- misc/sortTestFixtureFiles.php | 4 +- misc/statistics.php | 6 +- misc/test.php | 6 +- phpstan.neon | 3 + regexes/bots.yml | 2 +- regexes/client/browser_engine.yml | 2 +- regexes/client/browsers.yml | 2 +- regexes/client/feed_readers.yml | 2 +- regexes/client/libraries.yml | 2 +- regexes/client/mediaplayers.yml | 2 +- regexes/client/mobile_apps.yml | 2 +- regexes/client/pim.yml | 2 +- regexes/device/cameras.yml | 2 +- regexes/device/car_browsers.yml | 2 +- regexes/device/consoles.yml | 2 +- regexes/device/mobiles.yml | 2 +- regexes/device/portable_media_player.yml | 2 +- regexes/device/televisions.yml | 2 +- regexes/oss.yml | 2 +- regexes/vendorfragments.yml | 2 +- 71 files changed, 820 insertions(+), 539 deletions(-) rename Cache/{Cache.php => CacheInterface.php} (65%) rename Parser/{BotParserAbstract.php => AbstractBotParser.php} (77%) rename Parser/{ParserAbstract.php => AbstractParser.php} (87%) rename Parser/Client/{ClientParserAbstract.php => AbstractClientParser.php} (86%) rename Parser/Device/{DeviceParserAbstract.php => AbstractDeviceParser.php} (91%) rename Yaml/{Parser.php => ParserInterface.php} (87%) diff --git a/Cache/Cache.php b/Cache/CacheInterface.php similarity index 65% rename from Cache/Cache.php rename to Cache/CacheInterface.php index 7bf20e4a76..ae3fde746c 100644 --- a/Cache/Cache.php +++ b/Cache/CacheInterface.php @@ -3,38 +3,44 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org + * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ + namespace DeviceDetector\Cache; -interface Cache +interface CacheInterface { /** * @param string $id + * * @return mixed */ - public function fetch($id); + public function fetch(string $id); /** - * @param $id + * @param string $id + * * @return bool */ - public function contains($id): bool; + public function contains(string $id): bool; /** * @param string $id * @param mixed $data * @param int $lifeTime + * * @return bool */ - public function save($id, $data, $lifeTime = 0): bool; + public function save(string $id, $data, int $lifeTime = 0): bool; /** * @param string $id + * * @return bool */ - public function delete($id): bool; + public function delete(string $id): bool; /** * @return bool diff --git a/Cache/DoctrineBridge.php b/Cache/DoctrineBridge.php index 96e3e160d9..4d5b320ae2 100644 --- a/Cache/DoctrineBridge.php +++ b/Cache/DoctrineBridge.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -12,12 +12,12 @@ use Doctrine\Common\Cache\CacheProvider ; -class DoctrineBridge implements Cache +class DoctrineBridge implements CacheInterface { /** * @var CacheProvider */ - private $pool; + private $cache; /** * @param CacheProvider $cache @@ -30,7 +30,7 @@ public function __construct(CacheProvider $cache) /** * @inheritDoc */ - public function fetch($id) + public function fetch(string $id) { return $this->cache->fetch($id); } @@ -38,7 +38,7 @@ public function fetch($id) /** * @inheritDoc */ - public function contains($id): bool + public function contains(string $id): bool { return $this->cache->contains($id); } @@ -46,7 +46,7 @@ public function contains($id): bool /** * @inheritDoc */ - public function save($id, $data, $lifeTime = 0): bool + public function save(string $id, $data, int $lifeTime = 0): bool { return $this->cache->save($id, $data, $lifeTime); } @@ -54,7 +54,7 @@ public function save($id, $data, $lifeTime = 0): bool /** * @inheritDoc */ - public function delete($id): bool + public function delete(string $id): bool { return $this->cache->delete($id); } diff --git a/Cache/PSR16Bridge.php b/Cache/PSR16Bridge.php index f9a97b55bb..2ce69dfb0b 100644 --- a/Cache/PSR16Bridge.php +++ b/Cache/PSR16Bridge.php @@ -3,27 +3,27 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Cache; -use Psr\SimpleCache\CacheInterface; +use Psr\SimpleCache\CacheInterface as PsrCacheInterface; -class PSR16Bridge implements Cache +class PSR16Bridge implements CacheInterface { /** - * @var CacheInterface + * @var PsrCacheInterface */ private $cache; /** * PSR16Bridge constructor. - * @param CacheInterface $cache + * @param PsrCacheInterface $cache */ - public function __construct(CacheInterface $cache) + public function __construct(PsrCacheInterface $cache) { $this->cache = $cache; } @@ -31,7 +31,7 @@ public function __construct(CacheInterface $cache) /** * @inheritDoc */ - public function fetch($id) + public function fetch(string $id) { return $this->cache->get($id, false); } @@ -39,7 +39,7 @@ public function fetch($id) /** * @inheritDoc */ - public function contains($id): bool + public function contains(string $id): bool { return $this->cache->has($id); } @@ -47,7 +47,7 @@ public function contains($id): bool /** * @inheritDoc */ - public function save($id, $data, $lifeTime = 0): bool + public function save(string $id, $data, int $lifeTime = 0): bool { return $this->cache->set($id, $data, func_num_args() < 3 ? null : $lifeTime); } @@ -55,7 +55,7 @@ public function save($id, $data, $lifeTime = 0): bool /** * @inheritDoc */ - public function delete($id): bool + public function delete(string $id): bool { return $this->cache->delete($id); } diff --git a/Cache/PSR6Bridge.php b/Cache/PSR6Bridge.php index 3a4c41301e..51c3a1b5a8 100644 --- a/Cache/PSR6Bridge.php +++ b/Cache/PSR6Bridge.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -12,7 +12,7 @@ use Psr\Cache\CacheItemPoolInterface; -class PSR6Bridge implements Cache +class PSR6Bridge implements CacheInterface { /** * @var CacheItemPoolInterface @@ -31,7 +31,7 @@ public function __construct(CacheItemPoolInterface $pool) /** * @inheritDoc */ - public function fetch($id) + public function fetch(string $id) { $item = $this->pool->getItem($id); @@ -41,7 +41,7 @@ public function fetch($id) /** * @inheritDoc */ - public function contains($id): bool + public function contains(string $id): bool { return $this->pool->hasItem($id); } @@ -49,7 +49,7 @@ public function contains($id): bool /** * @inheritDoc */ - public function save($id, $data, $lifeTime = 0): bool + public function save(string $id, $data, int $lifeTime = 0): bool { $item = $this->pool->getItem($id); $item->set($data); @@ -64,7 +64,7 @@ public function save($id, $data, $lifeTime = 0): bool /** * @inheritDoc */ - public function delete($id): bool + public function delete(string $id): bool { return $this->pool->deleteItem($id); } diff --git a/Cache/StaticCache.php b/Cache/StaticCache.php index ae13f8ff37..a8fb58e87b 100644 --- a/Cache/StaticCache.php +++ b/Cache/StaticCache.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -16,7 +16,7 @@ * Simple Cache that caches in a static property * (Speeds up multiple detections in one request) */ -class StaticCache implements Cache +class StaticCache implements CacheInterface { /** * Holds the static cache data @@ -24,30 +24,45 @@ class StaticCache implements Cache */ protected static $staticCache = []; - public function fetch($id) + /** + * @inheritdoc + */ + public function fetch(string $id) { return $this->contains($id) ? self::$staticCache[$id] : false; } - public function contains($id): bool + /** + * @inheritdoc + */ + public function contains(string $id): bool { return isset(self::$staticCache[$id]) || array_key_exists($id, self::$staticCache); } - public function save($id, $data, $lifeTime = 0): bool + /** + * @inheritdoc + */ + public function save(string $id, $data, int $lifeTime = 0): bool { self::$staticCache[$id] = $data; return true; } - public function delete($id): bool + /** + * @inheritdoc + */ + public function delete(string $id): bool { unset(self::$staticCache[$id]); return true; } + /** + * @inheritdoc + */ public function flushAll(): bool { self::$staticCache = []; diff --git a/DeviceDetector.php b/DeviceDetector.php index 93db59f998..f3f9483844 100644 --- a/DeviceDetector.php +++ b/DeviceDetector.php @@ -3,23 +3,23 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector; -use DeviceDetector\Cache\Cache; +use DeviceDetector\Cache\CacheInterface; use DeviceDetector\Cache\StaticCache; +use DeviceDetector\Parser\AbstractBotParser; use DeviceDetector\Parser\Bot; -use DeviceDetector\Parser\BotParserAbstract; +use DeviceDetector\Parser\Client\AbstractClientParser; use DeviceDetector\Parser\Client\Browser; -use DeviceDetector\Parser\Client\ClientParserAbstract; -use DeviceDetector\Parser\Device\DeviceParserAbstract; +use DeviceDetector\Parser\Device\AbstractDeviceParser; use DeviceDetector\Parser\OperatingSystem; use DeviceDetector\Parser\VendorFragment; -use DeviceDetector\Yaml\Parser as YamlParser; +use DeviceDetector\Yaml\ParserInterface as YamlParser; use DeviceDetector\Yaml\Spyc; /** @@ -50,7 +50,12 @@ class DeviceDetector /** * Current version number of DeviceDetector */ - const VERSION = '3.12.0'; + public const VERSION = '3.12.0'; + + /** + * Constant used as value for unknown browser / os + */ + public const UNKNOWN = 'UNK'; /** * Holds all registered client types @@ -65,11 +70,6 @@ class DeviceDetector */ protected static $desktopOsArray = ['AmigaOS', 'IBM', 'GNU/Linux', 'Mac', 'Unix', 'Windows', 'BeOS', 'Chrome OS']; - /** - * Constant used as value for unknown browser / os - */ - const UNKNOWN = 'UNK'; - /** * Holds the useragent that should be parsed * @var string @@ -132,7 +132,7 @@ class DeviceDetector /** * Holds the cache class used for caching the parsed yml-Files - * @var Cache + * @var CacheInterface */ protected $cache = null; @@ -143,17 +143,17 @@ class DeviceDetector protected $yamlParser = null; /** - * @var array + * @var array */ protected $clientParsers = []; /** - * @var array + * @var array */ protected $deviceParsers = []; /** - * @var array + * @var array */ public $botParsers = []; @@ -169,7 +169,7 @@ class DeviceDetector */ public function __construct(string $userAgent = '') { - if ('' != $userAgent) { + if ('' !== $userAgent) { $this->setUserAgent($userAgent); } @@ -190,17 +190,23 @@ public function __construct(string $userAgent = '') $this->addBotParser(new Bot()); } - public function __call($methodName, $arguments) + /** + * @param string $methodName + * @param array $arguments + * + * @return bool + */ + public function __call(string $methodName, array $arguments): bool { - foreach (DeviceParserAbstract::getAvailableDeviceTypes() as $deviceName => $deviceType) { - if (strtolower($methodName) == 'is' . strtolower(str_replace(' ', '', $deviceName))) { - return $this->getDevice() == $deviceType; + foreach (AbstractDeviceParser::getAvailableDeviceTypes() as $deviceName => $deviceType) { + if (strtolower($methodName) === 'is' . strtolower(str_replace(' ', '', $deviceName))) { + return $this->getDevice() === $deviceType; } } foreach (self::$clientTypes as $client) { - if (strtolower($methodName) == 'is' . strtolower(str_replace(' ', '', $client))) { - return $this->getClient('type') == $client; + if (strtolower($methodName) === 'is' . strtolower(str_replace(' ', '', $client))) { + return $this->getClient('type') === $client; } } @@ -214,26 +220,15 @@ public function __call($methodName, $arguments) */ public function setUserAgent(string $userAgent): void { - if ($this->userAgent != $userAgent) { + if ($this->userAgent !== $userAgent) { $this->reset(); } $this->userAgent = $userAgent; } - protected function reset(): void - { - $this->bot = null; - $this->client = null; - $this->device = null; - $this->os = null; - $this->brand = ''; - $this->model = ''; - $this->parsed = false; - } - /** - * @param ClientParserAbstract|string $parser + * @param AbstractClientParser|string $parser * * @throws \Exception */ @@ -244,7 +239,7 @@ public function addClientParser($parser): void $parser = new $className(); } - if ($parser instanceof ClientParserAbstract) { + if ($parser instanceof AbstractClientParser) { $this->clientParsers[] = $parser; self::$clientTypes[] = $parser->getName(); @@ -255,7 +250,7 @@ public function addClientParser($parser): void } /** - * @return array + * @return array */ public function getClientParsers(): array { @@ -263,7 +258,7 @@ public function getClientParsers(): array } /** - * @param DeviceParserAbstract|string $parser + * @param AbstractDeviceParser|string $parser * * @throws \Exception */ @@ -274,7 +269,7 @@ public function addDeviceParser($parser): void $parser = new $className(); } - if ($parser instanceof DeviceParserAbstract) { + if ($parser instanceof AbstractDeviceParser) { $this->deviceParsers[] = $parser; return; @@ -284,7 +279,7 @@ public function addDeviceParser($parser): void } /** - * @return array + * @return array */ public function getDeviceParsers(): array { @@ -292,15 +287,15 @@ public function getDeviceParsers(): array } /** - * @param BotParserAbstract $parser + * @param AbstractBotParser $parser */ - public function addBotParser(BotParserAbstract $parser): void + public function addBotParser(AbstractBotParser $parser): void { $this->botParsers[] = $parser; } /** - * @return array + * @return array */ public function getBotParsers(): array { @@ -358,44 +353,20 @@ public function isTouchEnabled(): bool } /** - * Returns if the parsed UA contains the 'Android; Tablet;' fragment - * - * @return bool - */ - protected function hasAndroidTableFragment(): bool - { - $regex = 'Android( [\.0-9]+)?; Tablet;'; - - return !!$this->matchUserAgent($regex); - } - - /** - * Returns if the parsed UA contains the 'Android; Mobile;' fragment + * Returns if the parsed UA is detected as a mobile device * * @return bool */ - protected function hasAndroidMobileFragment(): bool - { - $regex = 'Android( [\.0-9]+)?; Mobile;'; - - return !!$this->matchUserAgent($regex); - } - - protected function usesMobileBrowser(): bool - { - return 'browser' === $this->getClient('type') && Browser::isMobileOnlyBrowser($this->getClientAttribute('short_name')); - } - public function isMobile(): bool { // Mobile device types if (!empty($this->device) && in_array($this->device, [ - DeviceParserAbstract::DEVICE_TYPE_FEATURE_PHONE, - DeviceParserAbstract::DEVICE_TYPE_SMARTPHONE, - DeviceParserAbstract::DEVICE_TYPE_TABLET, - DeviceParserAbstract::DEVICE_TYPE_PHABLET, - DeviceParserAbstract::DEVICE_TYPE_CAMERA, - DeviceParserAbstract::DEVICE_TYPE_PORTABLE_MEDIA_PAYER, + AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE, + AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE, + AbstractDeviceParser::DEVICE_TYPE_TABLET, + AbstractDeviceParser::DEVICE_TYPE_PHABLET, + AbstractDeviceParser::DEVICE_TYPE_CAMERA, + AbstractDeviceParser::DEVICE_TYPE_PORTABLE_MEDIA_PAYER, ]) ) { return true; @@ -403,9 +374,9 @@ public function isMobile(): bool // non mobile device types if (!empty($this->device) && in_array($this->device, [ - DeviceParserAbstract::DEVICE_TYPE_TV, - DeviceParserAbstract::DEVICE_TYPE_SMART_DISPLAY, - DeviceParserAbstract::DEVICE_TYPE_CONSOLE, + AbstractDeviceParser::DEVICE_TYPE_TV, + AbstractDeviceParser::DEVICE_TYPE_SMART_DISPLAY, + AbstractDeviceParser::DEVICE_TYPE_CONSOLE, ]) ) { return false; @@ -418,7 +389,7 @@ public function isMobile(): bool $osShort = $this->getOs('short_name'); - if (empty($osShort) || self::UNKNOWN == $osShort) { + if (empty($osShort) || self::UNKNOWN === $osShort) { return false; } @@ -435,9 +406,9 @@ public function isMobile(): bool */ public function isDesktop(): bool { - $osShort = $this->getOs('short_name'); + $osShort = $this->getOsAttribute('short_name'); - if (empty($osShort) || self::UNKNOWN == $osShort) { + if (empty($osShort) || self::UNKNOWN === $osShort) { return false; } @@ -458,31 +429,17 @@ public function isDesktop(): bool * * @param string $attr property to return(optional) * - * @return array|string + * @return array|string|null */ public function getOs(string $attr = '') { - if ('' == $attr) { + if ('' === $attr) { return $this->os; } return $this->getOsAttribute($attr); } - /** - * @param string $attr - * - * @return string - */ - protected function getOsAttribute(string $attr): string - { - if (!isset($this->os[$attr])) { - return self::UNKNOWN; - } - - return $this->os[$attr]; - } - /** * Returns the client data extracted from the parsed UA * @@ -490,35 +447,21 @@ protected function getOsAttribute(string $attr): string * * @param string $attr property to return(optional) * - * @return array|string + * @return array|string|null */ public function getClient(string $attr = '') { - if ('' == $attr) { + if ('' === $attr) { return $this->client; } return $this->getClientAttribute($attr); } - /** - * @param string $attr - * - * @return string - */ - protected function getClientAttribute(string $attr): string - { - if (!isset($this->client[$attr])) { - return self::UNKNOWN; - } - - return $this->client[$attr]; - } - /** * Returns the device type extracted from the parsed UA * - * @see DeviceParserAbstract::$deviceTypes for available device types + * @see AbstractDeviceParser::$deviceTypes for available device types * * @return int|null */ @@ -530,14 +473,14 @@ public function getDevice(): ?int /** * Returns the device type extracted from the parsed UA * - * @see DeviceParserAbstract::$deviceTypes for available device types + * @see AbstractDeviceParser::$deviceTypes for available device types * * @return string */ public function getDeviceName(): string { if (null !== $this->getDevice()) { - return DeviceParserAbstract::getDeviceName($this->getDevice()); + return AbstractDeviceParser::getDeviceName($this->getDevice()); } return ''; @@ -564,7 +507,7 @@ public function getBrand(): string */ public function getBrandName(): string { - return DeviceParserAbstract::getFullName($this->getBrand()); + return AbstractDeviceParser::getFullName($this->getBrand()); } /** @@ -641,6 +584,163 @@ public function parse(): void $this->parseDevice(); } + /** + * Parses a useragent and returns the detected data + * + * ATTENTION: Use that method only for testing or very small applications + * To get fast results from DeviceDetector you need to make your own implementation, + * that should use one of the caching mechanisms. See README.md for more information. + * + * @internal + * + * @deprecated + * + * @param string $ua UserAgent to parse + * + * @return array + */ + public static function getInfoFromUserAgent(string $ua): array + { + $deviceDetector = new DeviceDetector($ua); + $deviceDetector->parse(); + + if ($deviceDetector->isBot()) { + return [ + 'user_agent' => $deviceDetector->getUserAgent(), + 'bot' => $deviceDetector->getBot(), + ]; + } + + $osFamily = OperatingSystem::getOsFamily($deviceDetector->getOsAttribute('short_name')); + $browserFamily = Browser::getBrowserFamily($deviceDetector->getClientAttribute('short_name')); + + $processed = [ + 'user_agent' => $deviceDetector->getUserAgent(), + 'os' => $deviceDetector->getOs(), + 'client' => $deviceDetector->getClient(), + 'device' => [ + 'type' => $deviceDetector->getDeviceName(), + 'brand' => $deviceDetector->getBrand(), + 'model' => $deviceDetector->getModel(), + ], + 'os_family' => $osFamily ?? 'Unknown', + 'browser_family' => $browserFamily ?? 'Unknown', + ]; + + return $processed; + } + + /** + * Sets the Cache class + * + * @param CacheInterface $cache + */ + public function setCache(CacheInterface $cache): void + { + $this->cache = $cache; + } + + /** + * Returns Cache object + * + * @return CacheInterface + */ + public function getCache(): CacheInterface + { + if (!empty($this->cache)) { + return $this->cache; + } + + return new StaticCache(); + } + + /** + * Sets the Yaml Parser class + * + * @param YamlParser $yamlParser + */ + public function setYamlParser(YamlParser $yamlParser): void + { + $this->yamlParser = $yamlParser; + } + + /** + * Returns Yaml Parser object + * + * @return YamlParser + */ + public function getYamlParser(): YamlParser + { + if (!empty($this->yamlParser)) { + return $this->yamlParser; + } + + return new Spyc(); + } + + /** + * @param string $attr + * + * @return string + */ + protected function getClientAttribute(string $attr): string + { + if (!isset($this->client[$attr])) { + return self::UNKNOWN; + } + + return $this->client[$attr]; + } + + /** + * @param string $attr + * + * @return string + */ + protected function getOsAttribute(string $attr): string + { + if (!isset($this->os[$attr])) { + return self::UNKNOWN; + } + + return $this->os[$attr]; + } + + /** + * Returns if the parsed UA contains the 'Android; Tablet;' fragment + * + * @return bool + */ + protected function hasAndroidTableFragment(): bool + { + $regex = 'Android( [\.0-9]+)?; Tablet;'; + + return !!$this->matchUserAgent($regex); + } + + /** + * Returns if the parsed UA contains the 'Android; Mobile;' fragment + * + * @return bool + */ + protected function hasAndroidMobileFragment(): bool + { + $regex = 'Android( [\.0-9]+)?; Mobile;'; + + return !!$this->matchUserAgent($regex); + } + + /** + * Returns if the parsed UA contains usage of a mobile only browser + * + * @return bool + */ + protected function usesMobileBrowser(): bool + { + return 'browser' === $this->getClient('type') + && Browser::isMobileOnlyBrowser($this->getClientAttribute('short_name')); + } + /** * Parses the UA for bot information using the Bot parser */ @@ -673,7 +773,9 @@ protected function parseBot(): void } } - + /** + * Tries to detected the client (e.g. browser, mobile app, ...) + */ protected function parseClient(): void { $parsers = $this->getClientParsers(); @@ -692,6 +794,9 @@ protected function parseClient(): void } } + /** + * Tries to detected the device type, model and brand + */ protected function parseDevice(): void { $parsers = $this->getDeviceParsers(); @@ -737,26 +842,30 @@ protected function parseDevice(): void * If it is present the device should be a smartphone, otherwise it's a tablet * See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent */ - if (is_null($this->device) && 'Android' == $osFamily && in_array($this->getClient('name'), ['Chrome', 'Chrome Mobile'])) { + if (null === $this->device && 'Android' === $osFamily + && in_array($this->getClient('name'), ['Chrome', 'Chrome Mobile']) + ) { if ($this->matchUserAgent('Chrome/[\.0-9]* Mobile')) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_SMARTPHONE; + $this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE; } elseif ($this->matchUserAgent('Chrome/[\.0-9]* (?!Mobile)')) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TABLET; + $this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET; } } /** - * Some user agents simply contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices as tablets + * Some UA contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices as tablets */ - if (is_null($this->device) && ($this->hasAndroidTableFragment() || $this->matchUserAgent('Opera Tablet'))) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TABLET; + if (null === $this->device && ($this->hasAndroidTableFragment() + || $this->matchUserAgent('Opera Tablet')) + ) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET; } /** * Some user agents simply contain the fragment 'Android; Mobile;', so we assume those devices as smartphones */ - if (is_null($this->device) && $this->hasAndroidMobileFragment()) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_SMARTPHONE; + if (null === $this->device && $this->hasAndroidMobileFragment()) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE; } /** @@ -767,19 +876,21 @@ protected function parseDevice(): void * So were are expecting that all devices running Android < 2 are smartphones * Devices running Android 3.X are tablets. Device type of Android 2.X and 4.X+ are unknown */ - if (is_null($this->device) && 'AND' == $osShortName && '' != $osVersion) { - if (-1 == version_compare($osVersion, '2.0')) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_SMARTPHONE; - } elseif (version_compare($osVersion, '3.0') >= 0 and -1 == version_compare($osVersion, '4.0')) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TABLET; + if (null === $this->device && 'AND' === $osShortName && '' !== $osVersion) { + if (-1 === version_compare($osVersion, '2.0')) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE; + } elseif (version_compare($osVersion, '3.0') >= 0 + && -1 === version_compare($osVersion, '4.0') + ) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET; } } /** * All detected feature phones running android are more likely a smartphone */ - if (DeviceParserAbstract::DEVICE_TYPE_FEATURE_PHONE == $this->device && 'Android' == $osFamily) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_SMARTPHONE; + if (AbstractDeviceParser::DEVICE_TYPE_FEATURE_PHONE === $this->device && 'Android' === $osFamily) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE; } /** @@ -792,32 +903,37 @@ protected function parseDevice(): void * all Windows 8 touch devices are tablets. */ - if (is_null($this->device) && ('WRT' == $osShortName || ('WIN' == $osShortName && version_compare($osVersion, '8') >= 0)) && $this->isTouchEnabled()) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TABLET; + if (null === $this->device && ('WRT' === $osShortName || ('WIN' === $osShortName + && version_compare($osVersion, '8') >= 0)) && $this->isTouchEnabled() + ) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET; } /** * All devices running Opera TV Store are assumed to be a tv */ if ($this->matchUserAgent('Opera TV Store')) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TV; + $this->device = AbstractDeviceParser::DEVICE_TYPE_TV; } /** * Devices running Kylo or Espital TV Browsers are assumed to be a TV */ - if (is_null($this->device) && in_array($clientName, ['Kylo', 'Espial TV Browser'])) { - $this->device = DeviceParserAbstract::DEVICE_TYPE_TV; + if (null === $this->device && in_array($clientName, ['Kylo', 'Espial TV Browser'])) { + $this->device = AbstractDeviceParser::DEVICE_TYPE_TV; } - // set device type to desktop for all devices running a desktop os that were not detected as an other device type - if (!is_null($this->device) || !$this->isDesktop()) { + // set device type to desktop for all devices running a desktop os that were not detected as another device type + if (null !== $this->device || !$this->isDesktop()) { return; } - $this->device = DeviceParserAbstract::DEVICE_TYPE_DESKTOP; + $this->device = AbstractDeviceParser::DEVICE_TYPE_DESKTOP; } + /** + * Tries to detect the operating system + */ protected function parseOs(): void { $osParser = new OperatingSystem(); @@ -827,108 +943,33 @@ protected function parseOs(): void $this->os = $osParser->parse(); } - protected function matchUserAgent($regex) - { - $regex = '/(?:^|[^A-Z_-])(?:' . str_replace('/', '\/', $regex) . ')/i'; - - if (preg_match($regex, $this->userAgent, $matches)) { - return $matches; - } - - return false; - } - - /** - * Parses a useragent and returns the detected data - * - * ATTENTION: Use that method only for testing or very small applications - * To get fast results from DeviceDetector you need to make your own implementation, - * that should use one of the caching mechanisms. See README.md for more information. - * - * @internal - * - * @deprecated - * - * @param string $ua UserAgent to parse - * - * @return array - */ - public static function getInfoFromUserAgent(string $ua): array - { - $deviceDetector = new DeviceDetector($ua); - $deviceDetector->parse(); - - if ($deviceDetector->isBot()) { - return [ - 'user_agent' => $deviceDetector->getUserAgent(), - 'bot' => $deviceDetector->getBot(), - ]; - } - - $osFamily = OperatingSystem::getOsFamily($deviceDetector->getOs('short_name')); - $browserFamily = Browser::getBrowserFamily((string) $deviceDetector->getClient('short_name')); - - $processed = [ - 'user_agent' => $deviceDetector->getUserAgent(), - 'os' => $deviceDetector->getOs(), - 'client' => $deviceDetector->getClient(), - 'device' => [ - 'type' => $deviceDetector->getDeviceName(), - 'brand' => $deviceDetector->getBrand(), - 'model' => $deviceDetector->getModel(), - ], - 'os_family' => false !== $osFamily ? $osFamily : 'Unknown', - 'browser_family' => false !== $browserFamily ? $browserFamily : 'Unknown', - ]; - - return $processed; - } - /** - * Sets the Cache class + * @param string $regex * - * @param Cache $cache + * @return array|null */ - public function setCache(Cache $cache): void + protected function matchUserAgent(string $regex): ?array { - $this->cache = $cache; - } + $regex = '/(?:^|[^A-Z_-])(?:' . str_replace('/', '\/', $regex) . ')/i'; - /** - * Returns Cache object - * - * @return Cache - */ - public function getCache(): Cache - { - if (!empty($this->cache)) { - return $this->cache; + if (preg_match($regex, $this->userAgent, $matches)) { + return $matches; } - return new StaticCache(); - } - - /** - * Sets the Yaml Parser class - * - * @param YamlParser $yamlParser - */ - public function setYamlParser(YamlParser $yamlParser): void - { - $this->yamlParser = $yamlParser; + return null; } /** - * Returns Yaml Parser object - * - * @return YamlParser + * Resets all detected data */ - public function getYamlParser(): YamlParser + protected function reset(): void { - if (!empty($this->yamlParser)) { - return $this->yamlParser; - } - - return new Spyc(); + $this->bot = null; + $this->client = null; + $this->device = null; + $this->os = null; + $this->brand = ''; + $this->model = ''; + $this->parsed = false; } } diff --git a/Parser/BotParserAbstract.php b/Parser/AbstractBotParser.php similarity index 77% rename from Parser/BotParserAbstract.php rename to Parser/AbstractBotParser.php index 330d0e015a..05b031fa66 100644 --- a/Parser/BotParserAbstract.php +++ b/Parser/AbstractBotParser.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -11,11 +11,11 @@ namespace DeviceDetector\Parser; /** - * Class BotParserAbstract + * Class AbstractBotParser * * Abstract class for all bot parsers */ -abstract class BotParserAbstract extends ParserAbstract +abstract class AbstractBotParser extends AbstractParser { /** * Enables information discarding diff --git a/Parser/ParserAbstract.php b/Parser/AbstractParser.php similarity index 87% rename from Parser/ParserAbstract.php rename to Parser/AbstractParser.php index 97bd1a6c8c..af85a40ea2 100644 --- a/Parser/ParserAbstract.php +++ b/Parser/AbstractParser.php @@ -3,29 +3,30 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser; -use DeviceDetector\Cache\Cache; +use DeviceDetector\Cache\CacheInterface; use DeviceDetector\Cache\StaticCache; use DeviceDetector\DeviceDetector; -use DeviceDetector\Yaml\Parser as YamlParser; +use DeviceDetector\Yaml\ParserInterface as YamlParser; use DeviceDetector\Yaml\Spyc; /** - * Class ParserAbstract + * Class AbstractParser */ -abstract class ParserAbstract +abstract class AbstractParser { /** * Holds the path to the yml file containing regexes * @var string */ protected $fixtureFile; + /** * Holds the internal name of the parser * Used for caching @@ -62,34 +63,33 @@ abstract class ParserAbstract * Versioning constant used to set max versioning to major version only * Version examples are: 3, 5, 6, 200, 123, ... */ - - const VERSION_TRUNCATION_MAJOR = 0; + public const VERSION_TRUNCATION_MAJOR = 0; /** * Versioning constant used to set max versioning to minor version * Version examples are: 3.4, 5.6, 6.234, 0.200, 1.23, ... */ - const VERSION_TRUNCATION_MINOR = 1; + public const VERSION_TRUNCATION_MINOR = 1; /** * Versioning constant used to set max versioning to path level * Version examples are: 3.4.0, 5.6.344, 6.234.2, 0.200.3, 1.2.3, ... */ - const VERSION_TRUNCATION_PATCH = 2; + public const VERSION_TRUNCATION_PATCH = 2; /** * Versioning constant used to set versioning to build number * Version examples are: 3.4.0.12, 5.6.334.0, 6.234.2.3, 0.200.3.1, 1.2.3.0, ... */ - const VERSION_TRUNCATION_BUILD = 3; + public const VERSION_TRUNCATION_BUILD = 3; /** * Versioning constant used to set versioning to unlimited (no truncation) */ - const VERSION_TRUNCATION_NONE = -1; + public const VERSION_TRUNCATION_NONE = -1; /** - * @var Cache + * @var CacheInterface */ protected $cache; @@ -98,9 +98,19 @@ abstract class ParserAbstract */ protected $yamlParser; + /** + * parses the currently set useragents and returns possible results + * + * @return array|null + */ abstract public function parse(): ?array; - public function __construct($ua = '') + /** + * AbstractParser constructor. + * + * @param string $ua + */ + public function __construct(string $ua = '') { $this->setUserAgent($ua); } @@ -145,6 +155,54 @@ public function getName(): string return $this->parserName; } + /** + * Sets the Cache class + * + * @param CacheInterface $cache + */ + public function setCache(CacheInterface $cache): void + { + $this->cache = $cache; + } + + /** + * Returns Cache object + * + * @return CacheInterface + */ + public function getCache(): CacheInterface + { + if (!empty($this->cache)) { + return $this->cache; + } + + return new StaticCache(); + } + + /** + * Sets the YamlParser class + * + * @param YamlParser $yamlParser + */ + public function setYamlParser(YamlParser $yamlParser): void + { + $this->yamlParser = $yamlParser; + } + + /** + * Returns YamlParser object + * + * @return YamlParser + */ + public function getYamlParser(): YamlParser + { + if (!empty($this->yamlParser)) { + return $this->yamlParser; + } + + return new Spyc(); + } + /** * Returns the result of the parsed yml file defined in $fixtureFile * @@ -154,7 +212,7 @@ protected function getRegexes(): array { if (empty($this->regexList)) { $cacheKey = 'DeviceDetector-' . DeviceDetector::VERSION . 'regexes-' . $this->getName(); - $cacheKey = preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); + $cacheKey = (string) preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); $this->regexList = $this->getCache()->fetch($cacheKey); if (empty($this->regexList)) { @@ -235,7 +293,9 @@ protected function buildVersion(string $versionString, array $matches): string $versionString = $this->buildByMatch($versionString, $matches); $versionString = str_replace('_', '.', $versionString); - if (self::VERSION_TRUNCATION_NONE !== self::$maxMinorParts && substr_count($versionString, '.') > self::$maxMinorParts) { + if (self::VERSION_TRUNCATION_NONE !== self::$maxMinorParts + && substr_count($versionString, '.') > self::$maxMinorParts + ) { $versionParts = explode('.', $versionString); $versionParts = array_slice($versionParts, 0, 1 + self::$maxMinorParts); $versionString = implode('.', $versionParts); @@ -261,7 +321,7 @@ protected function preMatchOverall(): ?array static $overAllMatch; $cacheKey = $this->parserName . DeviceDetector::VERSION . '-all'; - $cacheKey = preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); + $cacheKey = (string) preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); if (empty($overAllMatch)) { $overAllMatch = $this->getCache()->fetch($cacheKey); @@ -277,52 +337,4 @@ protected function preMatchOverall(): ?array return $this->matchUserAgent($overAllMatch); } - - /** - * Sets the Cache class - * - * @param Cache $cache - */ - public function setCache(Cache $cache): void - { - $this->cache = $cache; - } - - /** - * Returns Cache object - * - * @return Cache - */ - public function getCache(): Cache - { - if (!empty($this->cache)) { - return $this->cache; - } - - return new StaticCache(); - } - - /** - * Sets the YamlParser class - * - * @param YamlParser $yamlParser - */ - public function setYamlParser(YamlParser $yamlParser): void - { - $this->yamlParser = $yamlParser; - } - - /** - * Returns YamlParser object - * - * @return YamlParser - */ - public function getYamlParser(): YamlParser - { - if (!empty($this->yamlParser)) { - return $this->yamlParser; - } - - return new Spyc(); - } } diff --git a/Parser/Bot.php b/Parser/Bot.php index a6f7dec4f4..9eae7a3871 100644 --- a/Parser/Bot.php +++ b/Parser/Bot.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -17,7 +17,7 @@ * * Detected bots are defined in regexes/bots.yml */ -class Bot extends BotParserAbstract +class Bot extends AbstractBotParser { /** * @var string diff --git a/Parser/Client/ClientParserAbstract.php b/Parser/Client/AbstractClientParser.php similarity index 86% rename from Parser/Client/ClientParserAbstract.php rename to Parser/Client/AbstractClientParser.php index 843e4f08df..3d4a6eafb9 100644 --- a/Parser/Client/ClientParserAbstract.php +++ b/Parser/Client/AbstractClientParser.php @@ -3,19 +3,26 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser\Client; -use DeviceDetector\Parser\ParserAbstract; +use DeviceDetector\Parser\AbstractParser; -abstract class ClientParserAbstract extends ParserAbstract +abstract class AbstractClientParser extends AbstractParser { + /** + * @var string + */ protected $fixtureFile = ''; - protected $parserName = ''; + + /** + * @var string + */ + protected $parserName = ''; /** * Parses the current UA and checks whether it contains any client information @@ -29,6 +36,8 @@ abstract class ClientParserAbstract extends ParserAbstract * -> Return the matched feed reader * * NOTE: Doing the big match before matching every single regex speeds up the detection + * + * @return array|null */ public function parse(): ?array { @@ -67,7 +76,7 @@ public static function getAvailableClients(): array $names = []; foreach ($regexes as $regex) { - if ('$1' == $regex['name']) { + if ('$1' === $regex['name']) { continue; } diff --git a/Parser/Client/Browser.php b/Parser/Client/Browser.php index c314100bf0..3980d7158f 100644 --- a/Parser/Client/Browser.php +++ b/Parser/Client/Browser.php @@ -17,10 +17,17 @@ * * Client parser for browser detection */ -class Browser extends ClientParserAbstract +class Browser extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/browsers.yml'; - protected $parserName = 'browser'; + + /** + * @var string + */ + protected $parserName = 'browser'; /** * Known browsers mapped to their internal short codes @@ -208,7 +215,11 @@ class Browser extends ClientParserAbstract 'BlackBerry Browser' => ['BB'], 'Baidu' => ['BD', 'BS'], 'Amiga' => ['AV', 'AW'], - 'Chrome' => ['CH', 'BA', 'BR', 'CC', 'CD', 'CM', 'CI', 'CF', 'CN', 'CR', 'CP', 'IR', 'RM', 'AO', 'TS', 'VI', 'PT', 'AS', 'TB', 'AD', 'SB'], + 'Chrome' => [ + 'CH', 'BA', 'BR', 'CC', 'CD', 'CM', 'CI', 'CF', 'CN', + 'CR', 'CP', 'IR', 'RM', 'AO', 'TS', 'VI', 'PT', 'AS', + 'TB', 'AD', 'SB', + ], 'Firefox' => ['FF', 'FE', 'FM', 'SX', 'FB', 'PX', 'MB', 'EI', 'WF', 'CU', 'TF', 'QM', 'FR'], 'Internet Explorer' => ['IE', 'IM', 'PS'], 'Konqueror' => ['KO'], @@ -251,9 +262,9 @@ public static function getAvailableBrowserFamilies(): array /** * @param string $browserLabel * - * @return bool|string If false, "Unknown" + * @return string|null If null, "Unknown" */ - public static function getBrowserFamily(string $browserLabel) + public static function getBrowserFamily(string $browserLabel): ?string { foreach (self::$browserFamilies as $browserFamily => $browserLabels) { if (in_array($browserLabel, $browserLabels)) { @@ -261,7 +272,7 @@ public static function getBrowserFamily(string $browserLabel) } } - return false; + return null; } /** @@ -273,9 +284,13 @@ public static function getBrowserFamily(string $browserLabel) */ public static function isMobileOnlyBrowser(string $browser): bool { - return in_array($browser, self::$mobileOnlyBrowsers) || (in_array($browser, self::$availableBrowsers) && in_array(array_search($browser, self::$availableBrowsers), self::$mobileOnlyBrowsers)); + return in_array($browser, self::$mobileOnlyBrowsers) || (in_array($browser, self::$availableBrowsers) + && in_array(array_search($browser, self::$availableBrowsers), self::$mobileOnlyBrowsers)); } + /** + * @inheritdoc + */ public function parse(): ?array { foreach ($this->getRegexes() as $regex) { @@ -293,7 +308,7 @@ public function parse(): ?array $name = $this->buildByMatch($regex['name'], $matches); foreach (self::getAvailableBrowsers() as $browserShort => $browserName) { - if (strtolower($name) == strtolower($browserName)) { + if (strtolower($name) === strtolower($browserName)) { $version = $this->buildVersion((string) $regex['version'], $matches); $engine = $this->buildEngine($regex['engine'] ?? [], $version); $engineVersion = $this->buildEngineVersion($engine); @@ -310,10 +325,16 @@ public function parse(): ?array } // This Exception should never be thrown. If so a defined browser name is missing in $availableBrowsers - throw new \Exception('Detected browser name was not found in $availableBrowsers. Tried to parse user agent: ' . $this->userAgent); // @codeCoverageIgnore + throw new \Exception(sprintf('Detected browser name was not found in $availableBrowsers. Tried to parse user agent: %s', $this->userAgent)); // @codeCoverageIgnore } - protected function buildEngine($engineData, $browserVersion): string + /** + * @param array $engineData + * @param string $browserVersion + * + * @return string + */ + protected function buildEngine(array $engineData, string $browserVersion): string { $engine = ''; @@ -346,7 +367,12 @@ protected function buildEngine($engineData, $browserVersion): string return $engine; } - protected function buildEngineVersion($engine): string + /** + * @param string $engine + * + * @return string + */ + protected function buildEngineVersion(string $engine): string { $engineVersionParser = new Engine\Version($this->userAgent, $engine); diff --git a/Parser/Client/Browser/Engine.php b/Parser/Client/Browser/Engine.php index ad59317fa8..ddb1fe0dcc 100644 --- a/Parser/Client/Browser/Engine.php +++ b/Parser/Client/Browser/Engine.php @@ -3,24 +3,31 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser\Client\Browser; -use DeviceDetector\Parser\Client\ClientParserAbstract; +use DeviceDetector\Parser\Client\AbstractClientParser; /** * Class Engine * * Client parser for browser engine detection */ -class Engine extends ClientParserAbstract +class Engine extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/browser_engine.yml'; - protected $parserName = 'browserengine'; + + /** + * @var string + */ + protected $parserName = 'browserengine'; /** * Known browser engines mapped to their internal short codes @@ -52,6 +59,9 @@ public static function getAvailableEngines(): array return self::$availableEngines; } + /** + * @inheritdoc + */ public function parse(): ?array { foreach ($this->getRegexes() as $regex) { @@ -69,12 +79,12 @@ public function parse(): ?array $name = $this->buildByMatch($regex['name'], $matches); foreach (self::getAvailableEngines() as $engineName) { - if (strtolower($name) == strtolower($engineName)) { + if (strtolower($name) === strtolower($engineName)) { return ['engine' => $engineName]; } } // This Exception should never be thrown. If so a defined browser name is missing in $availableEngines - throw new \Exception('Detected browser engine was not found in $availableEngines. Tried to parse user agent: ' . $this->userAgent); // @codeCoverageIgnore + throw new \Exception(sprintf('Detected browser engine was not found in $availableEngines. Tried to parse user agent: %s', $this->userAgent)); // @codeCoverageIgnore } } diff --git a/Parser/Client/Browser/Engine/Version.php b/Parser/Client/Browser/Engine/Version.php index 01a293ac0b..869a38dd74 100644 --- a/Parser/Client/Browser/Engine/Version.php +++ b/Parser/Client/Browser/Engine/Version.php @@ -3,21 +3,21 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser\Client\Browser\Engine; -use DeviceDetector\Parser\Client\ClientParserAbstract; +use DeviceDetector\Parser\Client\AbstractClientParser; /** * Class Version * * Client parser for browser engine version detection */ -class Version extends ClientParserAbstract +class Version extends AbstractClientParser { /** * @var string @@ -37,13 +37,20 @@ public function __construct(string $ua, string $engine) $this->engine = $engine; } + /** + * @inheritdoc + */ public function parse(): ?array { if (empty($this->engine)) { return null; } - preg_match("~{$this->engine}\s*/?\s*((?(?=\d+\.\d)\d+[.\d]*|\d{1,7}(?=(?:\D|$))))~i", $this->userAgent, $matches); + preg_match( + "~{$this->engine}\s*/?\s*((?(?=\d+\.\d)\d+[.\d]*|\d{1,7}(?=(?:\D|$))))~i", + $this->userAgent, + $matches + ); if (!$matches) { return null; diff --git a/Parser/Client/FeedReader.php b/Parser/Client/FeedReader.php index 472a758d2a..bcfca4803d 100644 --- a/Parser/Client/FeedReader.php +++ b/Parser/Client/FeedReader.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,8 +15,15 @@ * * Client parser for feed reader detection */ -class FeedReader extends ClientParserAbstract +class FeedReader extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/feed_readers.yml'; - protected $parserName = 'feed reader'; + + /** + * @var string + */ + protected $parserName = 'feed reader'; } diff --git a/Parser/Client/Library.php b/Parser/Client/Library.php index aa5f5d3907..7047fbb06b 100644 --- a/Parser/Client/Library.php +++ b/Parser/Client/Library.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -14,11 +14,16 @@ * Class Library * * Client parser for tool & software detection - * - * @package DeviceDetector\Parser\Client */ -class Library extends ClientParserAbstract +class Library extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/libraries.yml'; - protected $parserName = 'library'; + + /** + * @var string + */ + protected $parserName = 'library'; } diff --git a/Parser/Client/MediaPlayer.php b/Parser/Client/MediaPlayer.php index 6b6c8fd0bc..a0b0fb97b2 100644 --- a/Parser/Client/MediaPlayer.php +++ b/Parser/Client/MediaPlayer.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,8 +15,15 @@ * * Client parser for mediaplayer detection */ -class MediaPlayer extends ClientParserAbstract +class MediaPlayer extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/mediaplayers.yml'; - protected $parserName = 'mediaplayer'; + + /** + * @var string + */ + protected $parserName = 'mediaplayer'; } diff --git a/Parser/Client/MobileApp.php b/Parser/Client/MobileApp.php index fe92855d2e..82b02113c4 100644 --- a/Parser/Client/MobileApp.php +++ b/Parser/Client/MobileApp.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,8 +15,15 @@ * * Client parser for mobile app detection */ -class MobileApp extends ClientParserAbstract +class MobileApp extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/mobile_apps.yml'; - protected $parserName = 'mobile app'; + + /** + * @var string + */ + protected $parserName = 'mobile app'; } diff --git a/Parser/Client/PIM.php b/Parser/Client/PIM.php index 85695700db..a1ba9be8b0 100644 --- a/Parser/Client/PIM.php +++ b/Parser/Client/PIM.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,8 +15,15 @@ * * Client parser for pim (personal information manager) detection */ -class PIM extends ClientParserAbstract +class PIM extends AbstractClientParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/client/pim.yml'; - protected $parserName = 'pim'; + + /** + * @var string + */ + protected $parserName = 'pim'; } diff --git a/Parser/Device/DeviceParserAbstract.php b/Parser/Device/AbstractDeviceParser.php similarity index 91% rename from Parser/Device/DeviceParserAbstract.php rename to Parser/Device/AbstractDeviceParser.php index 5dc6889081..051a486c8e 100644 --- a/Parser/Device/DeviceParserAbstract.php +++ b/Parser/Device/AbstractDeviceParser.php @@ -3,37 +3,48 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser\Device; -use DeviceDetector\Parser\ParserAbstract; +use DeviceDetector\Parser\AbstractParser; /** - * Class DeviceParserAbstract + * Class AbstractDeviceParser * * Abstract class for all device parsers */ -abstract class DeviceParserAbstract extends ParserAbstract +abstract class AbstractDeviceParser extends AbstractParser { + /** + * @var ?int + */ protected $deviceType = null; - protected $model = ''; - protected $brand = ''; - - const DEVICE_TYPE_DESKTOP = 0; - const DEVICE_TYPE_SMARTPHONE = 1; - const DEVICE_TYPE_TABLET = 2; - const DEVICE_TYPE_FEATURE_PHONE = 3; - const DEVICE_TYPE_CONSOLE = 4; - const DEVICE_TYPE_TV = 5; // including set top boxes, blu-ray players,... - const DEVICE_TYPE_CAR_BROWSER = 6; - const DEVICE_TYPE_SMART_DISPLAY = 7; - const DEVICE_TYPE_CAMERA = 8; - const DEVICE_TYPE_PORTABLE_MEDIA_PAYER = 9; - const DEVICE_TYPE_PHABLET = 10; + + /** + * @var string + */ + protected $model = ''; + + /** + * @var string + */ + protected $brand = ''; + + public const DEVICE_TYPE_DESKTOP = 0; + public const DEVICE_TYPE_SMARTPHONE = 1; + public const DEVICE_TYPE_TABLET = 2; + public const DEVICE_TYPE_FEATURE_PHONE = 3; + public const DEVICE_TYPE_CONSOLE = 4; + public const DEVICE_TYPE_TV = 5; // including set top boxes, blu-ray players,... + public const DEVICE_TYPE_CAR_BROWSER = 6; + public const DEVICE_TYPE_SMART_DISPLAY = 7; + public const DEVICE_TYPE_CAMERA = 8; + public const DEVICE_TYPE_PORTABLE_MEDIA_PAYER = 9; + public const DEVICE_TYPE_PHABLET = 10; /** * Detectable device types @@ -584,6 +595,11 @@ abstract class DeviceParserAbstract extends ParserAbstract 'XX' => 'Unknown', ]; + /** + * Returns the device type represented by one of the DEVICE_TYPE_* constants + * + * @return int|null + */ public function getDeviceType(): ?int { return $this->deviceType; @@ -670,6 +686,9 @@ public function setUserAgent(string $userAgent): void parent::setUserAgent($userAgent); } + /** + * @inheritdoc + */ public function parse(): ?array { $brand = ''; @@ -687,12 +706,12 @@ public function parse(): ?array return null; } - if ('Unknown' != $brand) { + if ('Unknown' !== $brand) { $brandId = array_search($brand, self::$deviceBrands); if (false === $brandId) { // This Exception should never be thrown. If so a defined brand name is missing in $deviceBrands - throw new \Exception("The brand with name '{$brand}' should be listed in the deviceBrands array. Tried to parse user agent: " . $this->userAgent); // @codeCoverageIgnore + throw new \Exception(sprintf("The brand with name '%s' should be listed in the deviceBrands array. Tried to parse user agent: %s", $brand, $this->userAgent)); // @codeCoverageIgnore } $this->brand = (string) $brandId; @@ -725,8 +744,8 @@ public function parse(): ?array $this->model = $this->buildModel($modelRegex['model'], $modelMatches); - if (isset($modelRegex['brand']) && $brandId = array_search($modelRegex['brand'], self::$deviceBrands)) { - $this->brand = $brandId; + if (isset($modelRegex['brand']) && array_search($modelRegex['brand'], self::$deviceBrands)) { + $this->brand = (string) array_search($modelRegex['brand'], self::$deviceBrands); } if (isset($modelRegex['device']) && in_array($modelRegex['device'], self::$deviceTypes)) { @@ -737,7 +756,13 @@ public function parse(): ?array return $this->getResult(); } - protected function buildModel($model, $matches): string + /** + * @param string $model + * @param array $matches + * + * @return string + */ + protected function buildModel(string $model, array $matches): string { $model = $this->buildByMatch($model, $matches); @@ -752,6 +777,9 @@ protected function buildModel($model, $matches): string return trim($model); } + /** + * Resets the stored values + */ protected function reset(): void { $this->deviceType = null; @@ -759,6 +787,9 @@ protected function reset(): void $this->brand = ''; } + /** + * @return array + */ protected function getResult(): array { return [ diff --git a/Parser/Device/Camera.php b/Parser/Device/Camera.php index 1e5e0213cb..6a0218c4a5 100644 --- a/Parser/Device/Camera.php +++ b/Parser/Device/Camera.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,11 +15,21 @@ * * Device parser for camera detection */ -class Camera extends DeviceParserAbstract +class Camera extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/cameras.yml'; - protected $parserName = 'camera'; + /** + * @var string + */ + protected $parserName = 'camera'; + + /** + * @inheritdoc + */ public function parse(): ?array { if (!$this->preMatchOverall()) { diff --git a/Parser/Device/CarBrowser.php b/Parser/Device/CarBrowser.php index f9ff8e1019..37e47eb88b 100644 --- a/Parser/Device/CarBrowser.php +++ b/Parser/Device/CarBrowser.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,11 +15,21 @@ * * Device parser for car browser detection */ -class CarBrowser extends DeviceParserAbstract +class CarBrowser extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/car_browsers.yml'; - protected $parserName = 'car browser'; + /** + * @var string + */ + protected $parserName = 'car browser'; + + /** + * @inheritdoc + */ public function parse(): ?array { if (!$this->preMatchOverall()) { diff --git a/Parser/Device/Console.php b/Parser/Device/Console.php index b729f78ce7..03f68227ad 100644 --- a/Parser/Device/Console.php +++ b/Parser/Device/Console.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -14,14 +14,22 @@ * Class Console * * Device parser for console detection - * - * @package DeviceDetector\Parser\Device */ -class Console extends DeviceParserAbstract +class Console extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/consoles.yml'; - protected $parserName = 'console'; + /** + * @var string + */ + protected $parserName = 'console'; + + /** + * @inheritdoc + */ public function parse(): ?array { if (!$this->preMatchOverall()) { diff --git a/Parser/Device/HbbTv.php b/Parser/Device/HbbTv.php index 52dfb9c6d9..90149dfcf5 100644 --- a/Parser/Device/HbbTv.php +++ b/Parser/Device/HbbTv.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,20 +15,29 @@ * * Device parser for hbbtv detection */ -class HbbTv extends DeviceParserAbstract +class HbbTv extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/televisions.yml'; - protected $parserName = 'tv'; + + /** + * @var string + */ + protected $parserName = 'tv'; /** * Parses the current UA and checks whether it contains HbbTv information * * @see televisions.yml for list of detected televisions + * + * @return array|null */ public function parse(): ?array { // only parse user agents containing hbbtv fragment - if (is_null($this->isHbbTv())) { + if (null === $this->isHbbTv()) { return null; } diff --git a/Parser/Device/Mobile.php b/Parser/Device/Mobile.php index 2af597e5ad..43db34c0e5 100644 --- a/Parser/Device/Mobile.php +++ b/Parser/Device/Mobile.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,8 +15,15 @@ * * Device parser for mobile detection */ -class Mobile extends DeviceParserAbstract +class Mobile extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/mobiles.yml'; - protected $parserName = 'mobile'; + + /** + * @var string + */ + protected $parserName = 'mobile'; } diff --git a/Parser/Device/PortableMediaPlayer.php b/Parser/Device/PortableMediaPlayer.php index 6b5d3f84b0..390dea3dd6 100644 --- a/Parser/Device/PortableMediaPlayer.php +++ b/Parser/Device/PortableMediaPlayer.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,11 +15,20 @@ * * Device parser for portable media player detection */ -class PortableMediaPlayer extends DeviceParserAbstract +class PortableMediaPlayer extends AbstractDeviceParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/device/portable_media_player.yml'; - protected $parserName = 'portablemediaplayer'; + /** + * @var string + */ + protected $parserName = 'portablemediaplayer'; + /** + * @inheritdoc + */ public function parse(): ?array { if (!$this->preMatchOverall()) { diff --git a/Parser/OperatingSystem.php b/Parser/OperatingSystem.php index 8780e58e96..8d06d6891e 100644 --- a/Parser/OperatingSystem.php +++ b/Parser/OperatingSystem.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -18,10 +18,16 @@ * Detected operating systems can be found in self::$operatingSystems and /regexes/oss.yml * This class also defined some operating system families and methods to get the family for a specific os */ -class OperatingSystem extends ParserAbstract +class OperatingSystem extends AbstractParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/oss.yml'; - protected $parserName = 'os'; + /** + * @var string + */ + protected $parserName = 'os'; /** * Known operating systems mapped to their internal short codes @@ -130,7 +136,10 @@ class OperatingSystem extends ParserAbstract 'IBM' => ['OS2'], 'iOS' => ['IOS'], 'RISC OS' => ['ROS'], - 'GNU/Linux' => ['LIN', 'ARL', 'DEB', 'KNO', 'MIN', 'UBT', 'KBT', 'XBT', 'LBT', 'FED', 'RHT', 'VLN', 'MDR', 'GNT', 'SAB', 'SLW', 'SSE', 'CES', 'BTR', 'SAF'], + 'GNU/Linux' => [ + 'LIN', 'ARL', 'DEB', 'KNO', 'MIN', 'UBT', 'KBT', 'XBT', 'LBT', 'FED', + 'RHT', 'VLN', 'MDR', 'GNT', 'SAB', 'SLW', 'SSE', 'CES', 'BTR', 'SAF', + ], 'Mac' => ['MAC'], 'Mobile Gaming Console' => ['PSP', 'NDS', 'XBX'], 'Real-time OS' => ['MTK', 'TDX'], @@ -162,6 +171,9 @@ public static function getAvailableOperatingSystemFamilies(): array return self::$osFamilies; } + /** + * @inheritdoc + */ public function parse(): ?array { $return = $osRegex = $matches = []; @@ -182,7 +194,7 @@ public function parse(): ?array $short = 'UNK'; foreach (self::$operatingSystems as $osShort => $osName) { - if (strtolower($name) != strtolower($osName)) { + if (strtolower($name) !== strtolower($osName)) { continue; } @@ -204,40 +216,22 @@ public function parse(): ?array return $return; } - protected function parsePlatform() - { - if ($this->matchUserAgent('arm')) { - return 'ARM'; - } - - if ($this->matchUserAgent('WOW64|x64|win64|amd64|x86_64')) { - return 'x64'; - } - - if ($this->matchUserAgent('i[0-9]86|i86pc')) { - return 'x86'; - } - - return ''; - } - - /** * Returns the operating system family for the given operating system * * @param string $osLabel * - * @return bool|string If false, "Unknown" + * @return string|null If null, "Unknown" */ - public static function getOsFamily(string $osLabel) + public static function getOsFamily(string $osLabel): ?string { foreach (self::$osFamilies as $family => $labels) { if (in_array($osLabel, $labels)) { - return $family; + return (string) $family; } } - return false; + return null; } /** @@ -246,9 +240,9 @@ public static function getOsFamily(string $osLabel) * @param string $os * @param string|null $ver * - * @return bool|string + * @return ?string */ - public static function getNameFromId(string $os, ?string $ver = null) + public static function getNameFromId(string $os, ?string $ver = null): ?string { if (array_key_exists($os, self::$operatingSystems)) { $osFullName = self::$operatingSystems[$os]; @@ -256,6 +250,26 @@ public static function getNameFromId(string $os, ?string $ver = null) return trim($osFullName . ' ' . $ver); } - return false; + return null; + } + + /** + * @return string + */ + protected function parsePlatform(): string + { + if ($this->matchUserAgent('arm')) { + return 'ARM'; + } + + if ($this->matchUserAgent('WOW64|x64|win64|amd64|x86_64')) { + return 'x64'; + } + + if ($this->matchUserAgent('i[0-9]86|i86pc')) { + return 'x86'; + } + + return ''; } } diff --git a/Parser/VendorFragment.php b/Parser/VendorFragment.php index da9c9286fa..58e5f6858a 100644 --- a/Parser/VendorFragment.php +++ b/Parser/VendorFragment.php @@ -3,27 +3,40 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Parser; -use DeviceDetector\Parser\Device\DeviceParserAbstract; +use DeviceDetector\Parser\Device\AbstractDeviceParser; /** * Class VendorFragments * * Device parser for vendor fragment detection */ -class VendorFragment extends ParserAbstract +class VendorFragment extends AbstractParser { + /** + * @var string + */ protected $fixtureFile = 'regexes/vendorfragments.yml'; - protected $parserName = 'vendorfragments'; + /** + * @var string + */ + protected $parserName = 'vendorfragments'; + + /** + * @var string + */ protected $matchedRegex = null; + /** + * @inheritdoc + */ public function parse(): ?array { foreach ($this->getRegexes() as $brand => $regexes) { @@ -31,7 +44,7 @@ public function parse(): ?array if ($this->matchUserAgent($regex . '[^a-z0-9]+')) { $this->matchedRegex = $regex; - return ['brand' => array_search($brand, DeviceParserAbstract::$deviceBrands)]; + return ['brand' => array_search($brand, AbstractDeviceParser::$deviceBrands)]; } } } @@ -39,7 +52,10 @@ public function parse(): ?array return null; } - public function getMatchedRegex() + /** + * @return string|null + */ + public function getMatchedRegex(): ?string { return $this->matchedRegex; } diff --git a/Tests/Cache/PSR16CacheTest.php b/Tests/Cache/PSR16CacheTest.php index b41dae9ecc..addc4b4f45 100644 --- a/Tests/Cache/PSR16CacheTest.php +++ b/Tests/Cache/PSR16CacheTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,14 +15,15 @@ use MatthiasMullie\Scrapbook\Psr16\SimpleCache; use PHPUnit\Framework\TestCase; -if (!class_exists('\MatthiasMullie\Scrapbook\Adapters\MemoryStore')) { - return; -} - class PSR16CacheTest extends TestCase { protected function setUp(): void { + if (!class_exists('\MatthiasMullie\Scrapbook\Adapters\MemoryStore')) { + $this->markTestSkipped('class \MatthiasMullie\Scrapbook\Adapters\MemoryStore required for tests'); + return; + } + $cache = new PSR16Bridge(new SimpleCache(new MemoryStore())); $cache->flushAll(); } diff --git a/Tests/Cache/PSR6CacheTest.php b/Tests/Cache/PSR6CacheTest.php index 8f03134202..0c2f4a63d8 100644 --- a/Tests/Cache/PSR6CacheTest.php +++ b/Tests/Cache/PSR6CacheTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -15,14 +15,15 @@ use MatthiasMullie\Scrapbook\Psr6\Pool; use PHPUnit\Framework\TestCase; -if (!class_exists('\MatthiasMullie\Scrapbook\Adapters\MemoryStore')) { - return; -} - class PSR6CacheTest extends TestCase { protected function setUp(): void { + if (!class_exists('\MatthiasMullie\Scrapbook\Adapters\MemoryStore')) { + $this->markTestSkipped('class \MatthiasMullie\Scrapbook\Adapters\MemoryStore required for tests'); + return; + } + $cache = new PSR6Bridge(new Pool(new MemoryStore())); $cache->flushAll(); } diff --git a/Tests/Cache/StaticCacheTest.php b/Tests/Cache/StaticCacheTest.php index 7517108dea..7043f4e725 100644 --- a/Tests/Cache/StaticCacheTest.php +++ b/Tests/Cache/StaticCacheTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ diff --git a/Tests/DeviceDetectorTest.php b/Tests/DeviceDetectorTest.php index 5f0695b10c..0633f70e76 100644 --- a/Tests/DeviceDetectorTest.php +++ b/Tests/DeviceDetectorTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -11,8 +11,8 @@ namespace DeviceDetector\Tests; use DeviceDetector\DeviceDetector; -use DeviceDetector\Parser\Device\DeviceParserAbstract; -use DeviceDetector\Parser\ParserAbstract; +use DeviceDetector\Parser\AbstractParser; +use DeviceDetector\Parser\Device\AbstractDeviceParser; use DeviceDetector\Yaml\Symfony; use PHPUnit\Framework\TestCase; @@ -132,7 +132,7 @@ public function testIsParsed(): void public function testParse($fixtureData): void { $ua = $fixtureData['user_agent']; - DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); + AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE); $uaInfo = DeviceDetector::getInfoFromUserAgent($ua); $this->assertEquals($fixtureData, $uaInfo, "UserAgent: {$ua}"); } @@ -196,22 +196,22 @@ public function testInstanceReusage(): void */ public function testVersionTruncation($useragent, $truncationType, $osVersion, $clientVersion): void { - ParserAbstract::setVersionTruncation($truncationType); + AbstractParser::setVersionTruncation($truncationType); $dd = new DeviceDetector($useragent); $dd->parse(); $this->assertEquals($osVersion, $dd->getOs('version')); $this->assertEquals($clientVersion, $dd->getClient('version')); - ParserAbstract::setVersionTruncation(ParserAbstract::VERSION_TRUNCATION_NONE); + AbstractParser::setVersionTruncation(AbstractParser::VERSION_TRUNCATION_NONE); } public function getVersionTruncationFixtures() { return [ - ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', ParserAbstract::VERSION_TRUNCATION_NONE, '4.2.2', '34.0.1847.114'], - ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', ParserAbstract::VERSION_TRUNCATION_BUILD, '4.2.2', '34.0.1847.114'], - ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', ParserAbstract::VERSION_TRUNCATION_PATCH, '4.2.2', '34.0.1847'], - ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', ParserAbstract::VERSION_TRUNCATION_MINOR, '4.2', '34.0'], - ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', ParserAbstract::VERSION_TRUNCATION_MAJOR, '4', '34'], + ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', AbstractParser::VERSION_TRUNCATION_NONE, '4.2.2', '34.0.1847.114'], + ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', AbstractParser::VERSION_TRUNCATION_BUILD, '4.2.2', '34.0.1847.114'], + ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', AbstractParser::VERSION_TRUNCATION_PATCH, '4.2.2', '34.0.1847'], + ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', AbstractParser::VERSION_TRUNCATION_MINOR, '4.2', '34.0'], + ['Mozilla/5.0 (Linux; Android 4.2.2; ARCHOS 101 PLATINUM Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Safari/537.36', AbstractParser::VERSION_TRUNCATION_MAJOR, '4', '34'], ]; } diff --git a/Tests/Parser/BotTest.php b/Tests/Parser/BotTest.php index ec953180a1..c3d2af2ce7 100644 --- a/Tests/Parser/BotTest.php +++ b/Tests/Parser/BotTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ diff --git a/Tests/Parser/Client/BrowserTest.php b/Tests/Parser/Client/BrowserTest.php index dc8ab817f3..e7bf42e01b 100644 --- a/Tests/Parser/Client/BrowserTest.php +++ b/Tests/Parser/Client/BrowserTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\Browser; use \Spyc; +use DeviceDetector\Parser\Client\Browser; use PHPUnit\Framework\TestCase; class BrowserTest extends TestCase diff --git a/Tests/Parser/Client/FeedReaderTest.php b/Tests/Parser/Client/FeedReaderTest.php index 77230c0ed8..88adcc590e 100644 --- a/Tests/Parser/Client/FeedReaderTest.php +++ b/Tests/Parser/Client/FeedReaderTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\FeedReader; use \Spyc; +use DeviceDetector\Parser\Client\FeedReader; use PHPUnit\Framework\TestCase; class FeedReaderTest extends TestCase diff --git a/Tests/Parser/Client/LibraryTest.php b/Tests/Parser/Client/LibraryTest.php index 0f92992aee..cdd5af1ce6 100644 --- a/Tests/Parser/Client/LibraryTest.php +++ b/Tests/Parser/Client/LibraryTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\Library; use \Spyc; +use DeviceDetector\Parser\Client\Library; use PHPUnit\Framework\TestCase; class LibraryTest extends TestCase diff --git a/Tests/Parser/Client/MediaPlayerTest.php b/Tests/Parser/Client/MediaPlayerTest.php index 188e35af14..ed6f79a088 100644 --- a/Tests/Parser/Client/MediaPlayerTest.php +++ b/Tests/Parser/Client/MediaPlayerTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\MediaPlayer; use \Spyc; +use DeviceDetector\Parser\Client\MediaPlayer; use PHPUnit\Framework\TestCase; class MediaPlayerTest extends TestCase diff --git a/Tests/Parser/Client/MobileAppTest.php b/Tests/Parser/Client/MobileAppTest.php index c26194eb9d..8bfec4cef9 100644 --- a/Tests/Parser/Client/MobileAppTest.php +++ b/Tests/Parser/Client/MobileAppTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\MobileApp; use \Spyc; +use DeviceDetector\Parser\Client\MobileApp; use PHPUnit\Framework\TestCase; class MobileAppTest extends TestCase diff --git a/Tests/Parser/Client/PIMTest.php b/Tests/Parser/Client/PIMTest.php index 63f1291e51..4fbef3f69f 100644 --- a/Tests/Parser/Client/PIMTest.php +++ b/Tests/Parser/Client/PIMTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Client; -use DeviceDetector\Parser\Client\PIM; use \Spyc; +use DeviceDetector\Parser\Client\PIM; use PHPUnit\Framework\TestCase; class PIMTest extends TestCase diff --git a/Tests/Parser/Device/CameraTest.php b/Tests/Parser/Device/CameraTest.php index e3fe72dc1d..711b2a495c 100644 --- a/Tests/Parser/Device/CameraTest.php +++ b/Tests/Parser/Device/CameraTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Device; -use DeviceDetector\Parser\Device\Camera; use \Spyc; +use DeviceDetector\Parser\Device\Camera; use PHPUnit\Framework\TestCase; class CameraTest extends TestCase diff --git a/Tests/Parser/Device/CarBrowserTest.php b/Tests/Parser/Device/CarBrowserTest.php index b3674c74ff..2b3a4e9845 100644 --- a/Tests/Parser/Device/CarBrowserTest.php +++ b/Tests/Parser/Device/CarBrowserTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Device; -use DeviceDetector\Parser\Device\CarBrowser; use \Spyc; +use DeviceDetector\Parser\Device\CarBrowser; use PHPUnit\Framework\TestCase; class CarBrowserTest extends TestCase diff --git a/Tests/Parser/Device/ConsoleTest.php b/Tests/Parser/Device/ConsoleTest.php index dbe5bb4d0c..474f57d670 100644 --- a/Tests/Parser/Device/ConsoleTest.php +++ b/Tests/Parser/Device/ConsoleTest.php @@ -10,8 +10,8 @@ namespace DeviceDetector\Tests\Parser\Device; -use DeviceDetector\Parser\Device\Console; use \Spyc; +use DeviceDetector\Parser\Device\Console; use PHPUnit\Framework\TestCase; class ConsoleTest extends TestCase diff --git a/Tests/Parser/Device/DeviceParserAbstractTest.php b/Tests/Parser/Device/DeviceParserAbstractTest.php index 6e6deb4cb8..593ab887c2 100644 --- a/Tests/Parser/Device/DeviceParserAbstractTest.php +++ b/Tests/Parser/Device/DeviceParserAbstractTest.php @@ -3,36 +3,36 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Tests\Parser\Device; -use DeviceDetector\Parser\Device\DeviceParserAbstract; +use DeviceDetector\Parser\Device\AbstractDeviceParser; use PHPUnit\Framework\TestCase; class DeviceParserAbstractTest extends TestCase { public function testGetAvailableDeviceTypes(): void { - $available = DeviceParserAbstract::getAvailableDeviceTypes(); + $available = AbstractDeviceParser::getAvailableDeviceTypes(); $this->assertGreaterThan(5, count($available)); $this->assertContains('desktop', array_keys($available)); } public function testGetAvailableDeviceTypeNames(): void { - $available = DeviceParserAbstract::getAvailableDeviceTypeNames(); + $available = AbstractDeviceParser::getAvailableDeviceTypeNames(); $this->assertGreaterThan(5, count($available)); $this->assertContains('desktop', $available); } public function testGetFullName(): void { - $this->assertEquals('', DeviceParserAbstract::getFullName('Invalid')); - $this->assertEquals('Asus', DeviceParserAbstract::getFullName('AU')); - $this->assertEquals('Google', DeviceParserAbstract::getFullName('GO')); + $this->assertEquals('', AbstractDeviceParser::getFullName('Invalid')); + $this->assertEquals('Asus', AbstractDeviceParser::getFullName('AU')); + $this->assertEquals('Google', AbstractDeviceParser::getFullName('GO')); } } diff --git a/Tests/Parser/Device/HbbTvTest.php b/Tests/Parser/Device/HbbTvTest.php index ccae53b1a2..e00513342f 100644 --- a/Tests/Parser/Device/HbbTvTest.php +++ b/Tests/Parser/Device/HbbTvTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ diff --git a/Tests/Parser/OperatingSystemTest.php b/Tests/Parser/OperatingSystemTest.php index 0c49d08c66..73de48bea7 100644 --- a/Tests/Parser/OperatingSystemTest.php +++ b/Tests/Parser/OperatingSystemTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ diff --git a/Tests/Parser/VendorFragmentTest.php b/Tests/Parser/VendorFragmentTest.php index dff1388c87..8c4798f28f 100644 --- a/Tests/Parser/VendorFragmentTest.php +++ b/Tests/Parser/VendorFragmentTest.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ diff --git a/Yaml/Parser.php b/Yaml/ParserInterface.php similarity index 87% rename from Yaml/Parser.php rename to Yaml/ParserInterface.php index fd4c975450..16008c649c 100644 --- a/Yaml/Parser.php +++ b/Yaml/ParserInterface.php @@ -3,19 +3,20 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ namespace DeviceDetector\Yaml; -interface Parser +interface ParserInterface { /** * Parses the file with the given filename and returns the converted content * * @param string $file + * * @return mixed */ public function parseFile(string $file); diff --git a/Yaml/Pecl.php b/Yaml/Pecl.php index a3bc4563e9..79fec461f8 100644 --- a/Yaml/Pecl.php +++ b/Yaml/Pecl.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -18,7 +18,7 @@ * Parses a YAML file with LibYAML library * @see http://php.net/manual/en/function.yaml-parse-file.php */ -class Pecl implements Parser +class Pecl implements ParserInterface { /** * Parses the file with the given filename using PECL and returns the converted content @@ -34,7 +34,7 @@ public function parseFile(string $file) if (false === function_exists('yaml_parse_file')) { throw new Exception('Pecl YAML extension is not installed'); } - + return yaml_parse_file($file); } } diff --git a/Yaml/Spyc.php b/Yaml/Spyc.php index 76680b4ba0..d66597ccae 100644 --- a/Yaml/Spyc.php +++ b/Yaml/Spyc.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -12,12 +12,13 @@ use \Spyc as SpycParser; -class Spyc implements Parser +class Spyc implements ParserInterface { /** * Parses the file with the given filename using Spyc and returns the converted content * * @param string $file + * * @return mixed */ public function parseFile(string $file) diff --git a/Yaml/Symfony.php b/Yaml/Symfony.php index bbf6dddaf3..df32556353 100644 --- a/Yaml/Symfony.php +++ b/Yaml/Symfony.php @@ -3,7 +3,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -12,12 +12,13 @@ use Symfony\Component\Yaml\Yaml; -class Symfony implements Parser +class Symfony implements ParserInterface { /** * Parses the file with the given filename using Symfony Yaml parser and returns the converted content * * @param string $file + * * @return mixed */ public function parseFile(string $file) diff --git a/misc/file-test.php b/misc/file-test.php index 02bd126248..8fa2d65476 100644 --- a/misc/file-test.php +++ b/misc/file-test.php @@ -26,7 +26,7 @@ require __DIR__ . '/../vendor/autoload.php'; use DeviceDetector\DeviceDetector; -use DeviceDetector\Parser\Device\DeviceParserAbstract; +use DeviceDetector\Parser\Device\AbstractDeviceParser; if (php_sapi_name() !== 'cli') { echo "web not supported"; @@ -87,7 +87,7 @@ function printReport($result, $format) while (!feof($fn)) { $userAgent = fgets($fn); $userAgent = trim($userAgent); - DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); + AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE); $result = DeviceDetector::getInfoFromUserAgent($userAgent); if (!isset($result['device']['model'])) { diff --git a/misc/readme-report.php b/misc/readme-report.php index 518947576d..dea4f708a4 100644 --- a/misc/readme-report.php +++ b/misc/readme-report.php @@ -2,7 +2,7 @@ /** * Device Detector - The Universal Device Detection library for parsing User Agents * - * @link http://piwik.org + * @link https://matomo.org * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later */ @@ -58,7 +58,7 @@