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

mimmi20 / browser-detector / 15993998116

01 Jul 2025 08:27AM UTC coverage: 82.949% (-6.4%) from 89.356%
15993998116

push

github

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

add new devices

29 of 106 new or added lines in 14 files covered. (27.36%)

2588 of 3120 relevant lines covered (82.95%)

17.05 hits per line

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

0.0
/src/Loader/Data/Company.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\Engine as DataEngine;
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 Engine implements DataInterface
35
{
36
    private const string DATA_PATH = __DIR__ . '/../../../data/engines';
37

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

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

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

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

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

74
                assert($file instanceof SplFileInfo);
×
75

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

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

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

87
            $content = @file_get_contents($filepath);
×
88

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

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

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

97
            assert(is_array($fileData));
×
98

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

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

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

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

113
    /** @throws void */
NEW
114
    #[Override]
×
115
    public function getItem(string $stringKey): DataEngine | null
116
    {
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