Passed
Push — master ( af11e6...9ff364 )
by Sebastian
03:46
created

TrackingIDTrait::hasTrackingID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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\TrackingIDTrait} trait.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\TrackingIDTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Commands\Command\ShowURL\AutoTrackingID;
15
use Mailcode\Interfaces\Commands\Validation\TrackingIDInterface;
16
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
17
use Mailcode\Mailcode_Parser_Statement_Validator;
18
19
/**
20
 * Command validation drop-in: checks for the presence
21
 * of a tracking ID, which must be the first string
22
 * literal in the command's parameters list. If not
23
 * present or not a match for a tracking ID name, an
24
 * empty string is used as default.
25
 *
26
 * @package Mailcode
27
 * @subpackage Validation
28
 * @author Sebastian Mordziol <[email protected]>
29
 *
30
 * @see TrackingIDInterface
31
 * @property Mailcode_Parser_Statement_Validator $validator
32
 *
33
 */
34
trait TrackingIDTrait
35
{
36
    protected string $trackingID = '';
37
    private ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral $trackingIDToken = null;
38
39
    /**
40
     * @return string
41
     */
42
    public function getTrackingID() : string
43
    {
44
        return $this->trackingID ?? AutoTrackingID::generate($this);
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Traits\Commands...idation\TrackingIDTrait is incompatible with the type Mailcode\Interfaces\Comm...ion\TrackingIDInterface expected by parameter $command of Mailcode\Commands\Comman...oTrackingID::generate(). ( Ignorable by Annotation )

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

44
        return $this->trackingID ?? AutoTrackingID::generate(/** @scrutinizer ignore-type */ $this);
Loading history...
45
    }
46
47
    public function hasTrackingID() : bool
48
    {
49
        return !empty($this->trackingID);
50
    }
51
52
    /**
53
     * Checks if any of the parameters contain a trackingID.
54
     * This must be the first string literal in the parameters,
55
     * allowing any keywords to be placed before it, but not
56
     * after the optional query parameters.
57
     */
58
    protected function validateSyntax_tracking_id() : void
59
    {
60
        $literals = $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

60
        $literals = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
61
            ->getInfo()
62
            ->getStringLiterals();
63
64
        if(empty($literals))
65
        {
66
            return;
67
        }
68
69
        $trackingID = array_shift($literals);
70
71
        $id = $trackingID->getText();
72
73
        if(strpos($id, '=') === false)
74
        {
75
            $this->trackingID = $id;
76
            $this->trackingIDToken = $trackingID;
77
        }
78
    }
79
}
80