XXSSProtectionHeader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 33
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isBlocking() 0 3 1
A parse() 0 8 1
A hasReportUri() 0 3 1
A getOptions() 0 3 1
A protectionIsOn() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * @license  http://opensource.org/licenses/mit-license.php MIT
5
 * @link     https://github.com/nicoSWD
6
 * @author   Nicolas Oelgart <[email protected]>
7
 */
8
namespace nicoSWD\SecHeaderCheck\Domain\Validator\Header;
9
10
use nicoSWD\SecHeaderCheck\Domain\Result\Result\XXSSProtectionHeaderResult;
11
use nicoSWD\SecHeaderCheck\Domain\Validator\AbstractHeaderParser;
12
13
final class XXSSProtectionHeader extends AbstractHeaderParser
14
{
15
    private const MODE_ON = '1';
16
    private const MODE_BLOCK = 'mode=block';
17
18 8
    public function parse(): XXSSProtectionHeaderResult
19
    {
20 8
        $options = $this->getOptions();
21
22 8
        return (new XXSSProtectionHeaderResult($this->getName(), $this->getValue()))
23 8
            ->setProtectionIsOn($this->protectionIsOn($options))
24 8
            ->setIsBlocking($this->isBlocking($options))
25 8
            ->setHasReportUri($this->hasReportUri($options));
26
    }
27
28 8
    private function protectionIsOn(array $options): bool
29
    {
30 8
        return in_array(self::MODE_ON, $options, true);
31
    }
32
33 8
    private function isBlocking(array $options): bool
34
    {
35 8
        return in_array(self::MODE_BLOCK, $options, true);
36
    }
37
38 8
    private function hasReportUri(array $options): bool
39
    {
40 8
        return count(preg_grep('~report=~', $options)) === 1;
41
    }
42
43 8
    private function getOptions(): array
44
    {
45 8
        return preg_split('~\s*;\s*~', $this->getValue(), -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_split('~\s*;...er\PREG_SPLIT_NO_EMPTY) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
}
48