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 ( 475742...5b4d94 )
by Viktor
01:42
created

HtmlHeadingNormalizer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

10 Methods

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