• 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

85.71
/src/Parser/Header/UseragentClientCode.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\BrowserParserInterface;
20
use UaParser\ClientCodeInterface;
21
use UnexpectedValueException;
22

23
use function mb_strtolower;
24
use function preg_match;
25

26
final readonly class UseragentClientCode implements ClientCodeInterface
27
{
28
    /** @throws void */
29
    public function __construct(private BrowserParserInterface $browserParser, private NormalizerInterface $normalizer)
21✔
30
    {
31
        // nothing to do
32
    }
21✔
33

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

45
    /**
46
     * @return non-empty-string|null
47
     *
48
     * @throws void
49
     */
50
    #[Override]
21✔
51
    public function getClientCode(string $value): string | null
52
    {
53
        try {
54
            $normalizedValue = $this->normalizer->normalize($value);
21✔
NEW
55
        } catch (Exception) {
×
NEW
56
            return null;
×
57
        }
58

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

63
        $matches = [];
21✔
64

65
        if (preg_match('/pr\((?P<client>[^\/)]+)(?:\/[\d.]+)?\);/', $normalizedValue, $matches)) {
21✔
66
            $code = mb_strtolower($matches['client']);
11✔
67

68
            if ($code === 'ucbrowser') {
11✔
69
                return $code;
11✔
70
            }
71
        }
72

73
        try {
74
            $code = $this->browserParser->parse($normalizedValue);
10✔
75

76
            if ($code === '') {
7✔
77
                return null;
3✔
78
            }
79

80
            return $code;
4✔
81
        } catch (UnexpectedValueException) {
3✔
82
            // do nothing
83
        }
84

85
        return null;
3✔
86
    }
87
}
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