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 ( c6e89e...1f8c61 )
by Viktor
01:37
created

HtmlHeadingNormalizer   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 0
dl 0
loc 92
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 18 3
A getHeadings() 0 14 3
A normalizeHeadings() 0 13 2
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, $baseLevel = 1)
8
    {
9
        if (!self::htmlContainsHeadings($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
            $currentHeadingLevel = self::headingTagNameToNumber($heading->tagName);
47
            $newHeadingLevel = self::numberToHeadingLevel($baseLevel + $currentHeadingLevel - 1);
48
49
            $normalizedHeadings[] = self::cloneDomElementWithNewTagName($heading, $newHeadingLevel);
50
        }
51
52
        return $normalizedHeadings;
53
    }
54
55
    private static function htmlContainsHeadings($html)
56
    {
57
        $headingNeedle = '<h';
58
        $containsHeadings = (false !== stripos($html, $headingNeedle));
59
60
        return $containsHeadings;
61
    }
62
63
    private static function headingTagNameToNumber($headingLevel)
64
    {
65
        return substr($headingLevel, 1);
66
    }
67
68
    private static function numberToHeadingLevel($number)
69
    {
70
        return 'h'.$number;
71
    }
72
73
    private static function cloneDomElementWithNewTagName(\DOMElement $sourceDomElement, $newTagName)
74
    {
75
        $targetDomElement = $sourceDomElement->parentNode->ownerDocument->createElement($newTagName);
76
        self::copyAttributes($sourceDomElement, $targetDomElement);
77
        self::moveChildNodes($sourceDomElement, $targetDomElement);
78
79
        return $targetDomElement;
80
    }
81
82
    private static function copyAttributes(\DOMElement $source, \DOMElement $target)
83
    {
84
        foreach ($source->attributes as $attribute) {
85
            $target->setAttribute($attribute->name, $attribute->value);
86
        }
87
    }
88
89
    private static function moveChildNodes(\DOMElement $source, \DOMElement $target)
90
    {
91
        while ($source->hasChildNodes()) {
92
            // appendChild() actually moves the childNode
93
            $target->appendChild($source->childNodes->item(0));
94
        }
95
    }
96
}
97