Passed
Push — master ( 742ef2...7822c6 )
by Sebastian
04:11
created

getTextToken()   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
nc 2
nop 0
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Mailcode_Commands_Command_ShowEncoded}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Mailcode_Commands_Command_ShowEncoded
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Commands\ParamsException;
15
use Mailcode\Interfaces\Commands\EncodableInterface;
16
use Mailcode\Interfaces\Commands\Validation\IDNEncodeInterface;
17
use Mailcode\Interfaces\Commands\Validation\IDNEncodingInterface;
18
use Mailcode\Traits\Commands\EncodableTrait;
19
use Mailcode\Traits\Commands\Validation\IDNDecodeTrait;
20
use Mailcode\Traits\Commands\Validation\IDNEncodeTrait;
21
22
/**
23
 * Command used to encode bits of text to any number
24
 * of supported encodings, in the order that the encoding
25
 * keywords are added to the command.
26
 *
27
 * @package Mailcode
28
 * @subpackage Commands
29
 * @author Sebastian Mordziol <[email protected]>
30
 */
31
class Mailcode_Commands_Command_ShowEncoded
32
    extends Mailcode_Commands_ShowBase
33
    implements
34
    IDNEncodingInterface
35
{
36
    use IDNEncodeTrait;
37
    use IDNDecodeTrait;
38
39
    public const VALIDATION_MISSING_SUBJECT_STRING = 102201;
40
41
    public const ERROR_NO_TEXT_TOKEN_AVAILABLE = 102301;
42
43
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $textToken = null;
44
45
    public function getName() : string
46
    {
47
        return 'showencoded';
48
    }
49
50
    public function getLabel() : string
51
    {
52
        return t('Encode text using a variety of formats.');
53
    }
54
55
    protected function getValidations() : array
56
    {
57
        return array(
58
            'subject_text',
59
            'require_encoding'
60
        );
61
    }
62
63
    /**
64
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
65
     * @throws ParamsException
66
     */
67
    public function getTextToken() : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
68
    {
69
        if(isset($this->textToken))
70
        {
71
            return $this->textToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->textToken could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser...zer_Token_StringLiteral. Consider adding an additional type-check to rule them out.
Loading history...
72
        }
73
74
        throw new ParamsException(
75
            'No text token available in the command.',
76
            '',
77
            self::ERROR_NO_TEXT_TOKEN_AVAILABLE
78
        );
79
    }
80
81
    public function getText() : string
82
    {
83
        return $this->getTextToken()->getText();
84
    }
85
86
    public function setText(string $text) : self
87
    {
88
        if(isset($this->textToken))
89
        {
90
            $this->textToken->setText($text);
0 ignored issues
show
Bug introduced by
The method setText() 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

90
            $this->textToken->/** @scrutinizer ignore-call */ 
91
                              setText($text);

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...
91
            return $this;
92
        }
93
94
        $this->textToken = $this->requireParams()
95
            ->getInfo()
96
            ->addStringLiteral($text);
97
98
        return $this;
99
    }
100
101
    protected function validateSyntax_subject_text() : void
102
    {
103
        $strings = $this->requireParams()
104
            ->getInfo()
105
            ->getStringLiterals();
106
107
        if(empty($strings))
108
        {
109
            $this->getValidationResult()->makeError(
110
                t('No text to encode has been specified.'),
111
                self::VALIDATION_MISSING_SUBJECT_STRING
112
            );
113
            return;
114
        }
115
116
        $this->textToken = $strings[0];
117
    }
118
119
    protected function validateSyntax_require_encoding() : void
120
    {
121
        $keywords = $this->requireParams()
122
            ->getInfo()
123
            ->getKeywords();
124
125
        if(!empty($keywords))
126
        {
127
            return;
128
        }
129
130
        $this->getValidationResult()->makeError(
131
            t('No encodings have been specified.'),
132
            Mailcode_Commands_CommonConstants::VALIDATION_NO_ENCODINGS_SPECIFIED
133
        );
134
    }
135
}
136