GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d6a28a...b978ee )
by Sebastian
02:05
created

AbbreviationParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\HtmlElement;
4
5
use Spatie\HtmlElement\Helpers\Arr;
6
7
class AbbreviationParser
8
{
9
    /** @var string */
10
    protected $element = 'div';
11
12
    /** @var array */
13
    protected $classes = [];
14
15
    /** @var array */
16
    protected $attributes = [];
17
18
    public static function parse(string $tag) : array
19
    {
20
        $parsed = (new static($tag));
21
22
        return [
23
            'element' => $parsed->element,
24
            'classes' => $parsed->classes,
25
            'attributes' => $parsed->attributes,
26
        ];
27
    }
28
29
    protected function __construct(string $tag)
30
    {
31
        $this->parseTag($tag);
32
    }
33
34
    protected function parseTag(string $tag)
35
    {
36
        foreach ($this->explodeTag($tag) as $part) {
37
38
            switch ($part[0]) {
39
                case '.':
40
                    $this->parseClass($part);
41
                    break;
42
                case '#':
43
                    $this->parseId($part);
44
                    break;
45
                case '[':
46
                    $this->parseAttribute($part);
47
                    break;
48
                default:
49
                    $this->parseElement($part);
50
                    break;
51
            }
52
        }
53
    }
54
55
    protected function parseClass(string $class)
56
    {
57
        $this->classes[] = ltrim($class, '.');
58
    }
59
60
    protected function parseId(string $id)
61
    {
62
        $this->attributes['id'] = ltrim($id, '#');
63
    }
64
65
    protected function parseAttribute(string $attribute)
66
    {
67
        $keyValueSet = explode('=', trim($attribute, '[]'), 2);
68
69
        $key = $keyValueSet[0];
70
        $value = $keyValueSet[1] ?? null;
71
        
72
        $this->attributes[$key] = trim($value, '\'"');
73
    }
74
75
    protected function parseElement(string $element)
76
    {
77
        $this->element = $element;
78
    }
79
80
    protected function explodeTag(string $tag) : array
81
    {
82
        // First split out the attributes set with `[...=...]`
83
        $parts = preg_split('/(?=( \[[^]]+] ))/x', $tag);
84
85
        // Afterwards we can extract the rest of the attributes
86
        return Arr::flatMap($parts, function ($part) {
87
88
            if (strpos($part, '[') === 0) {
89
                list($attributeValue, $rest) = explode(']', $part, 2);
90
91
                return [$attributeValue] + $this->explodeTag($rest);
92
            }
93
94
            return preg_split('/(?=( (\.) | (\#) ))/x', $part);
95
        });
96
    }
97
}
98