Passed
Pull Request — master (#10)
by
unknown
08:56
created

TimezoneTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 49
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasTimezone() 0 3 2
A validateSyntax_check_timezone() 0 25 3
A getTimezoneString() 0 3 1
A getTimezoneVariable() 0 3 1
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