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   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
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 91
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 containsHeadings() 0 6 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
        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