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

mimmi20 / browser-detector / 26619626009

29 May 2026 05:20AM UTC coverage: 96.478% (-0.01%) from 96.488%
26619626009

push

github

web-flow
Merge pull request #1058 from mimmi20/dependabot/composer/development-dependencies-f926ab8d74

composer (master)(deps-dev): bump the development-dependencies group with 7 updates

15285 of 15843 relevant lines covered (96.48%)

157.91 hits per line

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

0.0
/src/Loader/Data/Os.php
1
<?php
2

3
/**
4
 * This file is part of the browser-detector package.
5
 *
6
 * Copyright (c) 2012-2026, 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\Loader\Data;
15

16
use BrowserDetector\Iterator\FilterIterator;
17
use BrowserDetector\Loader\InitData\Os as DataOs;
18
use Laminas\Hydrator\Strategy\StrategyInterface;
19
use Override;
20
use RecursiveDirectoryIterator;
21
use RecursiveIteratorIterator;
22
use RuntimeException;
23
use SplFileInfo;
24
use UnexpectedValueException;
25

26
use function array_key_exists;
27
use function assert;
28
use function file_get_contents;
29
use function is_array;
30
use function is_string;
31
use function sprintf;
32
use function str_replace;
33

34
/** @deprecated will be removed */
35
final class Os implements DataInterface
36
{
37
    private const string DATA_PATH = __DIR__ . '/../../../data/platforms';
38

39
    /** @var array<string, DataOs> */
40
    private array $items      = [];
41
    private bool $initialized = false;
42

43
    /** @throws void */
44
    public function __construct(private readonly StrategyInterface $strategy)
×
45
    {
46
        // nothing to do
47
    }
×
48

49
    /** @throws RuntimeException */
50
    #[Override]
51
    public function init(): void
×
52
    {
53
        if ($this->initialized) {
×
54
            return;
×
55
        }
56

57
        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::DATA_PATH));
×
58
        $files    = new FilterIterator($iterator, 'json');
×
59

60
        foreach ($files as $file) {
×
61
            assert($file instanceof SplFileInfo);
×
62

63
            $pathName = $file->getPathname();
×
64
            $filepath = str_replace('\\', '/', $pathName);
×
65
            assert(is_string($filepath));
×
66

67
            $content = @file_get_contents($filepath);
×
68

69
            assert($content === false || is_string($content));
×
70

71
            if ($content === false) {
×
72
                throw new RuntimeException(sprintf('could not read file "%s"', $file));
×
73
            }
74

75
            $fileData = $this->strategy->hydrate($content, []);
×
76

77
            assert(is_array($fileData));
×
78

79
            foreach ($fileData as $key => $data) {
×
80
                $stringKey = (string) $key;
×
81

82
                if (array_key_exists($stringKey, $this->items) || !$data instanceof DataOs) {
×
83
                    continue;
×
84
                }
85

86
                $this->items[$stringKey] = $data;
×
87
            }
88
        }
89

90
        $this->initialized = true;
×
91
    }
92

93
    /** @throws void */
94
    #[Override]
95
    public function getItem(string $stringKey): DataOs | null
×
96
    {
97
        if (array_key_exists($stringKey, $this->items)) {
×
98
            return $this->items[$stringKey];
×
99
        }
100

101
        try {
102
            $os = \BrowserDetector\Data\Os::fromName($stringKey);
×
103

104
            $data = new DataOs(
×
105
                name: $os->getName(),
×
106
                marketingName: $os->getMarketingName(),
×
107
                manufacturer: $os->getManufacturer()->getBrandname(),
×
108
                version: (object) $os->getVersion(),
×
109
            );
×
110

111
            $this->items[$stringKey] = $data;
×
112
        } catch (UnexpectedValueException) {
×
113
            // do nothing
114
        }
115

116
        return $this->items[$stringKey] ?? null;
×
117
    }
118
}
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