DataSetsFromContentTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 79
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getDataSets() 0 25 6
A getContent() 0 4 1
B getDataSetFromString() 0 28 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Crossjoin\Browscap\Source\Ini;
5
6
use Crossjoin\Browscap\Exception\ParserRuntimeException;
7
use Crossjoin\Browscap\Exception\SourceUnavailableException;
8
use Crossjoin\Browscap\Source\DataSet;
9
10
/**
11
 * Trait DataSetsFromContentTrait
12
 *
13
 * @package Crossjoin\Browscap\Source\Ini
14
 * @author Christoph Ziegenberg <[email protected]>
15
 * @link https://github.com/crossjoin/browscap
16
 */
17
trait DataSetsFromContentTrait
18
{
19
    /**
20
     * To be overwritten and filled with content by the class using this trait!
21
     *
22
     * @return \Generator
23
     */
24
    public function getContent() : \Generator
25
    {
26
        yield '';
27
    }
28
29
    /**
30
     * @inheritdoc
31
     *
32
     * @throws ParserRuntimeException
33
     * @throws SourceUnavailableException
34
     */
35
    public function getDataSets() : \Iterator
36
    {
37
        $unprocessedBytes = '';
38
        foreach ($this->getContent() as $bytes) {
39
            $combinedBytes = $unprocessedBytes . $bytes;
40
            $patternStart = strpos($combinedBytes, '[');
41
42
            $dataProcessed = false;
43
            if ($patternStart !== false) {
44
                while (($nextPatternStart = strpos($combinedBytes, '[', $patternStart + 1)) !== false) {
45
                    $dataSet = substr($combinedBytes, $patternStart, $nextPatternStart - $patternStart);
46
                    yield $this->getDataSetFromString($dataSet);
47
                    $patternStart = $nextPatternStart;
48
                    $dataProcessed = true;
49
                    $unprocessedBytes = substr($combinedBytes, $nextPatternStart);
50
                }
51
            }
52
            if ($dataProcessed === false) {
53
                $unprocessedBytes = $combinedBytes;
54
            }
55
        }
56
        if (trim($unprocessedBytes) !== '') {
57
            yield $this->getDataSetFromString($unprocessedBytes);
58
        }
59
    }
60
61
    /**
62
     * @param string $data
63
     *
64
     * @return DataSet
65
     * @throws ParserRuntimeException
66
     */
67
    protected function getDataSetFromString(string $data) : DataSet
68
    {
69
        if (strpos($data, "\n") === false) {
70
            throw new ParserRuntimeException('The data could not be parsed (no pattern found).', 1459589758);
71
        }
72
73
        if (strpos($data, "\r\n") !== false) {
74
            // if the source file was created under windows, replace the line endings
75
            $data = str_replace("\r\n", "\n", $data);
76
        }
77
78
        // Prepare the data from the data set
79
        list($pattern, $properties) = explode("\n", $data, 2);
80
        $pattern = substr($pattern, 1, -1);
81
82
        $properties = @parse_ini_string($properties);
83
        if ($properties === false) {
84
            throw new ParserRuntimeException(
85
                "The data could not be parsed (invalid properties for pattern '$pattern').",
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $pattern instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
86
                1459589759
87
            );
88
        }
89
90
        $dataSet = new DataSet($pattern);
91
        $dataSet->setProperties($properties);
92
93
        return $dataSet;
94
    }
95
}
96