Passed
Push — master ( c5f2a7...8e75e2 )
by Christian
02:33
created

DataAbstract::parseDataToArray()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace RemotelyLiving\PHPDNS\Entities;
3
4
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
5
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
6
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
7
8
abstract class DataAbstract implements Arrayable, Serializable
9
{
10
    abstract public function __toString();
11
12
    public function equals(DataAbstract $dataAbstract): bool
13
    {
14
        return (string)$this === (string)$dataAbstract;
15
    }
16
17
    public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self
18
    {
19
        if ($recordType->isA(DNSRecordType::TYPE_TXT)) {
20
            return new TXTData($data);
21
        }
22
23
        if ($recordType->isA(DNSRecordType::TYPE_NS)) {
24
            return new NSData(new Hostname($data));
25
        }
26
27
        if ($recordType->isA(DNSRecordType::TYPE_CNAME)) {
28
            return new CNAMEData(new Hostname($data));
29
        }
30
31
        $parsed = self::parseDataToArray($data);
32
33
        if ($recordType->isA(DNSRecordType::TYPE_MX)) {
34
            return new MXData(new Hostname($parsed[1]), (int)$parsed[0]);
35
        }
36
37
        if ($recordType->isA(DNSRecordType::TYPE_SOA)) {
38
            return new SOAData(
39
                new Hostname($parsed[0]),
40
                new Hostname($parsed[1]),
41
                (int)$parsed[2] ?? 0,
42
                (int)$parsed[3] ?? 0,
43
                (int)$parsed[4] ?? 0,
44
                (int)$parsed[5] ?? 0,
45
                (int)$parsed[6] ?? 0
46
            );
47
        }
48
49
50
51
        if ($recordType->isA(DNSRecordType::TYPE_CAA)) {
52
            return new CAAData($parsed[0], $parsed[1], $parsed[2]);
0 ignored issues
show
Bug introduced by
$parsed[0] of type string is incompatible with the type integer expected by parameter $flags of RemotelyLiving\PHPDNS\En...\CAAData::__construct(). ( Ignorable by Annotation )

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

52
            return new CAAData(/** @scrutinizer ignore-type */ $parsed[0], $parsed[1], $parsed[2]);
Loading history...
53
        }
54
55
        throw new InvalidArgumentException("{$data} could not be created with type {$recordType}");
56
    }
57
58
    private static function parseDataToArray(string $data): array
59
    {
60
        return explode(' ', $data);
61
    }
62
}
63