Passed
Pull Request — master (#17)
by
unknown
03:33
created

hasVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
 * @see Mailcode_Interfaces_Commands_Validation_Variable
26
 */
27
trait Mailcode_Traits_Commands_Validation_Variable
28
{
29
    /**
30
     * @var Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL
31
     */
32
    protected $variableToken;
33
34
    protected function validateSyntax_variable(): void
35
    {
36
        $var = $this->validator->createVariable();
37
38
        if ($var->isValid()) {
39
            $this->variableToken = $var->getToken();
40
        } else {
41
            $this->validationResult->makeError(
42
                t('No variable has been specified.'),
43
                Mailcode_Commands_CommonConstants::VALIDATION_VARIABLE_MISSING
44
            );
45
        }
46
    }
47
48
    protected function validateSyntax_variable_optional(): void
49
    {
50
        $var = $this->validator->createVariable();
51
52
        if ($var->isValid()) {
53
            $this->variableToken = $var->getToken();
54
        }
55
    }
56
57
    public function getVariableToken(): Mailcode_Parser_Statement_Tokenizer_Token_Variable
58
    {
59
        if (isset($this->variableToken)) {
60
            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...
61
        }
62
63
        throw new Mailcode_Exception(
64
            'No variable token available',
65
            null,
66
            Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE
67
        );
68
    }
69
70
    /**
71
     * Retrieves the variable being compared.
72
     *
73
     * @return Mailcode_Variables_Variable
74
     * @throws Mailcode_Exception
75
     */
76
    public function getVariable(): Mailcode_Variables_Variable
77
    {
78
        if (isset($this->variableToken)) {
79
            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

79
            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...
80
        }
81
82
        throw new Mailcode_Exception(
83
            'No variable available',
84
            null,
85
            Mailcode_Commands_CommonConstants::ERROR_NO_VARIABLE_AVAILABLE
86
        );
87
    }
88
89
    public function getVariableName(): string
90
    {
91
        return $this->getVariable()->getFullName();
92
    }
93
94
    /**
95
     * Checks whether the command is nested in a loop (FOR) command.
96
     *
97
     * @return bool
98
     */
99
    public function isInLoop(): bool
100
    {
101
        return $this->getLoopCommand() !== null;
102
    }
103
104
    /**
105
     * Retrieves the command's parent loop command, if any.
106
     *
107
     * @return Mailcode_Commands_Command_For|NULL
108
     */
109
    public function getLoopCommand(): ?Mailcode_Commands_Command_For
110
    {
111
        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

111
        return $this->findLoopRecursive(/** @scrutinizer ignore-type */ $this);
Loading history...
112
    }
113
114
    /**
115
     * Recursively tries to find a loop command in the command's
116
     * parent commands. Goes up the whole ancestry if need be.
117
     *
118
     * @param Mailcode_Commands_Command $subject
119
     * @return Mailcode_Commands_Command_For|null
120
     */
121
    protected function findLoopRecursive(Mailcode_Commands_Command $subject): ?Mailcode_Commands_Command_For
122
    {
123
        $parent = $subject->getParent();
124
125
        if ($parent === null) {
126
            return null;
127
        }
128
129
        if ($parent instanceof Mailcode_Commands_Command_For) {
130
            return $parent;
131
        }
132
133
        return $this->findLoopRecursive($parent);
134
    }
135
136
    public function hasVariable(): bool
137
    {
138
        return isset($this->variableToken);
139
    }
140
}
141