Test Failed
Push — master ( 36c5b6...af11e6 )
by Sebastian
04:51
created

validateSyntax_language()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 22
rs 9.9
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Mailcode_Commands_Command_Code} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Mailcode_Commands_Command_Code
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: opening CODE statement.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_Code
22
    extends Mailcode_Commands_Command
23
    implements
24
        Mailcode_Commands_Command_Type_Standalone,
25
        Mailcode_Interfaces_Commands_ProtectedContent
26
{
27
    use Mailcode_Traits_Commands_ProtectedContent;
28
29
    public const ERROR_LANG_TOKEN_MISSING = 73101;
30
31
    public const VALIDATION_LANGUAGE_NOT_SPECIFIED = 72901;
32
    public const VALIDATION_UNKNOWN_LANGUAGE = 72902;
33
34
    /**
35
     * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
36
     */
37
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $langToken = null;
38
39
    public function getName() : string
40
    {
41
        return 'code';
42
    }
43
    
44
    public function getLabel() : string
45
    {
46
        return t('Raw backend code');
47
    }
48
    
49
    public function supportsType(): bool
50
    {
51
        return false;
52
    }
53
54
    public function supportsURLEncoding() : bool
55
    {
56
        return false;
57
    }
58
59
    public function getDefaultType() : string
60
    {
61
        return '';
62
    }
63
    
64
    public function requiresParameters(): bool
65
    {
66
        return true;
67
    }
68
    
69
    public function supportsLogicKeywords() : bool
70
    {
71
        return false;
72
    }
73
    
74
    protected function getValidations() : array
75
    {
76
        return array(
77
            Mailcode_Interfaces_Commands_ProtectedContent::VALIDATION_NAME_CONTENT_ID,
78
            'token',
79
            'language'
80
        );
81
    }
82
    
83
    public function generatesContent() : bool
84
    {
85
        return true;
86
    }
87
88
    // region: Supported syntaxes
89
90
    public const SYNTAX_APACHE_VELOCITY = 'ApacheVelocity';
91
    public const SYNTAX_MAILCODE = 'Mailcode';
92
93
    /**
94
     * @return string[]
95
     */
96
    public static function getSupportedSyntaxes() : array
97
    {
98
        return array(
99
            self::SYNTAX_APACHE_VELOCITY,
100
            self::SYNTAX_MAILCODE
101
        );
102
    }
103
104
    /**
105
     * Retrieves the name of the syntax that the code is written in.
106
     *
107
     * @return string
108
     * @throws Mailcode_Exception If the command has no language parameter.
109
     *
110
     * @see Mailcode_Commands_Command_Code::ERROR_LANG_TOKEN_MISSING
111
     */
112
    public function getSyntaxName() : string
113
    {
114
        if(isset($this->langToken))
115
        {
116
            return $this->langToken->getText();
0 ignored issues
show
Bug introduced by
The method getText() 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

116
            return $this->langToken->/** @scrutinizer ignore-call */ getText();

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...
117
        }
118
119
        throw new Mailcode_Exception(
120
            'No language name available',
121
            'The command has no lang token.',
122
            self::ERROR_LANG_TOKEN_MISSING
123
        );
124
    }
125
126
    // endregion
127
128
    /**
129
     * Ensures that the language token is present.
130
     */
131
    protected function validateSyntax_token() : void
132
    {
133
        $lang = $this->requireParams()
134
            ->getInfo()
135
            ->getStringLiteralByIndex(1);
136
137
        if($lang)
138
        {
139
            $this->langToken = $lang;
140
            return;
141
        }
142
143
        $translator = new Mailcode_Translator();
144
145
        $this->validationResult->makeError(
146
            t('The target language has to be specified in the first parameter of the command.').' '.
147
            t('Possible values are:').' '.
148
            '<code>'.implode('</code>, <code>', $translator->getSyntaxNames()).'</code>',
149
            self::VALIDATION_LANGUAGE_NOT_SPECIFIED
150
        );
151
    }
152
153
    /**
154
     * Ensures that the syntax specified in the language token
155
     * is a valid translator syntax name.
156
     */
157
    protected function validateSyntax_language() : void
158
    {
159
        // To keep PHPStan happy. If no token has been found, this
160
        // method will not be called.
161
        if(!isset($this->langToken))
162
        {
163
            return;
164
        }
165
166
        $name = $this->langToken->getText();
167
        $supported = self::getSupportedSyntaxes();
168
169
        if(in_array($name, $supported))
170
        {
171
            return;
172
        }
173
174
        $this->validationResult->makeError(
175
            t('The language %1$s does not exist.', '<code>'.$name.'</code>').' '.
176
            t('Possible values are:').' '.
177
            '<code>'.implode('</code>, <code>', $supported).'</code>',
178
            self::VALIDATION_UNKNOWN_LANGUAGE
179
        );
180
    }
181
}
182