Test Failed
Push — master ( 9c7d00...09444e )
by
unknown
08:57
created

TimezoneTrait::createTimeZoneToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.8666
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Traits\Commands\Validation\TimezoneTrait}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\TimezoneTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Interfaces\Commands\Validation\TimezoneInterface;
15
use Mailcode\Mailcode_Commands_Keywords;
16
use Mailcode\Mailcode;
17
use Mailcode\Mailcode_Commands_Command_ShowDate;
18
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token;
19
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
20
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable;
21
use Mailcode\Mailcode_Variables_Variable;
22
use function Mailcode\t;
23
24
/**
25
 * @package Mailcode
26
 * @subpackage Validation
27
 * @author Olaf Böcker <[email protected]>
28
 *
29
 * @see TimezoneInterface
30
 */
31
trait TimezoneTrait
32
{
33
    private bool $timezoneEnabled = false;
34
35
    private ?Mailcode_Parser_Statement_Tokenizer_Token $timezoneToken = null;
36
37
    protected function validateSyntax_check_timezone(): void
38
    {
39
        $this->timezoneToken = $this->requireParams()
0 ignored issues
show
Bug introduced by
It seems like requireParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $this->timezoneToken = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
40
            ->getInfo()
41
            ->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_TIMEZONE);
42
43
        $val = $this->validator->createKeyword(Mailcode_Commands_Keywords::TYPE_TIMEZONE);
44
45
        if ($this->timezoneToken === null || !$val->isValid()) {
46
            return;
47
        }
48
49
        if (!$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral &&
50
            !$this->timezoneToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
51
            $this->validationResult->makeError(
52
                t('Invalid timezone:') . ' ' . t('Expected a string or variable.'),
53
                TimezoneInterface::VALIDATION_TIMEZONE_CODE_WRONG_TYPE
54
            );
55
        }
56
    }
57
58
    /**
59
     * Gets the time zone to use for the command. If none has
60
     * been specified in the original command, the default
61
     * time zone is used as defined via {@see Mailcode_Commands_Command_ShowDate::setDefaultTimezone()}.
62
     *
63
     * @return Mailcode_Parser_Statement_Tokenizer_Token
64
     */
65
    public function getTimezoneToken(): Mailcode_Parser_Statement_Tokenizer_Token
66
    {
67
        if(!isset($this->timezoneToken)) {
68
            $this->timezoneToken = $this->createTimeZoneToken();
69
        }
70
71
        return $this->timezoneToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->timezoneToken could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser_Statement_Tokenizer_Token. Consider adding an additional type-check to rule them out.
Loading history...
72
    }
73
74
    /**
75
     * Creates the default time zone token on demand.
76
     *
77
     * @return Mailcode_Parser_Statement_Tokenizer_Token
78
     */
79
    private function createTimeZoneToken() : Mailcode_Parser_Statement_Tokenizer_Token
80
    {
81
        $default = Mailcode_Commands_Command_ShowDate::getDefaultTimezone();
82
83
        if($default instanceof Mailcode_Variables_Variable) {
84
            return new Mailcode_Parser_Statement_Tokenizer_Token_Variable(
85
                'showdate-timezone-token',
86
                $default->getFullName(),
87
                $default,
88
                $this
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands\Validation\TimezoneTrait is incompatible with the type Mailcode\Mailcode_Commands_Command|null expected by parameter $sourceCommand of Mailcode\Mailcode_Parser...Variable::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                /** @scrutinizer ignore-type */ $this
Loading history...
89
            );
90
        }
91
92
        return new Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral(
93
            'showdate-timezone-token',
94
            $default,
95
            null,
96
            $this
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands\Validation\TimezoneTrait is incompatible with the type Mailcode\Mailcode_Commands_Command|null expected by parameter $sourceCommand of Mailcode\Mailcode_Parser...gLiteral::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            /** @scrutinizer ignore-type */ $this
Loading history...
97
        );
98
    }
99
}
100