Passed
Push — master ( 75ee28...758f58 )
by Sebastian
04:00
created

supportsURLEncoding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_For} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_For
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: opening FOR statement.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_For extends Mailcode_Commands_Command implements Mailcode_Commands_Command_Type_Opening
22
{
23
    const ERROR_SOURCE_VARIABLE_NOT_AVAILABLE = 64101;
24
    const ERROR_LOOP_VARIABLE_NOT_AVAILABLE = 64102;
25
    
26
    const VALIDATION_INVALID_FOR_STATEMENT = 49701;
27
    const VALIDATION_WRONG_KEYWORD = 49702;
28
    const VALIDATION_VARIABLE_NAME_IS_THE_SAME = 49703;
29
    
30
   /**
31
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
32
    */
33
    private $loopVar;
34
    
35
   /**
36
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
37
    */
38
    private $sourceVar;
39
    
40
   /**
41
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL
42
    */
43
    private $keyword;
44
    
45
    public function getName() : string
46
    {
47
        return 'for';
48
    }
49
    
50
    public function getLabel() : string
51
    {
52
        return t('FOR loop');
53
    }
54
    
55
    public function supportsType(): bool
56
    {
57
        return false;
58
    }
59
60
    public function supportsURLEncoding() : bool
61
    {
62
        return false;
63
    }
64
65
    public function getDefaultType() : string
66
    {
67
        return '';
68
    }
69
    
70
    public function requiresParameters(): bool
71
    {
72
        return true;
73
    }
74
    
75
    public function supportsLogicKeywords() : bool
76
    {
77
        return false;
78
    }
79
    
80
    protected function getValidations() : array
81
    {
82
        return array(
83
            'statement',
84
            'keyword',
85
            'variable_names'
86
        );
87
    }
88
    
89
    public function generatesContent() : bool
90
    {
91
        return false;
92
    }
93
    
94
    public function getSourceVariable() : Mailcode_Variables_Variable
95
    {
96
        if(isset($this->sourceVar))
97
        {
98
            return $this->sourceVar->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

98
            return $this->sourceVar->/** @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...
99
        }
100
        
101
        throw new Mailcode_Exception(
102
            'No source variable available',
103
            null,
104
            self::ERROR_SOURCE_VARIABLE_NOT_AVAILABLE
105
        );
106
    }
107
    
108
    public function getLoopVariable() : Mailcode_Variables_Variable
109
    {
110
        if(isset($this->loopVar))
111
        {
112
            return $this->loopVar->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

112
            return $this->loopVar->/** @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...
113
        }
114
        
115
        throw new Mailcode_Exception(
116
            'No loop variable available',
117
            null,
118
            self::ERROR_LOOP_VARIABLE_NOT_AVAILABLE
119
        );
120
    }
121
    
122
    protected function validateSyntax_statement() : void
123
    {
124
        $info = $this->params->getInfo();
125
        
126
        $this->loopVar = $info->getVariableByIndex(0);
127
        $this->keyword = $info->getKeywordByIndex(1);
128
        $this->sourceVar = $info->getVariableByIndex(2);
129
        
130
        if(!$this->loopVar || !$this->keyword || !$this->sourceVar)
131
        {
132
            $this->validationResult->makeError(
133
                t('Not a valid for loop.').' '.t('Is the %1$s keyword missing?', 'in:'),
134
                self::VALIDATION_INVALID_FOR_STATEMENT
135
            );
136
            
137
            return;
138
        }
139
    }
140
    
141
    protected function validateSyntax_keyword() : void
142
    {
143
        if($this->keyword->isForIn())
0 ignored issues
show
Bug introduced by
The method isForIn() 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
        if($this->keyword->/** @scrutinizer ignore-call */ isForIn())

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
            return;
146
        }
147
         
148
        $this->validationResult->makeError(
149
            t('The %1$s keyword cannot be used in this command.', $this->keyword->getKeyword()),
150
            self::VALIDATION_WRONG_KEYWORD
151
        );
152
    }
153
    
154
    protected function validateSyntax_variable_names() : void
155
    {
156
        if($this->sourceVar->getVariable()->getFullName() !== $this->loopVar->getVariable()->getFullName())
157
        {
158
            return;
159
        }
160
        
161
        $this->validationResult->makeError(
162
            t('The source and loop variables have the same name.'),
163
            self::VALIDATION_VARIABLE_NAME_IS_THE_SAME
164
        );
165
    }
166
}
167