Passed
Pull Request — master (#48)
by Christian
02:15
created

CAAData::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Entities;
4
5
use RemotelyLiving\PHPDNS\Exceptions;
6
7
use function preg_match;
8
use function serialize;
9
use function str_ireplace;
10
use function trim;
11
use function unserialize;
12
13
final class CAAData extends DataAbstract implements \Stringable
14
{
15
    private ?string $value;
16
17
    public function __construct(private int $flags, private string $tag, string $value = null)
18
    {
19
        $this->value = ($value)
20
            ? $this->normalizeValue($value)
21
            : null;
22
    }
23
24
    public function __toString(): string
25
    {
26
        return "{$this->flags} {$this->tag} \"{$this->value}\"";
27
    }
28
29
    public function __unserialize(array $unserialized): void
30
    {
31
        $this->flags = $unserialized['flags'];
32
        $this->tag = $unserialized['tag'];
33
        $this->value = $unserialized['value'];
34
    }
35
36
    public function getFlags(): int
37
    {
38
        return $this->flags;
39
    }
40
41
    public function getTag(): string
42
    {
43
        return $this->tag;
44
    }
45
46
    public function getValue(): ?string
47
    {
48
        return $this->value;
49
    }
50
51
    public function toArray(): array
52
    {
53
        return [
54
            'flags' => $this->flags,
55
            'tag' => $this->tag,
56
            'value' => $this->value,
57
        ];
58
    }
59
60
    private function normalizeValue(string $value): string
61
    {
62
        $normalized = trim(str_ireplace('"', '', $value));
0 ignored issues
show
Bug introduced by
It seems like str_ireplace('"', '', $value) can also be of type array; however, parameter $string of trim() does only seem to accept string, 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

62
        $normalized = trim(/** @scrutinizer ignore-type */ str_ireplace('"', '', $value));
Loading history...
63
64
        if (preg_match('/\s/m', $normalized)) {
65
            throw new Exceptions\InvalidArgumentException("$value is not a valid CAA value");
66
        }
67
68
        return $normalized;
69
    }
70
}
71