Passed
Push — master ( 1b5df6...213867 )
by Sebastian
13:22
created

Mailcode_Traits_Commands_IfVariable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 142
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A getValidations() 0 6 1
A getVariable() 0 11 2
A validateSyntax_operand() 0 27 3
A getComparator() 0 12 2
A validateSyntax_variable() 0 15 2
A validateSyntax_value() 0 26 3
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_If_Variable} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_If_Variable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * IF for variable comparisons.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 * 
21
 * @property Mailcode_Parser_Statement $params
22
 * @property \AppUtils\OperationResult $validationResult
23
 */
24
trait Mailcode_Traits_Commands_IfVariable
25
{
26
   /**
27
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
28
    */
29
    protected $variable;
30
    
31
   /**
32
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL
33
    */
34
    protected $comparator;
35
    
36
   /**
37
    * @var Mailcode_Parser_Statement_Tokenizer_Type_Value|NULL
38
    */
39
    protected $value;
40
    
41
    protected function getValidations() : array
42
    {
43
        return array(
44
            'variable',
45
            'operand',
46
            'value'
47
        );
48
    }
49
    
50
    protected function validateSyntax_variable() : void
51
    {
52
        $info = $this->params->getInfo();
53
        
54
        $var = $info->getVariableByIndex(0);
55
        
56
        if($var)
57
        {
58
            $this->variable = $var;
59
            return;
60
        }
61
        
62
        $this->validationResult->makeError(
63
            t('No variable specified in the command.'),
64
            Mailcode_Commands_IfBase::VALIDATION_VARIABLE_MISSING
65
        );
66
    }
67
    
68
    protected function validateSyntax_operand() : void
69
    {
70
        $info = $this->params->getInfo();
71
        
72
        $operand = $info->getOperandByIndex(1);
73
        
74
        if(!$operand)
75
        {
76
            $this->validationResult->makeError(
77
                t('No operand sign after the variable name.'),
78
                Mailcode_Commands_IfBase::VALIDATION_OPERAND_MISSING
79
            );
80
            
81
            return;
82
        }
83
        
84
        if(!$operand->isComparator())
85
        {
86
            $this->validationResult->makeError(
87
                t('The operand sign is not a comparison operand.'),
88
                Mailcode_Commands_IfBase::VALIDATION_OPERAND_NOT_COMPARISON
89
            );
90
            
91
            return;
92
        }
93
        
94
        $this->comparator = $operand;
95
    }
96
    
97
    protected function validateSyntax_value() : void
98
    {
99
        $info = $this->params->getInfo();
100
        
101
        $token = $info->getTokenByIndex(2);
102
        
103
        if(!$token)
104
        {
105
            $this->validationResult->makeError(
106
                t('Nothing found after the comparison operand.'),
107
                Mailcode_Commands_IfBase::VALIDATION_NOTHING_AFTER_OPERAND
108
            );
109
            
110
            return;
111
        }
112
        
113
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Type_Value)
114
        {
115
            $this->value = $token;
116
            
117
            return;
118
        }
119
        
120
        $this->validationResult->makeError(
121
            t('Not a valid value to compare to.'),
122
            Mailcode_Commands_IfBase::VALIDATION_INVALID_COMPARISON_TOKEN
123
        );
124
    }
125
    
126
   /**
127
    * Retrieves the variable being compared.
128
    *
129
    * @return Mailcode_Variables_Variable
130
    */
131
    public function getVariable() : Mailcode_Variables_Variable
132
    {
133
        if(isset($this->variable))
134
        {
135
            return $this->variable->getVariable();
0 ignored issues
show
Bug introduced by
The method getVariable() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            return $this->variable->/** @scrutinizer ignore-call */ getVariable();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        }
137
        
138
        throw new Mailcode_Exception(
139
            'No variable available',
140
            null,
141
            Mailcode_Commands_IfBase::ERROR_NO_VARIABLE_AVAILABLE
142
        );
143
    }
144
    
145
    public function getComparator() : string
146
    {
147
        if(!isset($this->comparator))
148
        {
149
            throw new Mailcode_Exception(
150
                'No comparator available',
151
                null,
152
                Mailcode_Commands_IfBase::ERROR_NO_COMPARATOR_AVAILABLE
153
            );
154
        }
155
        
156
        return $this->comparator->getOperand();
0 ignored issues
show
Bug introduced by
The method getOperand() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
        return $this->comparator->/** @scrutinizer ignore-call */ getOperand();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
    }
158
    
159
   /**
160
    * Retrieves the unquoted value 
161
    * @return string
162
    */
163
    public function getValue() : string
164
    {
165
        return $this->value->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
        return $this->value->/** @scrutinizer ignore-call */ getValue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
166
    }
167
}
168