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 ( e26f84...dcd545 )
by Yusuf
03:50
created

MicrodataDOMElement::hasPropertyNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace YusufKandemir\MicrodataParser;
4
5
class MicrodataDOMElement extends \DOMElement
6
{
7
    /** @var array "tag name" to "attribute name" mapping */
8
    private static $tagNameLookup = [
9
        'audio' => 'src',
10
        'embed' => 'src',
11
        'iframe' => 'src',
12
        'img' => 'src',
13
        'source' => 'src',
14
        'track' => 'src',
15
        'video' => 'src',
16
        'a' => 'href',
17
        'area' => 'href',
18
        'link' => 'href',
19
        'object' => 'data',
20
        'data' => 'value',
21
        'meter' => 'value',
22
        'time' => 'datetime',
23
    ];
24
25
    /** @var array Attributes that have absolute values */
26
    private static $absoluteAttributes = ['src', 'href', 'data',];
27
28
    /**
29
     * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-item-properties for details of algorithm
30
     *
31
     * @return array
32
     */
33 27
    public function getProperties() : array
34
    {
35 27
        $results = [];
36 27
        $memory = [$this];
37 27
        $pending = $this->getChildElementNodes();
38
39 27
        $pending = array_merge($pending, $this->getReferenceNodes());
40
41 27
        while ($pending) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pending of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42 27
            $current = array_pop($pending);
43
44 27
            foreach ($memory as $memory_item) {
45 27
                if ($current->isSameNode($memory_item)) {
46 27
                    continue 2; // Skip next part and continue while loop if memory contains $current
47
                }
48
            }
49
50 27
            $memory[] = $current;
51
52 27
            if (! $current->hasAttribute('itemscope')) {
53 27
                $pending = array_merge($pending, $current->getChildElementNodes());
54
            }
55
56 27
            if ($current->hasAttribute('itemprop') && $current->hasPropertyNames()) {
57 27
                $results[] = $current;
58
            }
59
        }
60
61 27
        return array_reverse($results);
62
    }
63
64
    /**
65
     * @return bool
66
     */
67 27
    public function hasPropertyNames() : bool
68
    {
69 27
        return !empty($this->tokenizeAttribute('itemprop'));
70
    }
71
72
    /**
73
     * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-property-name
74
     *
75
     * @return array
76
     */
77 27
    public function getPropertyNames() : array
78
    {
79 27
        $tokens = $this->tokenizeAttribute('itemprop');
80
81 27
        $properties = [];
82
83 27
        foreach ($tokens as $token) {
84 27
            if (!$this->isAbsoluteUri($token) && $this->tokenizeAttribute('itemtype')) {
85 18
                $token = /*$vocabularyIdentifier . */ $token;
86
            }
87
88 27
            $properties[] = $token;
89
        }
90
91 27
        return array_unique($properties);
92
    }
93
94
    /**
95
     * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-property-value for details of algorithm
96
     *
97
     * @param callable $absoluteUriHandler
98
     *
99
     * @return $this|string
100
     */
101 27
    public function getPropertyValue(callable $absoluteUriHandler = null)
102
    {
103 27
        if ($this->hasAttribute('itemscope')) {
104 18
            return $this;
105
        }
106
107 27
        if ($this->hasAttribute('content')) {
108
            return $this->getAttribute('content');
109
        }
110
111 27
        $value = '';
112
113 27
        if (\array_key_exists($this->tagName, self::$tagNameLookup)) {
114 27
            $attribute = self::$tagNameLookup[$this->tagName];
115 27
            $value = $this->getAttribute($attribute);
116
117 27
            if (!empty($value) && \in_array($attribute, self::$absoluteAttributes) && !$this->isAbsoluteUri($value)) {
118 27
                $value = $absoluteUriHandler($value, $this->ownerDocument->documentURI);
119
            }
120
        }
121
122 27
        return $value ?: $this->textContent;
123
    }
124
125
    /**
126
     * Checks a string to see if its absolute uri or not
127
     * Note: As it uses a simple regex to check, it is not that reliable
128
     *
129
     * @see \preg_match() for return values
130
     *
131
     * @param string $uri
132
     *
133
     * @return false|int
134
     */
135 27
    protected function isAbsoluteUri(string $uri)
136
    {
137 27
        return preg_match("/^\w+:/", trim($uri));
138
    }
139
140
    /**
141
     * Filters out TextNodes etc. and returns child ElementNodes as array
142
     *
143
     * @return array Result array which contains child ElementNodes
144
     */
145 27
    protected function getChildElementNodes()
146
    {
147 27
        $childNodes = [];
148
149 27
        foreach ($this->childNodes as $childNode) {
150 27
            if ($childNode->nodeType == XML_ELEMENT_NODE) {
151 27
                $childNodes[] = $childNode;
152
            }
153
        }
154
155 27
        return $childNodes;
156
    }
157
158
    /**
159
     * Tokenizes value of given attribute
160
     *
161
     * @param string $attributeName Name of the attribute
162
     *
163
     * @return array|array[]|false|string[]
164
     */
165 27
    public function tokenizeAttribute(string $attributeName)
166
    {
167 27
        $attribute = [];
168
169 27
        if ($this->hasAttribute($attributeName)) {
170 27
            $attribute = $this->tokenize($this->getAttribute($attributeName));
171
        }
172
173 27
        return $attribute;
174
    }
175
176
    /**
177
     * Splits given attribute value in space characters to array
178
     *
179
     * @see \preg_split() for possible return values and behaviour
180
     *
181
     * @see https://www.w3.org/TR/2018/WD-microdata-20180426/#dfn-split-a-string-on-spaces for definition of tokens
182
     *
183
     * @param string $attribute
184
     *
185
     * @return array[]|false|string[]
186
     */
187 27
    protected function tokenize(string $attribute)
188
    {
189 27
        return preg_split('/\s+/', trim($attribute));
190
    }
191
192
    /**
193
     * Finds the nodes that this node references through the document
194
     *
195
     * @see https://www.w3.org/TR/microdata/#dfn-item-properties 4th step
196
     *
197
     * @return array
198
     */
199 27
    protected function getReferenceNodes(): array
200
    {
201 27
        $referenceNodes = [];
202
203 27
        if ($this->hasAttribute('itemref')) {
204 9
            $tokens = $this->tokenizeAttribute('itemref');
205
206 9
            foreach ($tokens as $token) {
207 9
                $references = $this->ownerDocument->xpath->query('//*[@id="' . $token . '"]');
208
209 9
                if ($first = $references->item(0)) {
210 9
                    $referenceNodes[] = $first;
211
                }
212
            }
213
        }
214
215 27
        return $referenceNodes;
216
    }
217
}
218