Test Failed
Pull Request — master (#13)
by Sebastian
05:15
created

DecryptTrait::validateSyntax_check_decrypt()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

33
        $token = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
34
            ->getInfo()
35
            ->getTokenForKeyword(Mailcode_Commands_Keywords::TYPE_DECRYPT);
0 ignored issues
show
Bug introduced by
The constant Mailcode\Mailcode_Commands_Keywords::TYPE_DECRYPT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
37
        if ($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
38
            $this->decryptionKeyToken = $token;
39
        }
40
41
        $val = $this->validator->createKeyword(Mailcode_Commands_Keywords::TYPE_DECRYPT);
42
43
        if ($this->decryptionKeyToken === null || !$val->isValid()) {
44
            return;
45
        }
46
47
        if (!$this->decryptionKeyToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) {
0 ignored issues
show
introduced by
$this->decryptionKeyToken is always a sub-type of Mailcode\Mailcode_Parser...zer_Token_StringLiteral.
Loading history...
48
            $this->validationResult->makeError(
49
                t('Invalid decryption key token:') . ' ' . t('Expected a string.'),
50
                DecryptInterface::VALIDATION_DECRYPT_CODE_WRONG_TYPE
51
            );
52
        }
53
    }
54
55
    /**
56
     * Gets the decryption key to use for the command. If none has
57
     * been specified in the original command, the default
58
     * decryption key is used as defined via {@see Mailcode_Commands_Command_ShowVariable::setDefaultDecryptionKey()}.
59
     *
60
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
61
     */
62
    public function getDecryptionKeyToken(): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
63
    {
64
        if (!isset($this->decryptionKeyToken)) {
65
            $this->decryptionKeyToken = $this->createDecryptionKeyToken();
66
        }
67
68
        return $this->decryptionKeyToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->decryptionKeyToken could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser...zer_Token_StringLiteral. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    /**
72
     * Creates the default decryption key token on demand.
73
     *
74
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
75
     */
76
    private function createDecryptionKeyToken(): Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral
77
    {
78
        $default = Mailcode_Commands_Command_ShowVariable::getDefaultDecryptionKey();
79
80
        return new Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral(
81
            'showvar-decryption-key-token',
82
            $default,
83
            null,
84
            $this
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands\Validation\DecryptTrait is incompatible with the type Mailcode\Mailcode_Commands_Command|null expected by parameter $sourceCommand of Mailcode\Mailcode_Parser...gLiteral::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            /** @scrutinizer ignore-type */ $this
Loading history...
85
        );
86
    }
87
}
88