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 ( 1f8c61...782f80 )
by Viktor
01:27
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, $baseLevel = 1)
8
    {
9
        if (!self::containsHeadings($html)) {
10
            return $html;
11
        }
12
13
        $domDocument = new \DOMDocument();
14
        $domDocument->loadHTML($html);
15
16
        $originalHeadings = self::getHeadings($domDocument);
17
        $normalizedHeadings = self::normalizeHeadings($originalHeadings, $baseLevel);
18
19
        foreach ($originalHeadings as $i => $needle) {
20
            $needle->parentNode->replaceChild($normalizedHeadings[$i], $needle);
21
        }
22
23
        return $domDocument->saveHTML();
24
    }
25
26
    private static function getHeadings(\DOMDocument $domDocument)
27
    {
28
        $tagNames = ['h1', 'h2', 'h3', 'h4', 'h6'];
29
30
        $headings = [];
31
32
        foreach ($tagNames as $tagName) {
33
            foreach ($domDocument->getElementsByTagName($tagName) as $heading) {
34
                $headings[] = $heading;
35
            }
36
        }
37
38
        return $headings;
39
    }
40
41
    private static function normalizeHeadings(array $originalHeadings, $baseLevel)
42
    {
43
        $normalizedHeadings = [];
44
45
        foreach ($originalHeadings as $heading) {
46
            $currentLevel = self::tagNameToLevel($heading->tagName);
47
            $newTagName = self::levelToTagName($baseLevel + $currentLevel - 1);
48
49
            $normalizedHeadings[] = self::cloneHeading($heading, $newTagName);
50
        }
51
52
        return $normalizedHeadings;
53
    }
54
55
    private static function containsHeadings($html)
56
    {
57
        $heading_needle = '<h';
58
59
        return (false !== stripos($html, $heading_needle));
60
    }
61
62
    private static function tagNameToLevel($tagName)
63
    {
64
        return substr($tagName, 1);
65
    }
66
67
    private static function levelToTagName($number)
68
    {
69
        return 'h'.$number;
70
    }
71
72
    private static function cloneHeading(\DOMElement $sourceHeading, $tagName)
73
    {
74
        $targetHeading = $sourceHeading->parentNode->ownerDocument->createElement($tagName);
75
        self::copyAttributes($sourceHeading, $targetHeading);
76
        self::moveChildNodes($sourceHeading, $targetHeading);
77
78
        return $targetHeading;
79
    }
80
81
    private static function copyAttributes(\DOMElement $source, \DOMElement $target)
82
    {
83
        foreach ($source->attributes as $attribute) {
84
            $target->setAttribute($attribute->name, $attribute->value);
85
        }
86
    }
87
88
    private static function moveChildNodes(\DOMElement $source, \DOMElement $target)
89
    {
90
        while ($source->hasChildNodes()) {
91
            // appendChild() actually moves the childNode
92
            $target->appendChild($source->childNodes->item(0));
93
        }
94
    }
95
}
96