Completed
Push — master ( 0b908e...886262 )
by Kevin
02:30
created

Tokenizer::createElement()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0856
cc 3
eloc 14
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Groundskeeper\Tokens;
4
5
use Groundskeeper\Configuration;
6
use Groundskeeper\Tokens\Token as CleanableToken;
7
use Kevintweber\HtmlTokenizer\HtmlTokenizer;
8
use Kevintweber\HtmlTokenizer\Tokens\Element as BasicElement;
9
use Kevintweber\HtmlTokenizer\Tokens\Token as BasicToken;
10
11
class Tokenizer
12
{
13
    /** @var Configuration */
14
    private $configuration;
15
16
    /**
17
     * Constructor
18
     */
19 21
    public function __construct(Configuration $configuration)
20
    {
21 21
        $this->configuration = $configuration;
22 21
    }
23
24 21
    public function tokenize($html)
25
    {
26 21
        $tokenizer = new HtmlTokenizer(
27 21
            $this->configuration->get('error-strategy') == Configuration::ERROR_STRATEGY_THROW
28 21
        );
29 21
        $basicTokenCollection = $tokenizer->parse((string) $html);
30
31 21
        $tokenContainer = new TokenContainer($this->configuration);
32 21
        foreach ($basicTokenCollection as $basicToken) {
33 21
            $tokenContainer->addChild($this->createToken($basicToken));
34 21
        }
35
36 21
        return $tokenContainer;
37
    }
38
39 21
    private function createToken(BasicToken $basicToken, CleanableToken $parent = null)
40
    {
41 21
        switch ($basicToken->getType()) {
42 21
        case 'cdata':
43 2
            return new CData(
44 21
                $this->configuration,
45 2
                $parent,
46 2
                $basicToken->getValue()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kevintweber\HtmlTokenizer\Tokens\Token as the method getValue() does only exist in the following implementations of said interface: Kevintweber\HtmlTokenizer\Tokens\CData, Kevintweber\HtmlTokenizer\Tokens\Comment, Kevintweber\HtmlTokenizer\Tokens\DocType, Kevintweber\HtmlTokenizer\Tokens\Text.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
47 2
            );
48
49 19
        case 'comment':
50 2
            return new Comment(
51 2
                $this->configuration,
52 2
                $parent,
53 2
                $basicToken->getValue()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kevintweber\HtmlTokenizer\Tokens\Token as the method getValue() does only exist in the following implementations of said interface: Kevintweber\HtmlTokenizer\Tokens\CData, Kevintweber\HtmlTokenizer\Tokens\Comment, Kevintweber\HtmlTokenizer\Tokens\DocType, Kevintweber\HtmlTokenizer\Tokens\Text.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
54 2
            );
55
56 17
        case 'doctype':
57 2
            return new DocType(
58 2
                $this->configuration,
59 2
                $parent,
60 2
                $basicToken->getValue()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kevintweber\HtmlTokenizer\Tokens\Token as the method getValue() does only exist in the following implementations of said interface: Kevintweber\HtmlTokenizer\Tokens\CData, Kevintweber\HtmlTokenizer\Tokens\Comment, Kevintweber\HtmlTokenizer\Tokens\DocType, Kevintweber\HtmlTokenizer\Tokens\Text.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
61 2
            );
62
63 15
        case 'element':
64 13
            return $this->createElement($basicToken, $parent);
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...
65
66 6
        case 'text':
67 6
            return new Text(
68 6
                $this->configuration,
69 6
                $parent,
70 6
                $basicToken->getValue()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Kevintweber\HtmlTokenizer\Tokens\Token as the method getValue() does only exist in the following implementations of said interface: Kevintweber\HtmlTokenizer\Tokens\CData, Kevintweber\HtmlTokenizer\Tokens\Comment, Kevintweber\HtmlTokenizer\Tokens\DocType, Kevintweber\HtmlTokenizer\Tokens\Text.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
71 6
            );
72
        }
73
74
        throw new \RuntimeException(
75
            'Invalid token type: ' . $basicToken->getType()
76
        );
77
    }
78
79 13
    private function createElement(BasicElement $basicElement, CleanableToken $parent = null)
80
    {
81
        $elementClassName = 'Groundskeeper\\Tokens\\Elements\\' .
82 13
            ucfirst(strtolower($basicElement->getName()));
83 13
        if (!class_exists($elementClassName)) {
84 13
            $elementClassName = 'Groundskeeper\\Tokens\\Elements\\Element';
85 13
        }
86
87 13
        $cleanableElement = new $elementClassName(
88 13
            $this->configuration,
89 13
            $basicElement->getName(),
90 13
            $basicElement->getAttributes(),
91
            $parent
92 13
        );
93
94 13
        foreach ($basicElement->getChildren() as $basicChild) {
95 11
            $cleanableElement->addChild(
96 11
                $this->createToken($basicChild, $cleanableElement)
97 11
            );
98 13
        }
99
100 13
        return $cleanableElement;
101
    }
102
}
103