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
Branch master (70949a)
by Viktor
01:59 queued 19s
created

HtmlHeadingNormalizer::numberToHeadingLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Vikpe;
4
5
class HtmlHeadingNormalizer
6
{
7
    public static function normalize($html, $base_level = 1)
8
    {
9
        if (!self::htmlContainsHeadings($html)) {
10
            return $html;
11
        }
12
13
        $domDocument = new \DOMDocument();
14
        $domDocument->loadHTML($html);
15
16
        $headingTagNames = ['h1', 'h2', 'h3', 'h4', 'h6'];
17
18
        $originalHeadingDomElements = [];
19
        $normalizedHeadingDomElements = [];
20
21
        foreach ($headingTagNames as $headingTagName) {
22
            $headingDomElements = $domDocument->getElementsByTagName($headingTagName);
23
24
            foreach ($headingDomElements as $headingDomElement) {
25
                $currentHeadingLevel = self::headingTagNameToNumber($headingDomElement->tagName);
26
                $newHeadingLevel = self::numberToHeadingLevel($base_level + $currentHeadingLevel - 1);
27
28
                if ($newHeadingLevel !== $currentHeadingLevel) {
29
                    $originalHeadingDomElements[] = $headingDomElement;
30
                    $normalizedHeadingDomElements[] = self::cloneDomElementWithNewTagName($headingDomElement, $newHeadingLevel);
31
                }
32
            }
33
        }
34
35
        foreach ($originalHeadingDomElements as $i => $needle) {
36
            $needle->parentNode->replaceChild($normalizedHeadingDomElements[$i], $needle);
37
        }
38
39
        return $domDocument->saveHTML();
40
    }
41
42
    private static function htmlContainsHeadings($html)
43
    {
44
        $headingNeedle = '<h';
45
        $containsHeadings = (false !== stripos($html, $headingNeedle));
46
47
        return $containsHeadings;
48
    }
49
50
    private static function headingTagNameToNumber($headingLevel)
51
    {
52
        return substr($headingLevel, 1);
53
    }
54
55
    private static function numberToHeadingLevel($number)
56
    {
57
        return 'h' . $number;
58
    }
59
60
    private static function cloneDomElementWithNewTagName(\DOMElement $sourceDomElement, $newTagName)
61
    {
62
        $targetDomElement = $sourceDomElement->parentNode->ownerDocument->createElement($newTagName);
63
        self::copyAttributes($sourceDomElement, $targetDomElement);
64
        self::moveChildNodes($sourceDomElement, $targetDomElement);
65
66
        return $targetDomElement;
67
    }
68
69
    private static function copyAttributes(\DOMElement $source, \DOMElement $target)
70
    {
71
        foreach ($source->attributes as $attribute) {
72
            $target->setAttribute($attribute->name, $attribute->value);
73
        }
74
    }
75
76
    private static function moveChildNodes(\DOMElement $source, \DOMElement $target)
77
    {
78
        while ($source->hasChildNodes()) {
79
            // appendChild() actually moves the childNode
80
            $target->appendChild($source->childNodes->item(0));
81
        }
82
    }
83
}
84