Mailcode_Parser_Statement_Info::setParamName()   A
last analyzed

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
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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 all variables used in the statement.
84
     *
85
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable[]
86
     */
87
    public function getVariableTokens(): array
88
    {
89
        return $this->variables->getTokens();
90
    }
91
92
    /**
93
     * Retrieves a variable by its position in the command's parameters.
94
     * Returns null if there is no parameter at the specified index, or
95
     * if it is of another type.
96
     *
97
     * @param int $index Zero-based index.
98
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
99
     */
100
    public function getVariableByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Variable
101
    {
102
        return $this->variables->getByIndex($index);
103
    }
104
105
    /**
106
     * Retrieves a string literal by its position in the command's parameters.
107
     * Returns null if there is no parameter at the specified index, or
108
     * if it is of another type.
109
     *
110
     * @param int $index Zero-based index.
111
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
112
     */
113
    public function getStringLiteralByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
114
    {
115
        $token = $this->getTokenByIndex($index);
116
117
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
118
            return $token;
119
        }
120
121
        return null;
122
    }
123
124
    /**
125
     * Retrieves a keyword by its position in the command's parameters.
126
     * Returns null if there is no parameter at the specified index, or
127
     * if it is of another type.
128
     *
129
     * @param int $index Zero-based index.
130
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL
131
     */
132
    public function getKeywordByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword
133
    {
134
        return $this->keywords->getByIndex($index);
135
    }
136
137
    /**
138
     * Retrieves an operand by its position in the command's parameters.
139
     * Returns null if there is no parameter at the specified index, or
140
     * if it is of another type.
141
     *
142
     * @param int $index Zero-based index.
143
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL
144
     */
145
    public function getOperandByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token_Operand
146
    {
147
        $token = $this->getTokenByIndex($index);
148
149
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) {
150
            return $token;
151
        }
152
153
        return null;
154
    }
155
156
    /**
157
     * Retrieves a parameter token by its position in the command's parameters,
158
     * regardless of its type. Returns null if there is no parameter at the
159
     * specified index.
160
     *
161
     * @param int $index Zero-based index.
162
     * @return Mailcode_Parser_Statement_Tokenizer_Token|NULL
163
     */
164
    public function getTokenByIndex(int $index): ?Mailcode_Parser_Statement_Tokenizer_Token
165
    {
166
        $tokens = $this->tokenizer->getTokens();
167
168
        return $tokens[$index] ?? null;
169
    }
170
171
    public function getTokenForKeyWord(string $keywordName): ?Mailcode_Parser_Statement_Tokenizer_Token
172
    {
173
        $tokens = $this->tokenizer->getTokens();
174
175
        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...
176
            $token = $tokens[$index];
177
178
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) {
179
                if ($token->getKeyword() == $keywordName) {
180
                    $tokenIndex = $index + 1;
181
                    if (isset($tokens[$tokenIndex])) {
182
                        return $tokens[$tokenIndex];
183
                    }
184
                }
185
            }
186
        }
187
188
        return null;
189
    }
190
191
    public function hasTokenAtIndex(int $index): bool
192
    {
193
        $tokens = $this->tokenizer->getTokens();
194
195
        return isset($tokens[$index]);
196
    }
197
198
    /**
199
     * Retrieves all tokens.
200
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
201
     */
202
    public function getTokens(): array
203
    {
204
        return $this->tokenizer->getTokens();
205
    }
206
207
    /**
208
     * Retrieves all string literals that were found in the command.
209
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
210
     */
211
    public function getStringLiterals(): array
212
    {
213
        $result = array();
214
        $tokens = $this->tokenizer->getTokens();
215
216
        foreach ($tokens as $token) {
217
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
218
                $result[] = $token;
219
            }
220
        }
221
222
        return $result;
223
    }
224
225
    public function createPruner(): Mailcode_Parser_Statement_Info_Pruner
226
    {
227
        return new Mailcode_Parser_Statement_Info_Pruner($this);
228
    }
229
230
    /**
231
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[]
232
     */
233
    public function getKeywords(): array
234
    {
235
        return $this->keywords->getAll();
236
    }
237
238
    public function getKeywordsCollection(): Mailcode_Parser_Statement_Info_Keywords
239
    {
240
        return $this->keywords;
241
    }
242
243
    /**
244
     * Adds or removes a keyword depending on whether it should be enabled.
245
     *
246
     * @param string $keyword The keyword name, with or without :
247
     * @param bool $enabled
248
     * @return Mailcode_Parser_Statement_Info
249
     * @throws Mailcode_Parser_Exception
250
     */
251
    public function setKeywordEnabled(string $keyword, bool $enabled): Mailcode_Parser_Statement_Info
252
    {
253
        $this->keywords->setEnabled($keyword, $enabled);
254
255
        return $this;
256
    }
257
258
    /**
259
     * Adds a keyword to the command.
260
     *
261
     * @param string $keyword Keyword name, with or without :
262
     * @return $this
263
     * @throws Mailcode_Parser_Exception
264
     */
265
    public function addKeyword(string $keyword): Mailcode_Parser_Statement_Info
266
    {
267
        $this->keywords->add($keyword);
268
269
        return $this;
270
    }
271
272
    /**
273
     * Removes a keyword from the command, if it has one.
274
     * Has no effect otherwise.
275
     *
276
     * @param string $keyword Keyword name, with or without :
277
     * @return $this
278
     */
279
    public function removeKeyword(string $keyword): Mailcode_Parser_Statement_Info
280
    {
281
        $this->keywords->remove($keyword);
282
283
        return $this;
284
    }
285
286
    /**
287
     * Whether the command has the specified keyword.
288
     *
289
     * @param string $keyword Keyword name, with or without :
290
     * @return bool
291
     */
292
    public function hasKeyword(string $keyword): bool
293
    {
294
        return $this->keywords->hasKeyword($keyword);
295
    }
296
297
    public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token): void
298
    {
299
        $this->tokenizer->removeToken($token);
300
    }
301
302
    public function addVariable(Mailcode_Variables_Variable $variable): Mailcode_Parser_Statement_Tokenizer_Token_Variable
303
    {
304
        return $this->tokenizer->appendVariable($variable);
305
    }
306
307
    public function addStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
308
    {
309
        return $this->tokenizer->appendStringLiteral($text);
310
    }
311
312
    public function addNumber(string $number): Mailcode_Parser_Statement_Tokenizer_Token_Number
313
    {
314
        return $this->tokenizer->appendNumber($number);
315
    }
316
317
    public function prependStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
318
    {
319
        return $this->tokenizer->prependStringLiteral($text);
320
    }
321
322
    public function getTokenByParamName(string $name): ?Mailcode_Parser_Statement_Tokenizer_Token
323
    {
324
        $tokens = $this->tokenizer->getTokens();
325
326
        foreach ($tokens as $token) {
327
            if ($token->getName() === $name) {
328
                return $token;
329
            }
330
        }
331
332
        return null;
333
    }
334
335
    /**
336
     * Adds a parameter name for the target token, replacing any
337
     * existing name token if any.
338
     *
339
     * @param Mailcode_Parser_Statement_Tokenizer_Token $targetToken
340
     * @param string $name
341
     * @return Mailcode_Parser_Statement_Tokenizer_Token_ParamName
342
     *
343
     * @throws Mailcode_Parser_Exception {@see Mailcode_Parser_Statement_Tokenizer::ERROR_TARGET_INSERT_TOKEN_NOT_FOUND}
344
     */
345
    public function setParamName(Mailcode_Parser_Statement_Tokenizer_Token $targetToken, string $name): Mailcode_Parser_Statement_Tokenizer_Token_ParamName
346
    {
347
        return $this->tokenizer->injectParamName($targetToken, $name);
348
    }
349
350
    /**
351
     * Adds a parameter with a string-based value.
352
     *
353
     * @param string $paramName
354
     * @param string $value
355
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
356
     * @throws Mailcode_Parser_Exception
357
     */
358
    public function addParamString(string $paramName, string $value): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
359
    {
360
        $token = $this->addStringLiteral($value);
361
        $this->setParamName($token, $paramName);
362
        return $token;
363
    }
364
}
365