Passed
Pull Request — master (#11)
by Sebastian
06:57 queued 03:04
created

Mailcode_Parser_Statement_Info::hasKeyword()   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
c 0
b 0
f 0
nc 1
nop 1
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 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
        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...
170
            $token = $tokens[$index];
171
172
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) {
173
                if ($token->getKeyword() == $keywordName) {
174
                    $tokenIndex = $index + 1;
175
                    if (isset($tokens[$tokenIndex])) {
176
                        return $tokens[$tokenIndex];
177
                    }
178
                }
179
            }
180
        }
181
182
        return null;
183
    }
184
185
    public function hasTokenAtIndex(int $index): bool
186
    {
187
        $tokens = $this->tokenizer->getTokens();
188
189
        return isset($tokens[$index]);
190
    }
191
192
    /**
193
     * Retrieves all tokens.
194
     * @return Mailcode_Parser_Statement_Tokenizer_Token[]
195
     */
196
    public function getTokens(): array
197
    {
198
        return $this->tokenizer->getTokens();
199
    }
200
201
    /**
202
     * Retrieves all string literals that were found in the command.
203
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
204
     */
205
    public function getStringLiterals(): array
206
    {
207
        $result = array();
208
        $tokens = $this->tokenizer->getTokens();
209
210
        foreach ($tokens as $token) {
211
            if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
212
                $result[] = $token;
213
            }
214
        }
215
216
        return $result;
217
    }
218
219
    public function createPruner(): Mailcode_Parser_Statement_Info_Pruner
220
    {
221
        return new Mailcode_Parser_Statement_Info_Pruner($this);
222
    }
223
224
    /**
225
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[]
226
     */
227
    public function getKeywords(): array
228
    {
229
        return $this->keywords->getAll();
230
    }
231
232
    public function getKeywordsCollection(): Mailcode_Parser_Statement_Info_Keywords
233
    {
234
        return $this->keywords;
235
    }
236
237
    /**
238
     * Adds or removes a keyword depending on whether it should be enabled.
239
     *
240
     * @param string $keyword The keyword name, with or without :
241
     * @param bool $enabled
242
     * @return Mailcode_Parser_Statement_Info
243
     * @throws Mailcode_Parser_Exception
244
     */
245
    public function setKeywordEnabled(string $keyword, bool $enabled): Mailcode_Parser_Statement_Info
246
    {
247
        $this->keywords->setEnabled($keyword, $enabled);
248
249
        return $this;
250
    }
251
252
    /**
253
     * Adds a keyword to the command.
254
     *
255
     * @param string $keyword Keyword name, with or without :
256
     * @return $this
257
     * @throws Mailcode_Parser_Exception
258
     */
259
    public function addKeyword(string $keyword): Mailcode_Parser_Statement_Info
260
    {
261
        $this->keywords->add($keyword);
262
263
        return $this;
264
    }
265
266
    /**
267
     * Removes a keyword from the command, if it has one.
268
     * Has no effect otherwise.
269
     *
270
     * @param string $keyword Keyword name, with or without :
271
     * @return $this
272
     */
273
    public function removeKeyword(string $keyword): Mailcode_Parser_Statement_Info
274
    {
275
        $this->keywords->remove($keyword);
276
277
        return $this;
278
    }
279
280
    /**
281
     * Whether the command has the specified keyword.
282
     *
283
     * @param string $keyword Keyword name, with or without :
284
     * @return bool
285
     */
286
    public function hasKeyword(string $keyword): bool
287
    {
288
        return $this->keywords->hasKeyword($keyword);
289
    }
290
291
    public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token): void
292
    {
293
        $this->tokenizer->removeToken($token);
294
    }
295
296
    public function addStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
297
    {
298
        return $this->tokenizer->appendStringLiteral($text);
299
    }
300
301
    public function prependStringLiteral(string $text): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
302
    {
303
        return $this->tokenizer->prependStringLiteral($text);
304
    }
305
}
306