Passed
Push — master ( 19aa6f...a676bf )
by Sebastian
03:24
created

getListVariable()   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
declare(strict_types=1);
4
5
namespace Mailcode;
6
7
use AppUtils\OperationResult;
8
9
/**
10
 * Command validation drop-in: used by commands that accept
11
 * a single list variable property. Ensures that the specified
12
 * variable uses the scheme `$LIST.PROPERTY`, and not a single
13
 * path variable like `$FOO`.
14
 *
15
 * @package Mailcode
16
 * @subpackage Validation
17
 * @author Sebastian Mordziol <[email protected]>
18
 *
19
 * @property Mailcode_Parser_Statement_Validator $validator
20
 * @property OperationResult $validationResult
21
 *
22
 * @see Mailcode_Interfaces_Commands_ListPropertyVariable
23
 */
24
trait Mailcode_Traits_Commands_Validation_ListPropertyVariable
25
{
26
    /**
27
     * @var Mailcode_Variables_Variable|null
28
     */
29
    protected $listVariable = null;
30
31
    abstract public function getVariable() : Mailcode_Variables_Variable;
32
33
    protected function validateSyntax_list_property_variable() : void
34
    {
35
        $var = $this->getVariable();
36
37
        // Split the variable name by the dot
38
        $parts = explode('.', ltrim($var->getFullName(), '$'));
39
40
        if(count($parts) !== 2)
41
        {
42
            $this->validationResult->makeError(
43
                t('The variable %1$s is not a list property:', '<code>'.$var->getFullName().'</code>').' '.
44
                t('Expected a name with a dot, like %1$s', '<code>$'.t('LIST.PROPERTY').'</code>'),
45
                Mailcode_Interfaces_Commands_ListPropertyVariable::VALIDATION_NOT_A_LIST_PROPERTY
46
            );
47
48
            return;
49
        }
50
51
        // Create the list variable from the path of the property variable.
52
        $this->listVariable = new Mailcode_Variables_Variable(
53
            '',
54
            $parts[0],
55
            '$'.$parts[0],
56
            $var->getSourceCommand()
57
        );
58
    }
59
60
    /**
61
     * Retrieves the specified list variable, if any.
62
     * If the command is erroneous no list variable may
63
     * be present, in which case an exception is thrown.
64
     *
65
     * @return Mailcode_Variables_Variable
66
     * @throws Mailcode_Exception
67
     *
68
     * @see Mailcode_Interfaces_Commands_ListPropertyVariable::ERROR_NO_LIST_VARIABLE_PRESENT
69
     */
70
    public function getListVariable() : Mailcode_Variables_Variable
71
    {
72
        if(isset($this->listVariable))
73
        {
74
            return $this->listVariable;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->listVariable could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Variables_Variable. Consider adding an additional type-check to rule them out.
Loading history...
75
        }
76
77
        throw new Mailcode_Exception(
78
            'No list variable present.',
79
            '',
80
            Mailcode_Interfaces_Commands_ListPropertyVariable::ERROR_NO_LIST_VARIABLE_PRESENT
81
        );
82
    }
83
84
    /**
85
     * @return Mailcode_Variables_Variable
86
     * @throws Mailcode_Exception
87
     */
88
    public function getListProperty() : Mailcode_Variables_Variable
89
    {
90
        return $this->getVariable();
91
    }
92
}
93