Test Failed
Push — master ( 36c5b6...af11e6 )
by Sebastian
04:51
created

Mailcode_Parser_Statement_Info::removeToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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->tokens = $this->tokenizer->getTokens();
48
        $this->keywords = new Mailcode_Parser_Statement_Info_Keywords($this, $this->tokenizer);
49
        $this->variables = new Mailcode_Parser_Statement_Info_Variables($this, $this->tokenizer);
50
51
        // Add a listener to be informed when tokens are added or removed
52
        $this->tokenizer->onTokensChanged(array($this, 'handleTokensChanged'));
53
    }
54
    
55
   /**
56
    * Whether the whole statement is a variable being assigned a value.
57
    * 
58
    * @return bool
59
    */
60
    public function isVariableAssignment() : bool
61
    {
62
        return $this->variables->isAssignment();
63
    }
64
    
65
   /**
66
    * Whether the whole statement is a variable being compared to something.
67
    * 
68
    * @return bool
69
    */
70
    public function isVariableComparison() : bool
71
    {
72
        return $this->variables->isComparison();
73
    }
74
75
    /**
76
     * Retrieves all variables used in the statement.
77
     *
78
     * @return Mailcode_Variables_Variable[]
79
     * @throws Mailcode_Exception
80
     */
81
    public function getVariables() : array
82
    {
83
        return $this->variables->getAll();
84
    }
85
    
86
   /**
87
    * Retrieves a variable by its position in the command's parameters.
88
    * Returns null if there is no parameter at the specified index, or
89
    * if it is of another type.
90
    * 
91
    * @param int $index Zero-based index.
92
    * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
93
    */
94
    public function getVariableByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable
95
    {
96
        return $this->variables->getByIndex($index);
97
    }
98
    
99
   /**
100
    * Retrieves a string literal by its position in the command's parameters.
101
    * Returns null if there is no parameter at the specified index, or
102
    * if it is of another type.
103
    *
104
    * @param int $index Zero-based index.
105
    * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
106
    */
107
    public function getStringLiteralByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
108
    {
109
        $token = $this->getTokenByIndex($index);
110
        
111
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral)
112
        {
113
            return $token;
114
        }
115
        
116
        return null;
117
    }
118
    
119
   /**
120
    * Retrieves a keyword by its position in the command's parameters.
121
    * Returns null if there is no parameter at the specified index, or
122
    * if it is of another type.
123
    *
124
    * @param int $index Zero-based index.
125
    * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL
126
    */
127
    public function getKeywordByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword
128
    {
129
        return $this->keywords->getByIndex($index);
130
    }
131
    
132
   /**
133
    * Retrieves an operand by its position in the command's parameters.
134
    * Returns null if there is no parameter at the specified index, or
135
    * if it is of another type.
136
    *
137
    * @param int $index Zero-based index.
138
    * @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL
139
    */
140
    public function getOperandByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Operand
141
    {
142
        $token = $this->getTokenByIndex($index);
143
        
144
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand)
145
        {
146
            return $token;
147
        }
148
        
149
        return null;
150
    }
151
    
152
   /**
153
    * Retrieves a parameter token by its position in the command's parameters,
154
    * regardless of its type. Returns null if there is no parameter at the 
155
    * specified index.
156
    *
157
    * @param int $index Zero-based index.
158
    * @return Mailcode_Parser_Statement_Tokenizer_Token|NULL
159
    */
160
    public function getTokenByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token
161
    {
162
        if(isset($this->tokens[$index]))
163
        {
164
            return $this->tokens[$index];
165
        }
166
        
167
        return null;
168
    }
169
    
170
    public function hasTokenAtIndex(int $index) : bool
171
    {
172
        return isset($this->tokens[$index]);
173
    }
174
    
175
   /**
176
    * Retrieves all tokens.
177
    * @return Mailcode_Parser_Statement_Tokenizer_Token[]
178
    */
179
    public function getTokens() : array
180
    {
181
        return $this->tokens;
182
    }
183
    
184
   /**
185
    * Retrieves all string literals that were found in the command.
186
    * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
187
    */
188
    public function getStringLiterals() : array
189
    {
190
        $result = array();
191
        
192
        foreach($this->tokens as $token)
193
        {
194
            if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral)
195
            {
196
                $result[] = $token;
197
            }
198
        }
199
        
200
        return $result;
201
    }
202
    
203
    public function createPruner() : Mailcode_Parser_Statement_Info_Pruner
204
    {
205
        return new Mailcode_Parser_Statement_Info_Pruner($this);
206
    }
207
208
    /**
209
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[]
210
     */
211
    public function getKeywords() : array
212
    {
213
        return $this->keywords->getAll();
214
    }
215
216
    /**
217
     * Adds or removes a keyword dependin on whether it should be enabled.
218
     *
219
     * @param string $keyword The keyword name, with or without :
220
     * @param bool $enabled
221
     * @return Mailcode_Parser_Statement_Info
222
     * @throws Mailcode_Exception
223
     */
224
    public function setKeywordEnabled(string $keyword, bool $enabled) : Mailcode_Parser_Statement_Info
225
    {
226
        $this->keywords->setEnabled($keyword, $enabled);
227
228
        return $this;
229
    }
230
231
    /**
232
     * Adds a keyword to the command.
233
     *
234
     * @param string $keyword Keyword name, with or without :
235
     * @return $this
236
     * @throws Mailcode_Exception
237
     */
238
    public function addKeyword(string $keyword) : Mailcode_Parser_Statement_Info
239
    {
240
        $this->keywords->add($keyword);
241
242
        return $this;
243
    }
244
245
    /**
246
     * Removes a keyword from the command, if it has one.
247
     * Has no effect otherwise.
248
     *
249
     * @param string $keyword Keyword name, with or without :
250
     * @return $this
251
     */
252
    public function removeKeyword(string $keyword) : Mailcode_Parser_Statement_Info
253
    {
254
        $this->keywords->remove($keyword);
255
256
        return $this;
257
    }
258
259
    /**
260
     * Whether the command has the specified keyword.
261
     *
262
     * @param string $keyword Keyword name, with or without :
263
     * @return bool
264
     */
265
    public function hasKeyword(string $keyword) : bool
266
    {
267
        return $this->keywords->hasKeyword($keyword);
268
    }
269
270
    /**
271
     * Called whenever the token collection in the tokenizer has changed.
272
     */
273
    public function handleTokensChanged() : void
274
    {
275
        $this->tokens = $this->tokenizer->getTokens();
276
    }
277
278
    public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token) : void
279
    {
280
        $this->tokenizer->removeToken($token);
281
    }
282
}
283