Passed
Push — master ( 472134...e9e97f )
by
unknown
07:15 queued 03:21
created

NamespaceTrait::getNamespaceToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Traits\Commands\Validation\NamespaceTrait}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\NamespaceTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * @package Mailcode
16
 * @subpackage Validation
17
 * @author Olaf Böcker <[email protected]>
18
 *
19
 * @see NamespaceInterface
20
 */
21
trait NamespaceTrait
22
{
23
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $namespaceToken = null;
24
25
    protected function validateSyntax_check_namespace(): void
26
    {
27
        $token = $this
28
            ->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

28
            ->/** @scrutinizer ignore-call */ requireParams()
Loading history...
29
            ->getInfo()
30
            ->getTokenByParamName(NamespaceInterface::PARAMETER_NAMESPACE_NAME);
31
32
        if ($token === null) {
33
            return;
34
        }
35
36
        if (!$token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
37
            $this->validationResult->makeError(
38
                t('Invalid namespace token:') . ' ' . t('Expected a string.'),
39
                NamespaceInterface::VALIDATION_NAMESPACE_WRONG_TYPE
40
            );
41
            return;
42
        }
43
44
        $this->namespaceToken = $token;
45
    }
46
47
    public function isNamespacePresent(): bool
48
    {
49
        return $this->getNamespaceToken() !== null;
50
    }
51
52
    public function getNamespaceToken(): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
53
    {
54
        return $this->namespaceToken;
55
    }
56
57
    public function setNamespace(string $namespace = NamespaceInterface::DEFAULT_NAMESPACE): self
58
    {
59
        $this->namespaceToken = $this
60
            ->requireParams()
61
            ->getInfo()
62
            ->addParamString(NamespaceInterface::PARAMETER_NAMESPACE_NAME, $namespace);
63
64
        return $this;
65
    }
66
}
67