Passed
Push — master ( 430c98...5aa9a6 )
by Sebastian
02:51
created

Mailcode_Commands_Command_For   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 138
rs 10
wmc 20

13 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsType() 0 3 1
A getValidations() 0 6 1
A generatesContent() 0 3 1
A validateSyntax_variable_names() 0 10 2
A validateSyntax_keyword() 0 10 2
A validateSyntax_statement() 0 16 4
A getLabel() 0 3 1
A getSourceVariable() 0 11 2
A supportsLogicKeywords() 0 3 1
A requiresParameters() 0 3 1
A getDefaultType() 0 3 1
A getName() 0 3 1
A getLoopVariable() 0 11 2
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 getDefaultType() : string
61
    {
62
        return '';
63
    }
64
    
65
    public function requiresParameters(): bool
66
    {
67
        return true;
68
    }
69
    
70
    public function supportsLogicKeywords() : bool
71
    {
72
        return false;
73
    }
74
    
75
    protected function getValidations() : array
76
    {
77
        return array(
78
            'statement',
79
            'keyword',
80
            'variable_names'
81
        );
82
    }
83
    
84
    public function generatesContent() : bool
85
    {
86
        return false;
87
    }
88
    
89
    public function getSourceVariable() : Mailcode_Variables_Variable
90
    {
91
        if(isset($this->sourceVar))
92
        {
93
            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

93
            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...
94
        }
95
        
96
        throw new Mailcode_Exception(
97
            'No source variable available',
98
            null,
99
            self::ERROR_SOURCE_VARIABLE_NOT_AVAILABLE
100
        );
101
    }
102
    
103
    public function getLoopVariable() : Mailcode_Variables_Variable
104
    {
105
        if(isset($this->loopVar))
106
        {
107
            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

107
            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...
108
        }
109
        
110
        throw new Mailcode_Exception(
111
            'No loop variable available',
112
            null,
113
            self::ERROR_LOOP_VARIABLE_NOT_AVAILABLE
114
        );
115
    }
116
    
117
    protected function validateSyntax_statement() : void
118
    {
119
        $info = $this->params->getInfo();
120
        
121
        $this->loopVar = $info->getVariableByIndex(0);
122
        $this->keyword = $info->getKeywordByIndex(1);
123
        $this->sourceVar = $info->getVariableByIndex(2);
124
        
125
        if(!$this->loopVar || !$this->keyword || !$this->sourceVar)
126
        {
127
            $this->validationResult->makeError(
128
                t('Not a valid for loop.').' '.t('Is the %1$s keyword missing?', 'in:'),
129
                self::VALIDATION_INVALID_FOR_STATEMENT
130
            );
131
            
132
            return;
133
        }
134
    }
135
    
136
    protected function validateSyntax_keyword() : void
137
    {
138
        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

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