Test Failed
Pull Request — master (#11)
by
unknown
03:26
created

getVariableByIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Statement_Tokenizer} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Statement_Tokenizer
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode statement tokenizer: parses a mailcode statement
16
 * into its logical parts.
17
 *
18
 * @package Mailcode
19
 * @subpackage Parser
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class Mailcode_Parser_Statement_Info
23
{
24
    /**
25
     * @var Mailcode_Parser_Statement_Tokenizer
26
     */
27
    protected $tokenizer;
28
29
    /**
30
     * @var Mailcode_Parser_Statement_Tokenizer_Token[]
31
     */
32
    protected $tokens = array();
33
34
    /**
35
     * @var Mailcode_Parser_Statement_Info_Keywords
36
     */
37
    protected $keywords;
38
39
    /**
40
     * @var Mailcode_Parser_Statement_Info_Variables
41
     */
42
    protected $variables;
43
44
    public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer)
45
    {
46
        $this->tokenizer = $tokenizer;
47
        $this->keywords = new Mailcode_Parser_Statement_Info_Keywords($this, $this->tokenizer);
48
        $this->variables = new Mailcode_Parser_Statement_Info_Variables($this, $this->tokenizer);
49
    }
50
51
    /**
52
     * Whether the whole statement is a variable being assigned a value.
53
     *
54
     * @return bool
55
     */
56
    public function isVariableAssignment(): bool
57
    {
58
        return $this->variables->isAssignment();
59
    }
60
61
    /**
62
     * Whether the whole statement is a variable being compared to something.
63
     *
64
     * @return bool
65
     */
66
    public function isVariableComparison(): bool
67
    {
68
        return $this->variables->isComparison();
69
    }
70
71
    /**
72
     * Retrieves all variables used in the statement.
73
     *
74
     * @return Mailcode_Variables_Variable[]
75
     * @throws Mailcode_Parser_Exception
76
     */
77
    public function getVariables(): array
78
    {
79
        return $this->variables->getAll();
80
    }
81
82
    /**
83
     * Retrieves a variable by its position in the command's parameters.
84
     * Returns null if there is no parameter at the specified index, or
85
     * if it is of another type.
86
     *
87
     * @param int $index Zero-based index.
88
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
89
     */
90
    public function getVariableByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Variable
91
    {
92
        return $this->variables->getByIndex($index);
93
    }
94
95
    /**
96
     * Retrieves a string literal by its position in the command's parameters.
97
     * Returns null if there is no parameter at the specified index, or
98
     * if it is of another type.
99
     *
100
     * @param int $index Zero-based index.
101
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
102
     */
103
    public function getStringLiteralByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
104
    {
105
        $token = $this->getTokenByIndex($index);
106
107
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
108
            return $token;
109
        }
110
111
        return null;
112
    }
113
114
    /**
115
     * Retrieves a keyword by its position in the command's parameters.
116
     * Returns null if there is no parameter at the specified index, or
117
     * if it is of another type.
118
     *
119
     * @param int $index Zero-based index.
120
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL
121
     */
122
    public function getKeywordByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword
123
    {
124
        return $this->keywords->getByIndex($index);
125
    }
126
127
    /**
128
     * Retrieves an operand by its position in the command's parameters.
129
     * Returns null if there is no parameter at the specified index, or
130
     * if it is of another type.
131
     *
132
     * @param int $index Zero-based index.
133
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL
134
     */
135
    public function getOperandByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Operand
136
    {
137
        $token = $this->getTokenByIndex($index);
138
139
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) {
140
            return $token;
141
        }
142
143
        return null;
144
    }
145
146
    /**
147
     * Retrieves a parameter token by its position in the command's parameters,
148
     * regardless of its type. Returns null if there is no parameter at the
149
     * specified index.
150
     *
151
     * @param int $index Zero-based index.
152
     * @return Mailcode_Parser_Statement_Tokenizer_Token|NULL
153
     */
154
    public function getTokenByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token
155
    {
156
        $tokens = $this->tokenizer->getTokens();
157
158
        if (isset($tokens[$index])) {
159
            return $tokens[$index];
160
        }
161
162
        return null;
163
    }
164
165
    public function getTokenForKeyWord(string $keywordName): ?Mailcode_Parser_Statement_Tokenizer_Token
166
    {
167
        $tokens = $this->tokenizer->getTokens();
168
169
        $tokenCount = count($tokens);
170
171
        for ($index = 0; $index < $tokenCount; $index++) {
172
            $token = $tokens[$index];
173
174
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) {
175
                if ($token->getKeyword() == $keywordName) {
176
                    $tokenIndex = $index + 1;
177
                    if ($tokenIndex < $tokenCount) {
178
                        return $tokens[$tokenIndex];
179
                    }
180
                }
181
            }
182
        }
183
184
        return null;
185
    }
186
187
    public function hasTokenAtIndex(int $index): bool
188
    {
189
        $tokens = $this->tokenizer->getTokens();
190
191
        return isset($tokens[$index]);
192
    }
193
194
    /**
195
     * Retrieves all tokens.
196
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
197
     */
198
    public function getTokens(): array
199
    {
200
        return $this->tokenizer->getTokens();
201
    }
202
203
    /**
204
     * Retrieves all string literals that were found in the command.
205
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
206
     */
207
    public function getStringLiterals(): array
208
    {
209
        $result = array();
210
        $tokens = $this->tokenizer->getTokens();
211
212
        foreach ($tokens as $token) {
213
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
214
                $result[] = $token;
215
            }
216
        }
217
218
        return $result;
219
    }
220
221
    public function createPruner(): Mailcode_Parser_Statement_Info_Pruner
222
    {
223
        return new Mailcode_Parser_Statement_Info_Pruner($this);
224
    }
225
226
    /**
227
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[]
228
     */
229
    public function getKeywords(): array
230
    {
231
        return $this->keywords->getAll();
232
    }
233
234
    public function getKeywordsCollection(): Mailcode_Parser_Statement_Info_Keywords
235
    {
236
        return $this->keywords;
237
    }
238
239
    /**
240
     * Adds or removes a keyword depending on whether it should be enabled.
241
     *
242
     * @param string $keyword The keyword name, with or without :
243
     * @param bool $enabled
244
     * @return Mailcode_Parser_Statement_Info
245
     * @throws Mailcode_Parser_Exception
246
     */
247
    public function setKeywordEnabled(string $keyword, bool $enabled): Mailcode_Parser_Statement_Info
248
    {
249
        $this->keywords->setEnabled($keyword, $enabled);
250
251
        return $this;
252
    }
253
254
    /**
255
     * Adds a keyword to the command.
256
     *
257
     * @param string $keyword Keyword name, with or without :
258
     * @return $this
259
     * @throws Mailcode_Parser_Exception
260
     */
261
    public function addKeyword(string $keyword): Mailcode_Parser_Statement_Info
262
    {
263
        $this->keywords->add($keyword);
264
265
        return $this;
266
    }
267
268
    /**
269
     * Removes a keyword from the command, if it has one.
270
     * Has no effect otherwise.
271
     *
272
     * @param string $keyword Keyword name, with or without :
273
     * @return $this
274
     */
275
    public function removeKeyword(string $keyword): Mailcode_Parser_Statement_Info
276
    {
277
        $this->keywords->remove($keyword);
278
279
        return $this;
280
    }
281
282
    /**
283
     * Whether the command has the specified keyword.
284
     *
285
     * @param string $keyword Keyword name, with or without :
286
     * @return bool
287
     */
288
    public function hasKeyword(string $keyword): bool
289
    {
290
        return $this->keywords->hasKeyword($keyword);
291
    }
292
293
    public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token): void
294
    {
295
        $this->tokenizer->removeToken($token);
296
    }
297
298
    public function addStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
299
    {
300
        return $this->tokenizer->appendStringLiteral($text);
301
    }
302
303
    public function prependStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
304
    {
305
        return $this->tokenizer->prependStringLiteral($text);
306
    }
307
}
308