Completed
Branch Version3 (97e220)
by Sam
01:25
created

RdataTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 81
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 5 1
A getTypeCode() 0 5 1
A output() 0 6 1
A encodeName() 0 15 3
A decodeName() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
trait RdataTrait
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 30
    public function getType(): string
22
    {
23
        /* @const TYPE */
24 30
        return static::TYPE;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 4
    public function getTypeCode(): int
31
    {
32
        /* @const TYPE_CODE */
33 4
        return static::TYPE_CODE;
34
    }
35
36
    /**
37
     * @deprecated
38
     *
39
     * @return string
40
     */
41 3
    public function output(): string
42
    {
43 3
        @trigger_error('Method RdataInterface::output() has been deprecated. Use RdataInterface::toText().', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
45 3
        return $this->toText();
0 ignored issues
show
Bug introduced by
It seems like toText() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46
    }
47
48
    /**
49
     * Encode a domain name as a sequence of labels.
50
     *
51
     * @param string $name
52
     *
53
     * @return string
54
     */
55 26
    public static function encodeName(string $name): string
56
    {
57 26
        if ('.' === $name) {
58 2
            return chr(0);
59
        }
60
61 24
        $name = rtrim($name, '.').'.';
62 24
        $res = '';
63
64 24
        foreach (explode('.', $name) as $label) {
65 24
            $res .= chr(strlen($label)).$label;
66
        }
67
68 24
        return $res;
69
    }
70
71
    /**
72
     * @param string $string
73
     * @param int    $offset
74
     *
75
     * @return string
76
     */
77 14
    public static function decodeName(string $string, int &$offset = 0): string
78
    {
79 14
        $len = ord($string[$offset]);
80 14
        ++$offset;
81
82 14
        if (0 === $len) {
83 2
            return '.';
84
        }
85
86 12
        $name = '';
87 12
        while (0 !== $len) {
88 12
            $name .= substr($string, $offset, $len).'.';
89 12
            $offset += $len;
90 12
            $len = ord($string[$offset]);
91 12
            ++$offset;
92
        }
93
94 12
        return $name;
95
    }
96
}
97