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 ( 8677b9...70949a )
by Viktor
01:40
created

HtmlHeadingNormalizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 79
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 34 6
A htmlContainsHeadings() 0 7 1
A headingTagNameToNumber() 0 4 1
A numberToHeadingLevel() 0 4 1
A cloneDomElementWithNewTagName() 0 8 1
A copyAttributes() 0 6 2
A moveChildNodes() 0 7 2
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