Passed
Pull Request — master (#10)
by Sebastian
03:37
created

TimezoneTrait::validateSyntax_check_timezone()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
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
16
/**
17
 * @package Mailcode
18
 * @subpackage Validation
19
 * @author Olaf Böcker <[email protected]>
20
 *
21
 * @see TimezoneInterface
22
 */
23
trait TimezoneTrait
24
{
25
    /**
26
     * The timezone
27
     * @var string|NULL
28
     */
29
    protected ?string $timezoneString = null;
30
    private ?string $timezoneVariable = null;
31
32
    protected function validateSyntax_check_timezone(): void
33
    {
34
        // first, check if we have an explicit timezone
35
        $tokens = $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

35
        $tokens = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
36
            ->getInfo()
37
            ->getStringLiterals();
38
39
        if (count($tokens) > 1) {
40
            $this->timezoneString = $tokens[1]->getText();
41
            return;
42
        }
43
44
        // then, check if a variable is used for timezone
45
        $variables = $this->requireParams()
46
            ->getInfo()
47
            ->getVariables();
48
49
        if (count($variables) > 1) {
50
            $this->timezoneVariable = $variables[1]->getFullName();
51
            return;
52
        }
53
54
        // neither explicit timezone nor variable present, so use nothing
55
        $this->timezoneString= null;
56
        $this->timezoneVariable = null;
57
    }
58
59
    public function getTimezoneString(): ?string
60
    {
61
        return $this->timezoneString;
62
    }
63
64
    public function getTimezoneVariable() : ?string
65
    {
66
        return $this->timezoneVariable;
67
    }
68
69
    public function hasTimezone() : bool
70
    {
71
        return isset($this->timezoneString) || isset($this->timezoneVariable);
72
    }
73
}
74