Passed
Push — master ( 9253d7...e42fcd )
by Sebastian
03:09
created

isComparison()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Mailcode;
6
7
class Mailcode_Parser_Statement_Info_Variables
8
{
9
    /**
10
     * @var Mailcode_Parser_Statement_Info
11
     */
12
    private $info;
13
14
    /**
15
     * @var Mailcode_Parser_Statement_Tokenizer
16
     */
17
    private $tokenizer;
18
19
    public function __construct(Mailcode_Parser_Statement_Info $info, Mailcode_Parser_Statement_Tokenizer $tokenizer)
20
    {
21
        $this->info = $info;
22
        $this->tokenizer = $tokenizer;
23
    }
24
25
    /**
26
     * Whether the whole statement is a variable being assigned a value.
27
     *
28
     * @return bool
29
     */
30
    public function isAssignment() : bool
31
    {
32
        $variable = $this->getByIndex(0);
33
        $operand = $this->info->getOperandByIndex(1);
34
        $value = $this->info->getTokenByIndex(2);
35
36
        if($variable && $operand && $value && $operand->isAssignment())
37
        {
38
            return true;
39
        }
40
41
        return false;
42
    }
43
44
    /**
45
     * Whether the whole statement is a variable being compared to something.
46
     *
47
     * @return bool
48
     */
49
    public function isComparison() : bool
50
    {
51
        $variable = $this->getByIndex(0);
52
        $operand = $this->info->getOperandByIndex(1);
53
        $value = $this->info->getTokenByIndex(2);
54
55
        if($variable && $operand && $value && $operand->isComparator())
56
        {
57
            return true;
58
        }
59
60
        return false;
61
    }
62
63
    /**
64
     * Retrieves all variables used in the statement.
65
     *
66
     * @return Mailcode_Variables_Variable[]
67
     * @throws Mailcode_Exception
68
     */
69
    public function getAll() : array
70
    {
71
        $result = array();
72
        $tokens = $this->tokenizer->getTokens();
73
74
        foreach($tokens as $token)
75
        {
76
            if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable)
77
            {
78
                $result[] = $token->getVariable();
79
            }
80
        }
81
82
        return $result;
83
    }
84
85
    /**
86
     * Retrieves a variable by its position in the command's parameters.
87
     * Returns null if there is no parameter at the specified index, or
88
     * if it is of another type.
89
     *
90
     * @param int $index Zero-based index.
91
     * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
92
     */
93
    public function getByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable
94
    {
95
        $token = $this->info->getTokenByIndex($index);
96
97
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable)
98
        {
99
            return $token;
100
        }
101
102
        return null;
103
    }
104
}
105