ReferrerPolicyHeader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 19
dl 0
loc 39
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A valueIsIn() 0 6 1
A mayLeakOrigin() 0 10 1
A parse() 0 5 1
A doesNotLeakReferrer() 0 10 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\ReferrerPolicyHeaderResult;
11
use nicoSWD\SecHeaderCheck\Domain\Validator\AbstractHeaderParser;
12
13
final class ReferrerPolicyHeader extends AbstractHeaderParser
14
{
15 4
    public function parse(): ReferrerPolicyHeaderResult
16
    {
17 4
        return (new ReferrerPolicyHeaderResult($this->getName(), $this->getValue()))
18 4
            ->setMayLeakOrigin($this->mayLeakOrigin())
19 4
            ->setDoesNotLeakReferrer($this->doesNotLeakReferrer());
20
    }
21
22 4
    private function doesNotLeakReferrer(): bool
23
    {
24
        $secureReferrerOptions = [
25 4
            'no-referrer',
26
            'no-referrer-when-downgrade',
27
            'same-origin',
28
            'strict-origin',
29
        ];
30
31 4
        return $this->valueIsIn($secureReferrerOptions);
32
    }
33
34 4
    private function mayLeakOrigin(): bool
35
    {
36
        $leakyReferrerOptions = [
37 4
            'origin',
38
            'origin-when-cross-origin',
39
            'strict-origin-when-cross-origin',
40
            'unsafe-url',
41
        ];
42
43 4
        return $this->valueIsIn($leakyReferrerOptions);
44
    }
45
46 4
    private function valueIsIn(array $options): bool
47
    {
48 4
        $value = strtolower($this->getValue());
49 4
        $values = preg_split('~\s*,\s*~', $value, -1, PREG_SPLIT_NO_EMPTY);
50
51 4
        return ($options !== array_intersect($options, $values));
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type false; however, parameter $array2 of array_intersect() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

51
        return ($options !== array_intersect($options, /** @scrutinizer ignore-type */ $values));
Loading history...
52
    }
53
}
54