• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mimmi20 / browser-detector / 12945342151

22 Jan 2025 07:24PM UTC coverage: 89.212% (-10.8%) from 100.0%
12945342151

push

github

web-flow
Merge pull request #872 from mimmi20/updates

rewrite Headers

2983 of 3466 new or added lines in 72 files covered. (86.06%)

9 existing lines in 2 files now uncovered.

4234 of 4746 relevant lines covered (89.21%)

11.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.29
/src/Parser/Header/UseragentPlatformCode.php
1
<?php
2

3
/**
4
 * This file is part of the browser-detector package.
5
 *
6
 * Copyright (c) 2012-2025, Thomas Mueller <mimmi20@live.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types = 1);
13

14
namespace BrowserDetector\Parser\Header;
15

16
use Override;
17
use UaNormalizer\Normalizer\Exception\Exception;
18
use UaNormalizer\Normalizer\NormalizerInterface;
19
use UaParser\PlatformCodeInterface;
20
use UaParser\PlatformParserInterface;
21

22
use function mb_strtolower;
23
use function preg_match;
24

25
final readonly class UseragentPlatformCode implements PlatformCodeInterface
26
{
27
    /** @throws void */
28
    public function __construct(
21✔
29
        private PlatformParserInterface $platformParser,
30
        private NormalizerInterface $normalizer,
31
    ) {
32
        // nothing to do
33
    }
21✔
34

35
    /**
36
     * @throws void
37
     *
38
     * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
39
     */
40
    #[Override]
21✔
41
    public function hasPlatformCode(string $value): bool
42
    {
43
        return true;
21✔
44
    }
45

46
    /**
47
     * @throws void
48
     *
49
     * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
50
     */
51
    #[Override]
21✔
52
    public function getPlatformCode(string $value, string | null $derivate = null): string | null
53
    {
54
        try {
55
            $normalizedValue = $this->normalizer->normalize($value);
21✔
NEW
56
        } catch (Exception) {
×
NEW
57
            return null;
×
58
        }
59

60
        if ($normalizedValue === '' || $normalizedValue === null) {
21✔
NEW
61
            return null;
×
62
        }
63

64
        $matches = [];
21✔
65

66
        if (preg_match('/ov\((?P<platform>wds|android) (?:[\d_.]+)\);/i', $normalizedValue, $matches)) {
21✔
67
            $code = mb_strtolower($matches['platform']);
2✔
68

69
            return match ($code) {
70
                'wds' => 'windows phone',
2✔
71
                default => 'android',
2✔
72
            };
73
        }
74

75
        if (preg_match('/pf\((?P<platform>[^)]+)\);/', $normalizedValue, $matches)) {
19✔
76
            $code = mb_strtolower($matches['platform']);
8✔
77

78
            return match ($code) {
8✔
79
                'symbian', 'java' => $code,
2✔
80
                'windows' => 'windows phone',
1✔
81
                '42', '44' => 'ios',
2✔
82
                'linux' => 'android',
2✔
83
                default => null,
8✔
84
            };
8✔
85
        }
86

87
        $code = $this->platformParser->parse($normalizedValue);
11✔
88

89
        if ($code === '') {
11✔
90
            return null;
4✔
91
        }
92

93
        return $code;
7✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc