• 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

0.0
/src/Loader/Data/Device.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\Loader\Data;
15

16
use BrowserDetector\Loader\InitData\Device as DataDevice;
17
use FilterIterator;
18
use Iterator;
19
use Laminas\Hydrator\Strategy\StrategyInterface;
20
use Override;
21
use RecursiveDirectoryIterator;
22
use RecursiveIteratorIterator;
23
use RuntimeException;
24
use SplFileInfo;
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
final class Device
35
{
36
    private const string DATA_PATH = __DIR__ . '/../../../data/devices/';
37

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

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

48
    /** @throws RuntimeException */
NEW
49
    public function init(): void
×
50
    {
NEW
51
        if ($this->initialized) {
×
NEW
52
            return;
×
53
        }
54

NEW
55
        $iterator = new RecursiveIteratorIterator(
×
NEW
56
            new RecursiveDirectoryIterator(self::DATA_PATH . $this->company),
×
NEW
57
        );
×
NEW
58
        $files    = new class ($iterator, 'json') extends FilterIterator {
×
59
            /**
60
             * @param Iterator<SplFileInfo> $iterator
61
             *
62
             * @throws void
63
             */
64
            public function __construct(Iterator $iterator, private readonly string $extension)
65
            {
NEW
66
                parent::__construct($iterator);
×
67
            }
68

69
            /** @throws void */
70
            #[Override]
71
            public function accept(): bool
72
            {
NEW
73
                $file = $this->getInnerIterator()->current();
×
74

NEW
75
                assert($file instanceof SplFileInfo);
×
76

NEW
77
                return $file->isFile() && $file->getExtension() === $this->extension;
×
78
            }
NEW
79
        };
×
80

NEW
81
        foreach ($files as $file) {
×
NEW
82
            assert($file instanceof SplFileInfo);
×
83

NEW
84
            $pathName = $file->getPathname();
×
NEW
85
            $filepath = str_replace('\\', '/', $pathName);
×
NEW
86
            assert(is_string($filepath));
×
87

NEW
88
            $content = @file_get_contents($filepath);
×
89

NEW
90
            assert($content === false || is_string($content));
×
91

NEW
92
            if ($content === false) {
×
NEW
93
                throw new RuntimeException(sprintf('could not read file "%s"', $file));
×
94
            }
95

NEW
96
            $fileData = $this->strategy->hydrate($content, []);
×
97

NEW
98
            assert(is_array($fileData));
×
99

NEW
100
            foreach ($fileData as $key => $data) {
×
NEW
101
                $stringKey = (string) $key;
×
102

NEW
103
                if (array_key_exists($stringKey, $this->items) || !$data instanceof DataDevice) {
×
NEW
104
                    continue;
×
105
                }
106

NEW
107
                $this->items[$stringKey] = $data;
×
108
            }
109
        }
110

NEW
111
        $this->initialized = true;
×
112
    }
113

114
    /** @throws void */
NEW
115
    public function getItem(string $stringKey): DataDevice | null
×
116
    {
NEW
117
        return $this->items[$stringKey] ?? null;
×
118
    }
119
}
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