TokenMatcherTemplate::createContext()
last analyzed

Size

Total Lines 114
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
nc 1
nop 2
dl 0
loc 114
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TokenMatcherTemplate.php$0 ➔ getBuffer() 0 3 1
A TokenMatcherTemplate.php$0 ➔ getSymbolString() 0 7 2
A TokenMatcherTemplate.php$0 ➔ setNewToken() 0 5 1
A TokenMatcherTemplate.php$0 ➔ getSymbolList() 0 7 2
A TokenMatcherTemplate.php$0 ➔ setMode() 0 5 1
A TokenMatcherTemplate.php$0 ➔ setTokenAttribute() 0 7 1
A TokenMatcherTemplate.php$0 ➔ getMode() 0 3 1
A TokenMatcherTemplate.php$0 ➔ getToken() 0 3 1
A TokenMatcherTemplate.php$0 ➔ __construct() 0 14 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Lexer;
6
7
use Remorhaz\UniLex\Exception;
8
use Remorhaz\UniLex\IO\CharBufferInterface;
9
use Remorhaz\UniLex\IO\TokenExtractInterface;
10
11
abstract class TokenMatcherTemplate implements TokenMatcherInterface
12
{
13
    private $token;
14
15
    private $mode = self::DEFAULT_MODE;
16
17
    /**
18
     * @return Token
19
     * @throws Exception
20
     */
21
    public function getToken(): Token
22
    {
23
        if (!isset($this->token)) {
24
            throw new Exception("Token is not defined");
25
        }
26
27
        return $this->token;
28
    }
29
30
    protected function createContext(
31
        CharBufferInterface $buffer,
32
        TokenFactoryInterface $tokenFactory
33
    ): TokenMatcherContextInterface {
34
        $onConstruct = function (): void {
35
            unset($this->token);
36
        };
37
        $onSetNewToken = function (int $tokenType) use ($tokenFactory): void {
38
            $this->token = $tokenFactory->createToken($tokenType);
39
        };
40
        $onGetToken = function (): Token {
41
            return $this->getToken();
42
        };
43
        $onSetMode = function (string $mode): void {
44
            $this->mode = $mode;
45
        };
46
        $onGetMode = function (): string {
47
            return $this->mode;
48
        };
49
50
        return new class (
51
            $buffer,
52
            $onConstruct,
53
            $onSetNewToken,
54
            $onGetToken,
55
            $onSetMode,
56
            $onGetMode
57
        ) implements TokenMatcherContextInterface
58
        {
59
            private $buffer;
60
61
            private $onSetNewToken;
62
63
            private $onGetToken;
64
65
            private $onSetMode;
66
67
            private $onGetMode;
68
69
            public function __construct(
70
                CharBufferInterface $buffer,
71
                callable $onConstruct,
72
                callable $onSetNewToken,
73
                callable $onGetToken,
74
                callable $onSetMode,
75
                callable $onGetMode
76
            ) {
77
                $this->buffer = $buffer;
78
                $this->onSetNewToken = $onSetNewToken;
79
                $this->onGetToken = $onGetToken;
80
                $this->onSetMode = $onSetMode;
81
                $this->onGetMode = $onGetMode;
82
                call_user_func($onConstruct);
83
            }
84
85
            public function setNewToken(int $tokenType): TokenMatcherContextInterface
86
            {
87
                call_user_func($this->onSetNewToken, $tokenType);
88
89
                return $this;
90
            }
91
92
            /**
93
             * @param string $name
94
             * @param        $value
95
             * @return TokenMatcherContextInterface
96
             */
97
            public function setTokenAttribute(string $name, $value): TokenMatcherContextInterface
98
            {
99
                $this
100
                    ->getToken()
101
                    ->setAttribute($name, $value);
102
103
                return $this;
104
            }
105
106
            public function getToken(): Token
107
            {
108
                return call_user_func($this->onGetToken);
109
            }
110
111
            public function getBuffer(): CharBufferInterface
112
            {
113
                return $this->buffer;
114
            }
115
116
            public function getSymbolString(): string
117
            {
118
                $buffer = $this->getBuffer();
119
                if ($buffer instanceof TokenExtractInterface) {
120
                    return $buffer->getTokenAsString();
121
                }
122
                throw new Exception("Extracting strings is not supported by buffer");
123
            }
124
125
            public function getSymbolList(): array
126
            {
127
                $buffer = $this->getBuffer();
128
                if ($buffer instanceof TokenExtractInterface) {
129
                    return $buffer->getTokenAsArray();
130
                }
131
                throw new Exception("Extracting arrays is not supported by buffer");
132
            }
133
134
            public function getMode(): string
135
            {
136
                return call_user_func($this->onGetMode);
137
            }
138
139
            public function setMode(string $mode): TokenMatcherContextInterface
140
            {
141
                call_user_func($this->onSetMode, $mode);
142
143
                return $this;
144
            }
145
        };
146
    }
147
}
148