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 ( 837a18...e48a30 )
by Viktor
01:33
created

HtmlHeadingNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
1
<?php
2
3
namespace Vikpe;
4
5
class HtmlHeadingNormalizer
6
{
7
    public static function demote($html, $levels)
8
    {
9
        return self::normalize($html, $levels);
10
    }
11
12
    private static function normalize($html, $levels)
13
    {
14
        if (!self::containsHeadings($html)) {
15
            return $html;
16
        }
17
18
        $domDocument = new \DOMDocument();
19
        $domDocument->loadHTML($html);
20
21
        $originalHeadings = self::getHeadings($domDocument);
22
        $normalizedHeadings = self::normalizeHeadings($originalHeadings, $levels);
23
24
        self::replaceHeadings(
25
            $originalHeadings,
26
            $normalizedHeadings
27
        );
28
29
        return $domDocument->saveHTML();
30
    }
31
32
    private static function getHeadings(\DOMDocument $domDocument)
33
    {
34
        $tagNames = array('h1', 'h2', 'h3', 'h4', 'h6');
35
36
        $headings = array();
37
38
        foreach ($tagNames as $tagName) {
39
            foreach ($domDocument->getElementsByTagName($tagName) as $heading) {
40
                $headings[] = $heading;
41
            }
42
        }
43
44
        return $headings;
45
    }
46
47
    private static function normalizeHeadings(array $originalHeadings, $levelDelta)
48
    {
49
        $normalizedHeadings = array();
50
51
        foreach ($originalHeadings as $heading) {
52
            $currentLevel = self::tagNameToLevel($heading->tagName);
53
            $normalizedLevel = $currentLevel + $levelDelta;
54
55
            $normalizedHeadings[] = self::cloneHeading($heading, $normalizedLevel);
56
        }
57
58
        return $normalizedHeadings;
59
    }
60
61
    private static function replaceHeadings(array $needles, array $replacements)
62
    {
63
        foreach ($needles as $i => $needle) {
64
            $needle->parentNode->replaceChild($replacements[$i], $needle);
65
        }
66
    }
67
68
    private static function containsHeadings($html)
69
    {
70
        $headingNeedle = '<h';
71
        $containsHeadings = (false !== stripos($html, $headingNeedle));
72
73
        return $containsHeadings;
74
    }
75
76
    private static function tagNameToLevel($tagName)
77
    {
78
        return substr($tagName, 1);
79
    }
80
81
    private static function levelToTagName($level)
82
    {
83
        return 'h'.$level;
84
    }
85
86
    private static function cloneHeading(\DOMElement $sourceHeading, $level = null)
87
    {
88
        if (null !== $level) {
89
            $tagName = self::levelToTagName($level);
90
        } else {
91
            $tagName = $sourceHeading->tagName;
92
        }
93
94
        $targetHeading = $sourceHeading->parentNode->ownerDocument->createElement($tagName);
95
        self::copyAttributes($sourceHeading, $targetHeading);
96
        self::moveChildNodes($sourceHeading, $targetHeading);
97
98
        return $targetHeading;
99
    }
100
101
    private static function copyAttributes(\DOMElement $source, \DOMElement $target)
102
    {
103
        foreach ($source->attributes as $attribute) {
104
            $target->setAttribute($attribute->name, $attribute->value);
105
        }
106
    }
107
108
    private static function moveChildNodes(\DOMElement $source, \DOMElement $target)
109
    {
110
        while ($source->hasChildNodes()) {
111
            // appendChild() actually moves the childNode
112
            $target->appendChild($source->childNodes->item(0));
113
        }
114
    }
115
}
116