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 ( 6394d8...ebc892 )
by Emmanuel
02:56
created

GlHtmlNode::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Extend \DomNode
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  GLICER
8
 * @package   GlHtml
9
 * @author    Emmanuel ROECKER
10
 * @author    Rym BOUCHAGOUR
11
 * @copyright 2015 GLICER
12
 * @license   MIT
13
 * @link      http://dev.glicer.com/
14
 *
15
 * Created : 19/02/15
16
 * File : GlHtmlNode.php
17
 *
18
 */
19
20
21
namespace GlHtml;
22
23
/**
24
 * Class GlHtmlNode
25
 * @package GlHtml
26
 */
27
class GlHtmlNode
28
{
29
    /**
30
     * @var \DOMNode
31
     */
32
    private $node;
33
34
    /**
35
     * @param \DOMNode $node
36
     */
37
    public function __construct(\DOMNode $node)
38
    {
39
        $this->node = $node;
40
    }
41
42
    /**
43
     * @param array $attributes
44
     */
45
    public function setAttributes(array $attributes)
46
    {
47
        foreach ($attributes as $name => $value) {
48
            $this->node->setAttribute($name, $value);
49
        }
50
    }
51
52
    /**
53
     * @param string $value
54
     */
55
    public function setValue($value)
56
    {
57
        $this->node->nodeValue = $value;
58
    }
59
60
    /**
61
     * @param string $html
62
     */
63
    public
64
    function add(
65
        $html
66
    ) {
67
        $frag = $this->node->ownerDocument->createDocumentFragment();
68
        $frag->appendXML($html);
69
        $this->node->appendChild($frag);
70
    }
71
72
    /**
73
     * @return GlHtmlNode
74
     */
75
    public function getParent() {
76
        return new GlHtmlNode($this->node->parentNode);
77
    }
78
    
79
    /**
80
     * @param string $html
81
     */
82
    public
83
    function replaceMe(
84
        $html
85
    ) {
86
        $frag = $this->node->ownerDocument->createDocumentFragment();
87
        $frag->appendXML($html);
88
        $this->node->parentNode->replaceChild($frag, $this->node);
89
    }
90
91
    /**
92
     * @param string $html
93
     */
94
    public function replaceInner($html)
95
    {
96
        $this->setValue('');
97
        $this->add($html);
98
    }
99
100
101
    /**
102
     * @return \DOMNode
103
     */
104
    public function getDOMNode()
105
    {
106
        return $this->node;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getName()
113
    {
114
        return strtolower($this->node->nodeName);
115
    }
116
117
    /**
118
     * @param string $name
119
     *
120
     * @return string
121
     */
122
    public
123
    function getAttribute(
124
        $name
125
    ) {
126
        return $this->node->getAttribute($name);
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public
133
    function getText()
134
    {
135
        return $this->node->nodeValue;
136
    }
137
138
139
    /**
140
     * @return array
141
     */
142
    public function getSentences()
143
    {
144
        $sentences = [];
145
        $sentence  = "";
146
        static::iterateSentencesOverNode($this->node, $sentence, $sentences);
0 ignored issues
show
Bug introduced by
Since iterateSentencesOverNode() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of iterateSentencesOverNode() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
147
148
        $sentence = trim($sentence);
149
        if (strlen($sentence) > 0) {
150
            $sentences[] = $sentence;
151
        }
152
153
        return $sentences;
154
    }
155
156
    public
157
    function delete()
158
    {
159
        $this->node->parentNode->removeChild($this->node);
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public
166
    function getHtml()
167
    {
168
        $innerHTML = '';
169
        $children  = $this->node->childNodes;
170
        foreach ($children as $child) {
171
            $innerHTML .= $child->ownerDocument->saveXML($child, LIBXML_NOEMPTYTAG);
172
        }
173
174
        return $innerHTML;
175
    }
176
177
    /**
178
     * extract h tag from html
179
     *
180
     * @param \DOMNodeList $nodeList
181
     * @param callable     $fct
182
     */
183
    private function recursiveCallChild(\DOMNodeList $nodeList, callable $fct)
184
    {
185
        /**
186
         * @var \DOMNode $domNode
187
         */
188
        foreach ($nodeList as $domNode) {
189
            $fct(new GlHtmlNode($domNode));
190
            if ($domNode->hasChildNodes()) {
191
                $this->recursiveCallChild($domNode->childNodes, $fct);
192
            }
193
        }
194
    }
195
196
    /**
197
     * @param callable $fct
198
     */
199
    public function callChild(callable $fct)
200
    {
201
        $this->recursiveCallChild($this->node->childNodes, $fct);
202
    }
203
204
    /**
205
     * @param \DOMNode $node
206
     * @param string   $sentence
207
     * @param array    $sentences
208
     *
209
     * @return int
210
     */
211
    private static function iterateSentencesOverNode(\DOMNode $node, &$sentence, array &$sentences)
212
    {
213
        if ($node instanceof \DOMText) {
214
            $sentence .= preg_replace("/[\\t\\n\\f\\r ]+/im", " ", $node->wholeText);
215
216
            return 0;
217
        }
218
        if ($node instanceof \DOMDocumentType) {
219
            return 0;
220
        }
221
222
        $name = strtolower($node->nodeName);
223
        if (preg_match('/^h(\d+)$/', $name)) {
224
            $name = "hx";
225
        }
226
227
        switch ($name) {
228
            case "hr":
229
            case "style":
230
            case "head":
231
            case "title":
232
            case "meta":
233
            case "script":
234
            case "pre":
235
                return 0;
236
237
            case "p":
238
            case "hx":
239
            case "th":
240
            case "td":
241
            case "li":
242
            case "label":
243 View Code Duplication
            case "button":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
                $sentence = trim($sentence);
245
                if (strlen($sentence) > 0) {
246
                    $sentences[] = $sentence;
247
                }
248
                $sentence = "";
249
                break;
250
251
            case "br":
252
                $sentence .= " ";
253
                break;
254
            default:
255
                break;
256
        }
257
258
        $childs = $node->childNodes;
259
        foreach ($childs as $child) {
260
            static::iterateSentencesOverNode($child, $sentence, $sentences);
0 ignored issues
show
Bug introduced by
Since iterateSentencesOverNode() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of iterateSentencesOverNode() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
261
        }
262
263
        switch ($name) {
264
            case "style":
265
            case "head":
266
            case "title":
267
            case "meta":
268
            case "script":
269
                return "";
270
271
            case "p":
272
            case "hx":
273
            case "th":
274
            case "td":
275
            case "li":
276
            case "label":
277 View Code Duplication
            case "button":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
                $sentence = trim($sentence);
279
                if (strlen($sentence) > 0) {
280
                    $sentences[] = $sentence;
281
                }
282
                $sentence = "";
283
                break;
284
285
            default:
286
                break;
287
        }
288
289
        return 0;
290
    }
291
}
292