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

Mailcode_Traits_Commands_IfContains   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 146
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchTerm() 0 12 2
A getVariable() 0 11 2
A getValidations() 0 5 1
A isCaseInsensitive() 0 3 1
A validateSyntax_literal_after_keyword() 0 15 2
A validateSyntax_variable() 0 15 2
A validateSyntax_literal() 0 14 2
A validateSyntax_keyword() 0 31 3
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_If_Contains} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_If_Contains
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: opening IF statement.
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_IfContains
25
{
26
    /**
27
     * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
28
     */
29
    protected $variable;
30
31
   /**
32
    * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
33
    */
34
    protected $literal;
35
    
36
   /**
37
    * @var boolean
38
    */
39
    protected $caseInsensitive = false;
40
    
41
    protected function getValidations() : array
42
    {
43
        return array(
44
            'variable',
45
            'literal',
46
        );
47
    }
48
    
49
    protected function validateSyntax_variable() : void
50
    {
51
        $info = $this->params->getInfo();
52
        
53
        $variable = $info->getVariableByIndex(0);
54
        
55
        if($variable)
56
        {
57
            $this->variable = $variable;
58
            return;
59
        }
60
        
61
        $this->validationResult->makeError(
62
            t('No variable specified in the command.'),
63
            Mailcode_Commands_IfBase::VALIDATION_VARIABLE_MISSING
64
        );
65
    }
66
    
67
    protected function validateSyntax_literal() : void
68
    {
69
        $info = $this->params->getInfo();
70
        
71
        // first variant: variable "Search term"
72
        $string = $info->getStringLiteralByIndex(1);
73
        
74
        if($string)
75
        {
76
            $this->literal = $string;
77
            return;
78
        }
79
        
80
        $this->validateSyntax_keyword();
81
    }
82
    
83
    protected function validateSyntax_keyword() : void
84
    {
85
        $info = $this->params->getInfo();
86
        
87
        // second variant: variable insensitive: "Search term"
88
        $keyword = $info->getKeywordByIndex(1);
89
        
90
        if(!$keyword)
91
        {
92
            $this->validationResult->makeError(
93
                t('Expected a search term or the %1$s keyword after the variable name.', 'insensitive:'),
94
                Mailcode_Commands_IfBase::VALIDATION_EXPECTED_KEYWORD
95
            );
96
            
97
            return;
98
        }
99
        
100
        if($keyword->getKeyword() !== 'insensitive:')
101
        {
102
            $this->validationResult->makeError(
103
                t('Invalid keyword %1$s.', $keyword->getKeyword()).' '.
104
                t('Only the keyword %1$s may be used here.', 'insensitive:'),
105
                Mailcode_Commands_IfBase::VALIDATION_INVALID_KEYWORD
106
            );
107
            
108
            return;
109
        }
110
        
111
        $this->caseInsensitive = true;
112
        
113
        $this->validateSyntax_literal_after_keyword();
114
    }
115
    
116
    protected function validateSyntax_literal_after_keyword() : void
117
    {
118
        $info = $this->params->getInfo();
119
        
120
        $string = $info->getStringLiteralByIndex(2);
121
        
122
        if($string)
123
        {
124
            $this->literal = $string;
125
            return;
126
        }
127
        
128
        $this->validationResult->makeError(
129
            t('Expected a search term after the %1$s keyword.', 'insensitive:'),
130
            Mailcode_Commands_IfBase::VALIDATION_CONTAINS_MISSING_SEARCH_TERM
131
        );
132
    }
133
134
   /**
135
    * Retrieves the variable being compared.
136
    *
137
    * @return Mailcode_Variables_Variable
138
    */
139
    public function getVariable() : Mailcode_Variables_Variable
140
    {
141
        if(isset($this->variable))
142
        {
143
            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

143
            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...
144
        }
145
        
146
        throw new Mailcode_Exception(
147
            'No variable available',
148
            null,
149
            Mailcode_Commands_IfBase::ERROR_NO_VARIABLE_AVAILABLE
150
        );
151
    }
152
    
153
    public function isCaseInsensitive() : bool
154
    {
155
        return $this->caseInsensitive;
156
    }
157
    
158
    public function getSearchTerm() : string
159
    {
160
        if(!isset($this->literal))
161
        {
162
            throw new Mailcode_Exception(
163
                'No string literal available',
164
                null,
165
                Mailcode_Commands_IfBase::ERROR_NO_STRING_LITERAL_AVAILABLE
166
            );
167
        }
168
        
169
        return $this->literal->getNormalized();
0 ignored issues
show
Bug introduced by
The method getNormalized() 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

169
        return $this->literal->/** @scrutinizer ignore-call */ getNormalized();

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...
170
    }
171
}
172