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

QueryParamsTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 74
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hasQueryParam() 0 3 1
A getQueryParams() 0 3 1
A analyzeQueryString() 0 9 1
A validateSyntax_query_params() 0 13 4
A getQueryParamTokens() 0 3 1
A getQueryParam() 0 8 2
A hasQueryParams() 0 3 1
1
<?php
2
/**
3
 * File containing the {@see \Mailcode\Traits\Commands\Validation\QueryParamsTrait} trait.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see \Mailcode\Traits\Commands\Validation\QueryParamsTrait
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Traits\Commands\Validation;
13
14
use Mailcode\Interfaces\Commands\Validation\QueryParamsInterface;
15
use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral;
16
17
/**
18
 * Command validation drop-in: checks for the presence
19
 * of query parameters specified in separate string
20
 * literals, identified by the format <code>"paramName=paramValue"</code>.
21
 *
22
 * @package Mailcode
23
 * @subpackage Validation
24
 * @author Sebastian Mordziol <[email protected]>
25
 *
26
 * @see QueryParamsInterface
27
 */
28
trait QueryParamsTrait
29
{
30
    /**
31
     * @var array<string,string>
32
     */
33
    private array $queryParams = array();
34
35
    /**
36
     * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
37
     */
38
    private array $queryParamTokens = array();
39
40
    protected function validateSyntax_query_params() : void
41
    {
42
        $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

42
        $literals = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
43
            ->getInfo()
44
            ->getStringLiterals();
45
46
        foreach($literals as $literal)
47
        {
48
            $text = $literal->getText();
49
50
            if(strpos($text, '=') !== false && $this->analyzeQueryString($text))
51
            {
52
                $this->queryParamTokens[] = $literal;
53
            }
54
        }
55
    }
56
57
    private function analyzeQueryString(string $param) : bool
58
    {
59
        $tokens = explode('=', $param);
60
        $name = trim(array_shift($tokens));
61
        $value = trim(implode('=', $tokens));
62
63
        $this->queryParams[$name] = $value;
64
65
        return true;
66
    }
67
68
    public function hasQueryParams() : bool
69
    {
70
        return !empty($this->queryParams);
71
    }
72
73
    /**
74
     * @return array<string,string>
75
     */
76
    public function getQueryParams() : array
77
    {
78
        return $this->queryParams;
79
    }
80
81
    /**
82
     * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[]
83
     */
84
    public function getQueryParamTokens() : array
85
    {
86
        return $this->queryParamTokens;
87
    }
88
89
    public function getQueryParam(string $name) : string
90
    {
91
        if(isset($this->queryParams[$name]))
92
        {
93
            return $this->queryParams[$name];
94
        }
95
96
        return '';
97
    }
98
99
    public function hasQueryParam(string $name) : bool
100
    {
101
        return isset($this->queryParams[$name]);
102
    }
103
}
104