Passed
Push — master ( ad7522...d3eb9e )
by Sebastian
05:45
created

Mailcode_Commands_Command_ShowNumber::isAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Command_ShowNumber} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Command_ShowNumber
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Mailcode command: show a date variable value.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Command_ShowNumber extends Mailcode_Commands_ShowBase
22
{
23
    public const VALIDATION_NOT_A_FORMAT_STRING = 72201;
24
    public const VALIDATION_PADDING_SEPARATOR_OVERFLOW = 72202;
25
    public const VALIDATION_INVALID_FORMAT_NUMBER = 72203;
26
    public const VALIDATION_INVALID_THOUSANDS_SEPARATOR = 72204;
27
    public const VALIDATION_INVALID_DECIMALS_NO_DECIMALS = 72205;
28
    public const VALIDATION_INVALID_CHARACTERS = 72206;
29
    public const VALIDATION_PADDING_INVALID_CHARS = 72207;
30
    public const VALIDATION_INVALID_DECIMALS_CHARS = 72208;
31
    public const VALIDATION_INVALID_DECIMAL_SEPARATOR = 72209;
32
    public const VALIDATION_SEPARATORS_SAME_CHARACTER = 72210;
33
    
34
   /**
35
    * The default number format string.
36
    * @var string
37
    */
38
    private $formatString = Mailcode_Number_Info::DEFAULT_FORMAT;
39
40
    /**
41
     * @var Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL
42
     */
43
    private $absoluteKeyword;
44
45
    public function getName() : string
46
    {
47
        return 'shownumber';
48
    }
49
50
    public function getLabel() : string
51
    {
52
        return t('Show number variable');
53
    }
54
55
    protected function getValidations() : array
56
    {
57
        return array(
58
            Mailcode_Interfaces_Commands_Validation_Variable::VALIDATION_NAME_VARIABLE,
59
            'check_format',
60
            Mailcode_Interfaces_Commands_Validation_URLEncode::VALIDATION_NAME_URLENCODE,
61
            Mailcode_Interfaces_Commands_Validation_URLDecode::VALIDATION_NAME_URLDECODE,
62
            'absolute'
63
        );
64
    }
65
66
    protected function validateSyntax_check_format() : void
67
    {
68
         $tokens = $this->params->getInfo()->getStringLiterals();
69
         
70
         // no format specified? Use the default one.
71
         if(empty($tokens))
72
         {
73
             return;
74
         }
75
76
         $token = array_pop($tokens);
77
         $this->parseFormatString($token->getText());
78
    }
79
80
    protected function validateSyntax_absolute() : void
81
    {
82
        $keywords = $this->params->getInfo()->getKeywords();
83
84
        foreach($keywords as $keyword)
85
        {
86
            if($keyword->getKeyword() === Mailcode_Commands_Keywords::TYPE_ABSOLUTE)
87
            {
88
                $this->absoluteKeyword = $keyword;
89
                break;
90
            }
91
        }
92
    }
93
94
    private function parseFormatString(string $format) : void
95
    {
96
        $result = new Mailcode_Number_Info($format);
97
98
        if($result->isValid())
99
        {
100
            $this->formatString = $format;
101
            return;
102
        }
103
104
        $this->validationResult->makeError(
105
            $result->getErrorMessage(),
106
            $result->getCode()
107
        );
108
    }
109
    
110
   /**
111
    * Retrieves the format string used to format the number.
112
    * 
113
    * @return string
114
    */
115
    public function getFormatString() : string
116
    {
117
        return $this->formatString;
118
    }
119
120
    public function isAbsolute() : bool
121
    {
122
        return isset($this->absoluteKeyword);
123
    }
124
125
    public function setAbsolute(bool $absolute) : Mailcode_Commands_Command_ShowNumber
126
    {
127
        if($absolute === false && isset($this->absoluteKeyword))
128
        {
129
            $this->params->getInfo()->removeKeyword($this->absoluteKeyword->getKeyword());
0 ignored issues
show
Bug introduced by
The method getKeyword() 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

129
            $this->params->getInfo()->removeKeyword($this->absoluteKeyword->/** @scrutinizer ignore-call */ getKeyword());

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...
130
            $this->absoluteKeyword = null;
131
        }
132
133
        if($absolute === true && !isset($this->absoluteKeyword))
134
        {
135
             $this->params
136
                 ->getInfo()
137
                 ->addKeyword(Mailcode_Commands_Keywords::TYPE_ABSOLUTE);
138
139
             $this->validateSyntax_absolute();
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * Retrieves information on how the number should be formatted.
147
     *
148
     * @return Mailcode_Number_Info
149
     */
150
    public function getFormatInfo() : Mailcode_Number_Info
151
    {
152
        return new Mailcode_Number_Info($this->getFormatString());
153
    }
154
}
155
156