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

Tokenizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 93.65%

Importance

Changes 8
Bugs 2 Features 4
Metric Value
wmc 14
c 8
b 2
f 4
lcom 1
cbo 9
dl 0
loc 98
ccs 59
cts 63
cp 0.9365
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A tokenize() 0 12 2
C createToken() 0 41 7
B createElement() 0 29 4
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