Passed
Pull Request — master (#13)
by Sebastian
04:02
created

Mailcode_Parser_Statement_Info   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 65
dl 0
loc 333
rs 9.36
c 9
b 0
f 0
wmc 38

28 Methods

Rating   Name   Duplication   Size   Complexity  
A getVariables() 0 3 1
A isVariableAssignment() 0 3 1
A isVariableComparison() 0 3 1
A __construct() 0 5 1
A getVariableByIndex() 0 3 1
A getStringLiteralByIndex() 0 9 2
A getKeywordByIndex() 0 3 1
A getOperandByIndex() 0 9 2
A addKeyword() 0 5 1
A getStringLiterals() 0 12 3
A getTokenForKeyWord() 0 18 5
A hasTokenAtIndex() 0 5 1
A getKeywordsCollection() 0 3 1
A getKeywords() 0 3 1
A addNumber() 0 3 1
A createPruner() 0 3 1
A addVariable() 0 3 1
A addStringLiteral() 0 3 1
A getTokenByParamName() 0 13 3
A setKeywordEnabled() 0 5 1
A getTokenByIndex() 0 5 1
A hasKeyword() 0 3 1
A removeKeyword() 0 5 1
A getTokens() 0 3 1
A removeToken() 0 3 1
A prependStringLiteral() 0 3 1
A setParamName() 0 3 1
A addParamString() 0 5 1
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
        return $tokens[$index] ?? null;
159
    }
160
161
    public function getTokenForKeyWord(string $keywordName): ?Mailcode_Parser_Statement_Tokenizer_Token
162
    {
163
        $tokens = $this->tokenizer->getTokens();
164
165
        for ($index = 0; $index < count($tokens); $index++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
166
            $token = $tokens[$index];
167
168
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) {
169
                if ($token->getKeyword() == $keywordName) {
170
                    $tokenIndex = $index + 1;
171
                    if (isset($tokens[$tokenIndex])) {
172
                        return $tokens[$tokenIndex];
173
                    }
174
                }
175
            }
176
        }
177
178
        return null;
179
    }
180
181
    public function hasTokenAtIndex(int $index): bool
182
    {
183
        $tokens = $this->tokenizer->getTokens();
184
185
        return isset($tokens[$index]);
186
    }
187
188
    /**
189
     * Retrieves all tokens.
190
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
191
     */
192
    public function getTokens(): array
193
    {
194
        return $this->tokenizer->getTokens();
195
    }
196
197
    /**
198
     * Retrieves all string literals that were found in the command.
199
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
200
     */
201
    public function getStringLiterals(): array
202
    {
203
        $result = array();
204
        $tokens = $this->tokenizer->getTokens();
205
206
        foreach ($tokens as $token) {
207
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
208
                $result[] = $token;
209
            }
210
        }
211
212
        return $result;
213
    }
214
215
    public function createPruner(): Mailcode_Parser_Statement_Info_Pruner
216
    {
217
        return new Mailcode_Parser_Statement_Info_Pruner($this);
218
    }
219
220
    /**
221
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[]
222
     */
223
    public function getKeywords(): array
224
    {
225
        return $this->keywords->getAll();
226
    }
227
228
    public function getKeywordsCollection(): Mailcode_Parser_Statement_Info_Keywords
229
    {
230
        return $this->keywords;
231
    }
232
233
    /**
234
     * Adds or removes a keyword depending on whether it should be enabled.
235
     *
236
     * @param string $keyword The keyword name, with or without :
237
     * @param bool $enabled
238
     * @return Mailcode_Parser_Statement_Info
239
     * @throws Mailcode_Parser_Exception
240
     */
241
    public function setKeywordEnabled(string $keyword, bool $enabled): Mailcode_Parser_Statement_Info
242
    {
243
        $this->keywords->setEnabled($keyword, $enabled);
244
245
        return $this;
246
    }
247
248
    /**
249
     * Adds a keyword to the command.
250
     *
251
     * @param string $keyword Keyword name, with or without :
252
     * @return $this
253
     * @throws Mailcode_Parser_Exception
254
     */
255
    public function addKeyword(string $keyword): Mailcode_Parser_Statement_Info
256
    {
257
        $this->keywords->add($keyword);
258
259
        return $this;
260
    }
261
262
    /**
263
     * Removes a keyword from the command, if it has one.
264
     * Has no effect otherwise.
265
     *
266
     * @param string $keyword Keyword name, with or without :
267
     * @return $this
268
     */
269
    public function removeKeyword(string $keyword): Mailcode_Parser_Statement_Info
270
    {
271
        $this->keywords->remove($keyword);
272
273
        return $this;
274
    }
275
276
    /**
277
     * Whether the command has the specified keyword.
278
     *
279
     * @param string $keyword Keyword name, with or without :
280
     * @return bool
281
     */
282
    public function hasKeyword(string $keyword): bool
283
    {
284
        return $this->keywords->hasKeyword($keyword);
285
    }
286
287
    public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token): void
288
    {
289
        $this->tokenizer->removeToken($token);
290
    }
291
292
    public function addVariable(Mailcode_Variables_Variable $variable): Mailcode_Parser_Statement_Tokenizer_Token_Variable
293
    {
294
        return $this->tokenizer->appendVariable($variable);
295
    }
296
297
    public function addStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
298
    {
299
        return $this->tokenizer->appendStringLiteral($text);
300
    }
301
302
    public function addNumber(string $number): Mailcode_Parser_Statement_Tokenizer_Token_Number
303
    {
304
        return $this->tokenizer->appendNumber($number);
305
    }
306
307
    public function prependStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
308
    {
309
        return $this->tokenizer->prependStringLiteral($text);
310
    }
311
312
    public function getTokenByParamName(string $name) : ?Mailcode_Parser_Statement_Tokenizer_Token
313
    {
314
        $tokens = $this->tokenizer->getTokens();
315
316
        foreach($tokens as $token)
317
        {
318
            if($token->getName() === $name)
319
            {
320
                return $token;
321
            }
322
        }
323
324
        return null;
325
    }
326
327
    /**
328
     * Adds a parameter name for the target token, replacing any
329
     * existing name token if any.
330
     *
331
     * @param Mailcode_Parser_Statement_Tokenizer_Token $targetToken
332
     * @param string $name
333
     * @return Mailcode_Parser_Statement_Tokenizer_Token_ParamName
334
     *
335
     * @throws Mailcode_Parser_Exception {@see Mailcode_Parser_Statement_Tokenizer::ERROR_TARGET_INSERT_TOKEN_NOT_FOUND}
336
     */
337
    public function setParamName(Mailcode_Parser_Statement_Tokenizer_Token $targetToken, string $name) : Mailcode_Parser_Statement_Tokenizer_Token_ParamName
338
    {
339
        return $this->tokenizer->injectParamName($targetToken, $name);
340
    }
341
342
    /**
343
     * Adds a parameter with a string-based value.
344
     *
345
     * @param string $paramName
346
     * @param string $value
347
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
348
     * @throws Mailcode_Parser_Exception
349
     */
350
    public function addParamString(string $paramName, string $value) : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
351
    {
352
        $token = $this->addStringLiteral($value);
353
        $this->setParamName($token, $paramName);
354
        return $token;
355
    }
356
}
357