Passed
Push — master ( c28222...9253d7 )
by Sebastian
02:41
created

getVariableToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Traits_Commands_Validation_Variable} trait.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see Mailcode_Traits_Commands_Validation_Variable
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Command validation drop-in: checks for the presence
16
 * of a variable name. Will accept the first variable 
17
 * it finds.
18
 *
19
 * @package Mailcode
20
 * @subpackage Validation
21
 * @author Sebastian Mordziol <[email protected]>
22
 *
23
 * @property Mailcode_Parser_Statement_Validator $validator
24
 */
25
trait Mailcode_Traits_Commands_Validation_Variable
26
{
27
   /**
28
    * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
29
    */
30
    protected $variableToken;
31
    
32
    protected function validateSyntax_variable() : void
33
    {
34
        $var = $this->validator->createVariable();
35
        
36
        if($var->isValid())
37
        {
38
            $this->variableToken = $var->getToken();
39
        }
40
        else
41
        {
42
            $this->validationResult->makeError(
43
                t('No variable has been specified.'),
44
                Mailcode_Commands_CommonConstants::VALIDATION_VARIABLE_MISSING
45
            );
46
        }
47
    }
48
49
    public function getVariableToken() : Mailcode_Parser_Statement_Tokenizer_Token_Variable
50
    {
51
        if(isset($this->variableToken))
52
        {
53
            return $this->variableToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->variableToken could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser...okenizer_Token_Variable. Consider adding an additional type-check to rule them out.
Loading history...
54
        }
55
56
        throw new Mailcode_Exception(
57
            'No variable token available',
58
            null,
59
            Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE
60
        );
61
    }
62
    
63
   /**
64
    * Retrieves the variable being compared.
65
    *
66
    * @return Mailcode_Variables_Variable
67
    */
68
    public function getVariable() : Mailcode_Variables_Variable
69
    {
70
        if(isset($this->variableToken))
71
        {
72
            return $this->variableToken->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

72
            return $this->variableToken->/** @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...
73
        }
74
        
75
        throw new Mailcode_Exception(
76
            'No variable available',
77
            null,
78
            Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE
79
        );
80
    }
81
    
82
    public function getVariableName() : string
83
    {
84
        return $this->getVariable()->getFullName();
85
    }
86
87
    /**
88
     * Checks whether the command is nested in a loop (FOR) command.
89
     *
90
     * @return bool
91
     */
92
    public function isInLoop() : bool
93
    {
94
        return $this->getLoopCommand() !== null;
95
    }
96
97
    /**
98
     * Retrieves the command's parent loop command, if any.
99
     *
100
     * @return Mailcode_Commands_Command_For|NULL
101
     */
102
    public function getLoopCommand() : ?Mailcode_Commands_Command_For
103
    {
104
        return $this->findLoopRecursive($this);
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Mailcode_Traits...nds_Validation_Variable is incompatible with the type Mailcode\Mailcode_Commands_Command expected by parameter $subject of Mailcode\Mailcode_Traits...le::findLoopRecursive(). ( Ignorable by Annotation )

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

104
        return $this->findLoopRecursive(/** @scrutinizer ignore-type */ $this);
Loading history...
105
    }
106
107
    /**
108
     * Recursively tries to find a loop command in the command's
109
     * parent commands. Goes up the whole ancestry if need be.
110
     *
111
     * @param Mailcode_Commands_Command $subject
112
     * @return Mailcode_Commands_Command_For|null
113
     */
114
    protected function findLoopRecursive(Mailcode_Commands_Command $subject) : ?Mailcode_Commands_Command_For
115
    {
116
        $parent = $subject->getParent();
117
118
        if($parent === null)
119
        {
120
            return null;
121
        }
122
123
        if($parent instanceof Mailcode_Commands_Command_For)
124
        {
125
            return $parent;
126
        }
127
128
        return $this->findLoopRecursive($parent);
129
    }
130
}
131