Passed
Pull Request — master (#11)
by Sebastian
06:57 queued 03:04
created

CountTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isCountEnabled() 0 3 1
A validateSyntax_check_count() 0 18 4
A getCountVariable() 0 3 2
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Traits\Commands\Validation\CountTrait} trait.
4
 *
5
 * @see \Mailcode\Traits\Commands\Validation\CountTrait
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\CountInterface;
15
use Mailcode\Mailcode_Commands_Keywords;
16
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable;
17
use Mailcode\Mailcode_Variables_Variable;
18
use function Mailcode\t;
19
20
/**
21
 * Command validation drop-in: checks for the presence
22
 * of the `count:` keyword in the command statement.
23
 *
24
 * @package Mailcode
25
 * @subpackage Validation
26
 * @author Olaf Böcker <[email protected]>
27
 *
28
 * @see CountInterface
29
 */
30
trait CountTrait
31
{
32
    private bool $countEnabled = false;
33
34
    private ?Mailcode_Parser_Statement_Tokenizer_Token_Variable $countToken = null;
35
36
    protected function validateSyntax_check_count(): void
37
    {
38
        $token = $this->requireParams()->getInfo()->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_COUNT);
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

38
        $token = $this->/** @scrutinizer ignore-call */ requireParams()->getInfo()->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_COUNT);
Loading history...
39
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
40
            $this->countToken = $token;
41
        }
42
43
        $val = $this->validator->createKeyword(Mailcode_Commands_Keywords::TYPE_COUNT);
44
45
        $this->countEnabled = $val->isValid();
46
47
        if ($this->countEnabled) {
48
            if (!$this->countToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) {
49
                $this->validationResult->makeError(
50
                    t('Invalid count type.' . ' ' . 'Expected Variable.'),
51
                    CountInterface::VALIDATION_COUNT_CODE_WRONG_TYPE
52
                );
53
                return;
54
            }
55
        }
56
    }
57
58
    public function isCountEnabled(): bool
59
    {
60
        return $this->countEnabled;
61
    }
62
63
    public function getCountVariable(): ?Mailcode_Variables_Variable
64
    {
65
        return $this->countToken != null ? $this->countToken->getVariable() : null;
66
    }
67
}
68