Passed
Push — master ( 8482a1...034aeb )
by Sebastian
15:02
created

NamespaceTrait::isNamespacePresent()   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
use AppUtils\ConvertHelper;
15
use Mailcode\Commands\CommandException;
16
17
/**
18
 * @package Mailcode
19
 * @subpackage Validation
20
 * @author Olaf Böcker <[email protected]>
21
 *
22
 * @see NamespaceInterface
23
 */
24
trait NamespaceTrait
25
{
26
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $namespaceToken = null;
27
28
    protected function validateSyntax_check_namespace(): void
29
    {
30
        $token = $this
31
            ->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

31
            ->/** @scrutinizer ignore-call */ requireParams()
Loading history...
32
            ->getInfo()
33
            ->getTokenByParamName(NamespaceInterface::PARAMETER_NAMESPACE_NAME);
34
35
        if ($token === null) {
36
            return;
37
        }
38
39
        if (!$token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
40
            $this->validationResult->makeError(
41
                t('Invalid namespace token:') . ' ' . t('Expected a string.'),
42
                NamespaceInterface::VALIDATION_NAMESPACE_WRONG_TYPE
43
            );
44
            return;
45
        }
46
47
        $this->namespaceToken = $token;
48
    }
49
50
    public function isNamespacePresent(): bool
51
    {
52
        return $this->getNamespaceToken() !== null;
53
    }
54
55
    public function getNamespaceToken(): ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
56
    {
57
        return $this->namespaceToken;
58
    }
59
60
    public function requireNamespaceToken() : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
61
    {
62
        $token = $this->getNamespaceToken();
63
64
        if($token !== null) {
65
            return $token;
66
        }
67
68
        throw new CommandException(
69
            'The command does not have a namespace token.',
70
            sprintf(
71
                'A namespace token was required, but not present. '.PHP_EOL.
72
                'Use [%s] to check for its presence before calling this method.',
73
                ConvertHelper::callback2string(array($this, 'isNamespacePresent'))
74
            ),
75
            NamespaceInterface::ERROR_NO_NAMESPACE_TOKEN_PRESENT
76
        );
77
    }
78
79
    public function setNamespace(string $namespace = NamespaceInterface::DEFAULT_NAMESPACE): self
80
    {
81
        $this->namespaceToken = $this
82
            ->requireParams()
83
            ->getInfo()
84
            ->addParamString(NamespaceInterface::PARAMETER_NAMESPACE_NAME, $namespace);
85
86
        return $this;
87
    }
88
}
89