Skip to content

Commit 0eb5389

Browse files
committed
fix Android tablet detection in VERSION_TRUNCATION_MAJOR mode
1 parent cc4eb16 commit 0eb5389

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

DeviceDetector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,10 @@ protected function parseDevice(): void
980980
* Devices running Android 3.X are tablets. Device type of Android 2.X and 4.X+ are unknown
981981
*/
982982
if (null === $this->device && 'Android' === $osName && '' !== $osVersion) {
983-
if (-1 === \version_compare($osVersion, '2.0')) {
983+
if (-1 === \version_compare($osVersion, '2')) {
984984
$this->device = AbstractDeviceParser::DEVICE_TYPE_SMARTPHONE;
985-
} elseif (\version_compare($osVersion, '3.0') >= 0
986-
&& -1 === \version_compare($osVersion, '4.0')
985+
} elseif (\version_compare($osVersion, '3') >= 0
986+
&& -1 === \version_compare($osVersion, '4')
987987
) {
988988
$this->device = AbstractDeviceParser::DEVICE_TYPE_TABLET;
989989
}

Tests/DeviceDetectorTest.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function testParse(array $fixtureData): void
230230
}
231231

232232
$errorMessage = \sprintf(
233-
"UserAgent: %s\nHeaders: %s",
233+
"UserAgent: %s\nHeaders: %s\nVersion truncation: none",
234234
$ua,
235235
\print_r($fixtureData['headers'] ?? null, true)
236236
);
@@ -240,6 +240,63 @@ public function testParse(array $fixtureData): void
240240
$this->assertEquals($fixtureData, $uaInfo, $errorMessage);
241241
}
242242

243+
private static function truncateVersion(?string $versionString): ?string
244+
{
245+
if (is_null($versionString)) {
246+
return null;
247+
}
248+
249+
if (\substr_count($versionString, '.') > 0) {
250+
$versionParts = \explode('.', $versionString);
251+
$versionParts = \array_slice($versionParts, 0, 1);
252+
$versionString = \implode('.', $versionParts);
253+
}
254+
255+
return \trim($versionString, ' .');
256+
}
257+
258+
/**
259+
* @dataProvider getFixtures
260+
*/
261+
public function testParseWithVersionTruncationMajor(array $fixtureData): void
262+
{
263+
$ua = $fixtureData['user_agent'];
264+
$clientHints = !empty($fixtureData['headers']) ? ClientHints::factory($fixtureData['headers']) : null;
265+
266+
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_MAJOR);
267+
268+
try {
269+
$uaInfo = DeviceDetector::getInfoFromUserAgent($ua, $clientHints);
270+
} catch (\Exception $exception) {
271+
throw new \Exception(
272+
\sprintf('Error: %s from useragent %s', $exception->getMessage(), $ua),
273+
$exception->getCode(),
274+
$exception
275+
);
276+
}
277+
278+
$errorMessage = \sprintf(
279+
"UserAgent: %s\nHeaders: %s\nVersion truncation: major",
280+
$ua,
281+
\print_r($fixtureData['headers'] ?? null, true)
282+
);
283+
284+
unset($fixtureData['headers']); // ignore headers in result
285+
286+
// truncate versions
287+
if (array_key_exists('version', $fixtureData['os'] ?? [])) {
288+
$fixtureData['os']['version'] = self::truncateVersion($fixtureData['os']['version']);
289+
}
290+
291+
if (array_key_exists('version', $fixtureData['client'] ?? [])) {
292+
$fixtureData['client']['version'] = self::truncateVersion($fixtureData['client']['version']);
293+
}
294+
295+
$this->assertEquals($fixtureData, $uaInfo, $errorMessage);
296+
297+
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
298+
}
299+
243300
public function getFixtures(): array
244301
{
245302
$fixtures = [];

0 commit comments

Comments
 (0)