Completed
Push — master ( 9f8b44...e0ac7a )
by Kevin
03:57
created

Tokenizer::createToken()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0872

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 41
ccs 29
cts 33
cp 0.8788
rs 6.7272
cc 7
eloc 26
nc 7
nop 1
crap 7.0872
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 74
    public function __construct(Configuration $configuration)
20
    {
21 74
        $this->configuration = $configuration;
22 74
    }
23
24 74
    public function tokenize($html)
25
    {
26 74
        $tokenizer = new HtmlTokenizer(false);
27 74
        $basicTokenCollection = $tokenizer->parse((string) $html);
28
29 74
        $tokenContainer = new TokenContainer($this->configuration);
30 74
        foreach ($basicTokenCollection as $basicToken) {
31 74
            $tokenContainer->appendChild($this->createToken($basicToken));
32 74
        }
33
34 74
        return $tokenContainer;
35
    }
36
37 74
    private function createToken(BasicToken $basicToken)
38
    {
39 74
        switch ($basicToken->getType()) {
40 74
        case 'cdata':
41 3
            return new CData(
42 3
                $this->configuration,
43 3
                $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\Php, 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...
44 74
            );
45
46 72
        case 'comment':
47 12
            return new Comment(
48 12
                $this->configuration,
49 12
                $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\Php, 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...
50 12
            );
51
52 70
        case 'doctype':
53 3
            return new DocType(
54 3
                $this->configuration,
55 3
                $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\Php, 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...
56 3
            );
57
58 68
        case 'element':
59 66
            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...
60
61 59
        case 'php':
62 1
            return new Php(
63 1
                $this->configuration,
64 1
                $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\Php, 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...
65 1
            );
66
67 58
        case 'text':
68 58
            return new Text(
69 58
                $this->configuration,
70 58
                $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\Php, 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 58
            );
72
        }
73
74
        throw new \RuntimeException(
75
            'Invalid token type: ' . $basicToken->getType()
76
        );
77
    }
78
79 66
    private function createElement(BasicElement $basicElement)
80
    {
81
        // Primary class name.
82
        $elementClassName = 'Groundskeeper\\Tokens\\Elements\\' .
83 66
            ucfirst(strtolower($basicElement->getName()));
84 66
        if (!class_exists($elementClassName)) {
85
            // Secondary class name.
86
            // For elements whose names conflict with PHP keywords: var
87
            $elementClassName = 'Groundskeeper\\Tokens\\Elements\\' .
88 7
                ucfirst(strtolower($basicElement->getName())) . 'Element';
89 7
            if (!class_exists($elementClassName)) {
90 7
                $elementClassName = 'Groundskeeper\\Tokens\\Element';
91 7
            }
92 7
        }
93
94 66
        $cleanableElement = new $elementClassName(
95 66
            $this->configuration,
96 66
            $basicElement->getName(),
97 66
            $basicElement->getAttributes()
98 66
        );
99
100 66
        foreach ($basicElement->getChildren() as $basicChild) {
101 64
            $cleanableElement->appendChild(
102 64
                $this->createToken($basicChild)
103 64
            );
104 66
        }
105
106 66
        return $cleanableElement;
107
    }
108
}
109