From 7a3206dd3685c97e4062712da6145634837dd905 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Mon, 27 Nov 2023 11:36:49 +0100 Subject: [PATCH] Fix newly detected PHPStan issues --- ClientHints.php | 1 + DeviceDetector.php | 4 ++-- Parser/AbstractParser.php | 32 ++++++++++++++++++-------- Parser/Client/Browser.php | 2 +- Parser/Client/MobileApp.php | 2 +- Parser/Device/AbstractDeviceParser.php | 16 +++++++++---- phpstan.neon | 3 ++- 7 files changed, 41 insertions(+), 19 deletions(-) diff --git a/ClientHints.php b/ClientHints.php index 33f7184160..d7b9a16d9a 100644 --- a/ClientHints.php +++ b/ClientHints.php @@ -192,6 +192,7 @@ public function getBrandList(): array $versions = \array_column($this->fullVersionList, 'version'); if (\count($brands) === \count($versions)) { + // @phpstan-ignore-next-line return \array_combine($brands, $versions); } } diff --git a/DeviceDetector.php b/DeviceDetector.php index 67d2037c3a..c7bf0de376 100644 --- a/DeviceDetector.php +++ b/DeviceDetector.php @@ -149,13 +149,13 @@ class DeviceDetector /** * Holds the cache class used for caching the parsed yml-Files - * @var CacheInterface + * @var CacheInterface|null */ protected $cache = null; /** * Holds the parser class used for parsing yml-Files - * @var YamlParser + * @var YamlParser|null */ protected $yamlParser = null; diff --git a/Parser/AbstractParser.php b/Parser/AbstractParser.php index e33fdbc488..6f59125ebe 100644 --- a/Parser/AbstractParser.php +++ b/Parser/AbstractParser.php @@ -110,14 +110,14 @@ abstract class AbstractParser public const VERSION_TRUNCATION_NONE = -1; /** - * @var CacheInterface + * @var CacheInterface|null */ - protected $cache; + protected $cache = null; /** - * @var YamlParser + * @var YamlParser|null */ - protected $yamlParser; + protected $yamlParser = null; /** * parses the currently set useragents and returns possible results @@ -244,14 +244,24 @@ public function getYamlParser(): YamlParser protected function getRegexes(): array { if (empty($this->regexList)) { - $cacheKey = 'DeviceDetector-' . DeviceDetector::VERSION . 'regexes-' . $this->getName(); - $cacheKey = (string) \preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); - $this->regexList = $this->getCache()->fetch($cacheKey); + $cacheKey = 'DeviceDetector-' . DeviceDetector::VERSION . 'regexes-' . $this->getName(); + $cacheKey = (string) \preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); + $cacheContent = $this->getCache()->fetch($cacheKey); + + if (\is_array($cacheContent)) { + $this->regexList = $cacheContent; + } if (empty($this->regexList)) { - $this->regexList = $this->getYamlParser()->parseFile( + $parsedContent = $this->getYamlParser()->parseFile( $this->getRegexesDirectory() . DIRECTORY_SEPARATOR . $this->fixtureFile ); + + if (!\is_array($parsedContent)) { + $parsedContent = []; + } + + $this->regexList = $parsedContent; $this->getCache()->save($cacheKey, $this->regexList); } } @@ -385,7 +395,11 @@ protected function preMatchOverall(): ?array $cacheKey = (string) \preg_replace('/([^a-z0-9_-]+)/i', '', $cacheKey); if (empty($this->overAllMatch)) { - $this->overAllMatch = $this->getCache()->fetch($cacheKey); + $overAllMatch = $this->getCache()->fetch($cacheKey); + + if (\is_string($overAllMatch)) { + $this->overAllMatch = $overAllMatch; + } } if (empty($this->overAllMatch)) { diff --git a/Parser/Client/Browser.php b/Parser/Client/Browser.php index 1363ef2c87..a2e270eb50 100644 --- a/Parser/Client/Browser.php +++ b/Parser/Client/Browser.php @@ -26,7 +26,7 @@ class Browser extends AbstractClientParser { /** - * @var BrowserHints|null + * @var BrowserHints */ private $browserHints; diff --git a/Parser/Client/MobileApp.php b/Parser/Client/MobileApp.php index b28012d78a..d5c0a1edce 100644 --- a/Parser/Client/MobileApp.php +++ b/Parser/Client/MobileApp.php @@ -25,7 +25,7 @@ class MobileApp extends AbstractClientParser { /** - * @var AppHints|null + * @var AppHints */ private $appHints; diff --git a/Parser/Device/AbstractDeviceParser.php b/Parser/Device/AbstractDeviceParser.php index 60bc60f135..65d374835a 100644 --- a/Parser/Device/AbstractDeviceParser.php +++ b/Parser/Device/AbstractDeviceParser.php @@ -1830,11 +1830,17 @@ public static function getAvailableDeviceTypeNames(): array * * @param int $deviceType one of the DEVICE_TYPE_* constants * - * @return mixed + * @return string */ - public static function getDeviceName(int $deviceType) + public static function getDeviceName(int $deviceType): string { - return \array_search($deviceType, self::$deviceTypes); + $deviceName = \array_search($deviceType, self::$deviceTypes); + + if (\is_string($deviceName)) { + return $deviceName; + } + + return ''; } /** @@ -1908,8 +1914,8 @@ public function parse(): ?array // is freeze user-agent then restoring the original UA for the device definition if ('' !== $deviceModel && \preg_match('~Android 10[.\d]*; K(?: Build/|[;)])~i', $this->userAgent)) { - $osVersion = $this->clientHints->getOperatingSystemVersion(); - $this->setUserAgent(\preg_replace( + $osVersion = $this->clientHints ? $this->clientHints->getOperatingSystemVersion() : ''; + $this->setUserAgent((string) \preg_replace( '(Android 10[.\d]*; K)', \sprintf('Android %s; %s', '' !== $osVersion ? $osVersion : '10', $deviceModel), $this->userAgent diff --git a/phpstan.neon b/phpstan.neon index 3ef25a8e38..124fb0123a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,12 +4,13 @@ parameters: - %rootDir%/../../autoload.php paths: - %rootDir%/../../../ - excludes_analyse: + excludePaths: - %rootDir%/../../../vendor - %rootDir%/../../../misc - %rootDir%/../../../Tests - %rootDir%/../../../autoload.php ignoreErrors: - '#.+Doctrine\\Common\\Cache\\CacheProvider.+#' + - '#.+unknown class Illuminate\\Support\\Facades\\Cache.+#' reportUnmatchedIgnoredErrors: false checkMissingIterableValueType: false \ No newline at end of file