Passed
Push — master ( 2f714f...1c67eb )
by Sebastian
02:15
created

Mailcode_Parser_Statement_Info::getVariables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
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
    public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer)
35
    {
36
        $this->tokenizer = $tokenizer;
37
        $this->tokens = $this->tokenizer->getTokens(); 
38
    }
39
    
40
   /**
41
    * Whether the whole statement is a variable being assigned a value.
42
    * 
43
    * @return bool
44
    */
45
    public function isVariableAssignment() : bool
46
    {
47
        $variable = $this->getVariableByIndex(0);
48
        $operand = $this->getOperandByIndex(1);
49
        $value = $this->getTokenByIndex(2);
50
        
51
        if($variable && $operand && $value && $operand->isAssignment())
52
        {
53
            return true;
54
        }
55
        
56
        return false;
57
    }
58
    
59
   /**
60
    * Whether the whole statement is a variable being compared to something.
61
    * 
62
    * @return bool
63
    */
64
    public function isVariableComparison() : bool
65
    {
66
        $variable = $this->getVariableByIndex(0);
67
        $operand = $this->getOperandByIndex(1);
68
        $value = $this->getTokenByIndex(2);
69
        
70
        if($variable && $operand && $value && $operand->isComparator())
71
        {
72
            return true;
73
        }
74
        
75
        return false;
76
    }
77
    
78
   /**
79
    * Retrieves all variables used in the statement.
80
    * 
81
    * @return \Mailcode\Mailcode_Variables_Variable[]
82
    */
83
    public function getVariables()
84
    {
85
        $result = array();
86
        
87
        foreach($this->tokens as $token)
88
        {
89
            if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable)
90
            {
91
                $result[] = $token->getVariable();
92
            }
93
        }
94
        
95
        return $result;
96
    }
97
    
98
    public function getVariableByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable
99
    {
100
        $token = $this->getTokenByIndex($index);
101
        
102
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable)
103
        {
104
            return $token;
105
        }
106
        
107
        return null;
108
    }
109
    
110
    public function getKeywordByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword
111
    {
112
        $token = $this->getTokenByIndex($index);
113
        
114
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword)
115
        {
116
            return $token;
117
        }
118
        
119
        return null;
120
    }
121
    
122
    public function getOperandByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Operand
123
    {
124
        $token = $this->getTokenByIndex($index);
125
        
126
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand)
127
        {
128
            return $token;
129
        }
130
        
131
        return null;
132
    }
133
    
134
    public function getTokenByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token
135
    {
136
        if(isset($this->tokens[$index]))
137
        {
138
            return $this->tokens[$index];
139
        }
140
        
141
        return null;
142
    }
143
    
144
    public function hasTokenAtIndex(int $index) : bool
145
    {
146
        return isset($this->tokens[$index]);
147
    }
148
}
149