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

Mailcode_Commands_Command_Code::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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_Opening,
25
        Mailcode_Interfaces_Commands_ProtectedContent
26
{
27
    use Mailcode_Traits_Commands_ProtectedContent;
28
29
    const ERROR_LANG_TOKEN_MISSING = 73101;
30
31
    const VALIDATION_LANGUAGE_NOT_SPECIFIED = 72901;
32
    const VALIDATION_UNKNOWN_LANGUAGE = 72902;
33
34
    /**
35
     * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
36
     */
37
    private $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
            'token',
78
            'language'
79
        );
80
    }
81
    
82
    public function generatesContent() : bool
83
    {
84
        return true;
85
    }
86
87
    /**
88
     * Ensures that the language token is present.
89
     */
90
    protected function validateSyntax_token() : void
91
    {
92
        $lang = $this->params->getInfo()->getStringLiteralByIndex(0);
93
94
        if($lang)
95
        {
96
            $this->langToken = $lang;
97
            return;
98
        }
99
100
        $translator = new Mailcode_Translator();
101
102
        $this->validationResult->makeError(
103
            t('The target language has to be specified in the first parameter of the command.').' '.
104
            t('Possible values are:').' '.
105
            '<code>'.implode('</code>, <code>', $translator->getSyntaxNames()).'</code>',
106
            self::VALIDATION_LANGUAGE_NOT_SPECIFIED
107
        );
108
    }
109
110
    /**
111
     * Ensures that the syntax specified in the language token
112
     * is a valid translator syntax name.
113
     */
114
    protected function validateSyntax_language() : void
115
    {
116
        // To keep PHPStan happy. If no token has been found, this
117
        // method will not be called.
118
        if(!isset($this->langToken))
119
        {
120
            return;
121
        }
122
123
        $name = $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

123
        /** @scrutinizer ignore-call */ 
124
        $name = $this->langToken->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...
124
        $translator = new Mailcode_Translator();
125
126
        if($translator->syntaxExists($name))
127
        {
128
            return;
129
        }
130
131
        $this->validationResult->makeError(
132
            t('The language %1$s does not exist.', '<code>'.$name.'</code>').' '.
133
            t('Possible values are:').' '.
134
            '<code>'.implode('</code>, <code>', $translator->getSyntaxNames()).'</code>',
135
            self::VALIDATION_UNKNOWN_LANGUAGE
136
        );
137
    }
138
139
    /**
140
     * Retrieves the name of the syntax that the code is written in.
141
     *
142
     * @return string
143
     * @throws Mailcode_Exception If the command has no language parameter.
144
     *
145
     * @see Mailcode_Commands_Command_Code::ERROR_LANG_TOKEN_MISSING
146
     */
147
    public function getSyntaxName() : string
148
    {
149
        if(isset($this->langToken))
150
        {
151
            return $this->langToken->getText();
152
        }
153
154
        throw new Mailcode_Exception(
155
            'No language name available',
156
            'The command has no lang token.',
157
            self::ERROR_LANG_TOKEN_MISSING
158
        );
159
    }
160
161
    /**
162
     * Retrieves an instance of the translation syntax used for
163
     * the code contained in the command.
164
     *
165
     * @return Mailcode_Translator_Syntax
166
     * @throws Mailcode_Exception
167
     */
168
    public function getSyntax() : Mailcode_Translator_Syntax
169
    {
170
        $translator = new Mailcode_Translator();
171
        return $translator->createSyntax($this->getSyntaxName());
172
    }
173
}
174