Completed
Push — master ( 2260a7...0929a3 )
by Kevin
06:11
created

Tokenizer::getElementClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Kevintweber\HtmlTokenizer\HtmlTokenizer;
7
use Kevintweber\HtmlTokenizer\Tokens\Element as BasicElement;
8
use Kevintweber\HtmlTokenizer\Tokens\Token as BasicToken;
9
10
class Tokenizer
11
{
12
    /** @var Configuration */
13
    private $configuration;
14
15
    /**
16
     * Constructor
17
     */
18 153
    public function __construct(Configuration $configuration)
19
    {
20 153
        $this->configuration = $configuration;
21 153
    }
22
23 153
    public function tokenize($html)
24
    {
25 153
        $tokenizer = new HtmlTokenizer(false);
26 153
        $basicTokenCollection = $tokenizer->parse((string) $html);
27
28 153
        $tokenContainer = new TokenContainer($this->configuration);
29 153
        foreach ($basicTokenCollection as $basicToken) {
30 153
            $tokenContainer->appendChild($this->createToken($basicToken));
31
        }
32
33 153
        return $tokenContainer;
34
    }
35
36 153
    private function createToken(BasicToken $basicToken)
37
    {
38 153
        switch ($basicToken->getType()) {
39 153
        case 'cdata':
40 3
            return new CData(
41 3
                $this->configuration,
42 3
                $basicToken->getLine(),
43 3
                $basicToken->getPosition(),
44 3
                $basicToken->getValue()
45
            );
46
47 151
        case 'comment':
48 23
            return new Comment(
49 23
                $this->configuration,
50 23
                $basicToken->getLine(),
51 23
                $basicToken->getPosition(),
52 23
                $basicToken->getValue()
53
            );
54
55 149
        case 'doctype':
56 3
            return new DocType(
57 3
                $this->configuration,
58 3
                $basicToken->getLine(),
59 3
                $basicToken->getPosition(),
60 3
                $basicToken->getValue()
61
            );
62
63 147
        case 'php':
64 1
            return new Php(
65 1
                $this->configuration,
66 1
                $basicToken->getLine(),
67 1
                $basicToken->getPosition(),
68 1
                $basicToken->getValue()
69
            );
70
71 147
        case 'text':
72 127
            return new Text(
73 127
                $this->configuration,
74 127
                $basicToken->getLine(),
75 127
                $basicToken->getPosition(),
76 127
                $basicToken->getValue()
77
            );
78
        }
79
80 145
        return $this->createElement($basicToken);
0 ignored issues
show
Compatibility introduced by
$basicToken of type object<Kevintweber\HtmlTokenizer\Tokens\Token> is not a sub-type of object<Kevintweber\HtmlTokenizer\Tokens\Element>. It seems like you assume a concrete implementation of the interface Kevintweber\HtmlTokenizer\Tokens\Token to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
81
    }
82
83 145
    private function createElement(BasicElement $basicElement)
84
    {
85 145
        $elementClassName = $this->getElementClassName($basicElement);
86 145
        $cleanableElement = new $elementClassName(
87 145
            $this->configuration,
88 145
            $basicElement->getLine(),
89 145
            $basicElement->getPosition(),
90 145
            $basicElement->getName(),
91 145
            $basicElement->getAttributes()
92
        );
93
94 145
        foreach ($basicElement->getChildren() as $basicChild) {
95 139
            $cleanableElement->appendChild(
96 139
                $this->createToken($basicChild)
97
            );
98
        }
99
100 145
        return $cleanableElement;
101
    }
102
103 145
    private function getElementClassName(BasicElement $basicElement) : string
104
    {
105
        $elementClassName = 'Groundskeeper\\Tokens\\Elements\\' .
106 145
            ucfirst(strtolower($basicElement->getName()));
107 145
        if (!class_exists($elementClassName)) {
108
            // Secondary class name.
109
            // For elements whose names conflict with PHP keywords: var
110
            $elementClassName = 'Groundskeeper\\Tokens\\Elements\\' .
111 8
                ucfirst(strtolower($basicElement->getName())) . 'Element';
112 8
            if (!class_exists($elementClassName)) {
113 8
                $elementClassName = 'Groundskeeper\\Tokens\\Element';
114
            }
115
        }
116
117 145
        return $elementClassName;
118
    }
119
}
120