Passed
Push — master ( 2b422d...2c1f94 )
by Sebastian
03:35
created

BreakAtTrait::isBreakAtEnabled()   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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Traits\Commands\Validation\BreakAtTrait} trait.
4
 *
5
 * @see \Mailcode\Traits\Commands\Validation\BreakAtTrait
6
 * @subpackage Validation
7
 * @package Mailcode
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Interfaces\Commands\Validation\BreakAtInterface;
15
use Mailcode\Mailcode_Commands_Keywords;
16
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Process_StringLiterals;
17
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token;
18
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Number;
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
 * Command validation drop-in: checks for the presence
26
 * of the `break-at:` keyword in the command statement.
27
 *
28
 * @package Mailcode
29
 * @subpackage Validation
30
 * @author Olaf Böcker <[email protected]>
31
 *
32
 * @see BreakAtInterface
33
 */
34
trait BreakAtTrait
35
{
36
    private bool $breakAtEnabled = false;
37
38
    private ?Mailcode_Parser_Statement_Tokenizer_Token $breakAtToken = null;
39
40
    protected function validateSyntax_check_break_at(): void
41
    {
42
        $this->breakAtToken = $this->requireParams()->getInfo()->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_BREAK_AT);
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

42
        $this->breakAtToken = $this->/** @scrutinizer ignore-call */ requireParams()->getInfo()->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_BREAK_AT);
Loading history...
43
44
        $val = $this->validator->createKeyword(Mailcode_Commands_Keywords::TYPE_BREAK_AT);
45
46
        $this->breakAtEnabled = $val->isValid() && $this->breakAtToken != null;;
47
48
        if ($this->breakAtEnabled) {
49
            if (!$this->breakAtToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number &&
50
                !$this->breakAtToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
51
                $this->validationResult->makeError(
52
                    t('Invalid break-at type.' . ' ' . 'Expected Number or Variable.'),
53
                    BreakAtInterface::VALIDATION_BREAK_AT_CODE_WRONG_TYPE
54
                );
55
                return;
56
            }
57
        }
58
    }
59
60
    public function isBreakAtEnabled(): bool
61
    {
62
        return $this->breakAtEnabled;
63
    }
64
65
    public function getBreakAtToken(): ?Mailcode_Parser_Statement_Tokenizer_Token
66
    {
67
        return $this->breakAtToken;
68
    }
69
70
    /**
71
     * @return Mailcode_Variables_Variable|int|NULL
72
     */
73
    public function getBreakAt()
74
    {
75
        $token = $this->getBreakAtToken();
76
        if($token === null) {
77
            return null;
78
        }
79
80
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
81
            return $token->getVariable();
82
        }
83
84
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number) {
85
            return (int)$token->getValue();
86
        }
87
88
        if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
89
            return (int)$token->getText();
90
        }
91
92
        return null;
93
    }
94
}
95