GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c430e2...993294 )
by Nicholas
03:11
created

Codepoint::toEncodedCharacter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace UCD\Unicode;
4
5
use UCD\Exception\InvalidArgumentException;
6
use UCD\Exception\OutOfRangeException;
7
8
class Codepoint implements Comparable
9
{
10
    const MIN = 0x0;
11
    const MAX = 0x10FFFF;
12
13
    /**
14
     * @var int
15
     */
16
    private $value;
17
18
    /**
19
     * @param int $value
20
     * @throws OutOfRangeException
21
     * @throws InvalidArgumentException
22
     */
23
    private function __construct($value)
24
    {
25
        if (!is_int($value)) {
26
            throw new InvalidArgumentException('Codepoint value must be an integer');
27
        }
28
29
        if ($value < self::MIN || $value > self::MAX) {
30
            throw new OutOfRangeException('Codepoint value must reside between 0x0 and 0x10FFFF');
31
        }
32
33
        $this->value = $value;
34
    }
35
36
    /**
37
     * @param string $value
38
     * @return self
39
     */
40
    public static function fromHex($value)
41
    {
42
        return self::fromInt(hexdec($value));
43
    }
44
45
    /**
46
     * @param int $value
47
     * @return self
48
     */
49
    public static function fromInt($value)
50
    {
51
        return new self($value);
52
    }
53
54
    /**
55
     * @param string $value
56
     * @return self
57
     * @throws InvalidArgumentException
58
     */
59
    public static function fromUTF8($value)
60
    {
61
        return self::fromEncodedCharacter(
62
            $value,
63
            TransformationFormat::ofType(TransformationFormat::EIGHT)
64
        );
65
    }
66
67
    /**
68
     * @param string $value
69
     * @return self
70
     * @throws InvalidArgumentException
71
     */
72
    public static function fromUTF16($value)
73
    {
74
        return self::fromEncodedCharacter(
75
            $value,
76
            TransformationFormat::ofType(TransformationFormat::SIXTEEN)
77
        );
78
    }
79
80
    /**
81
     * @param string $character
82
     * @param TransformationFormat $encoding
83
     * @return self
84
     * @throws InvalidArgumentException
85
     */
86 View Code Duplication
    public static function fromEncodedCharacter($character, TransformationFormat $encoding)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $convertTo = TransformationFormat::ofType(TransformationFormat::THIRTY_TWO_BIG_ENDIAN);
89
        $converted = TransformationFormat\StringUtility::convertCharacter($character, $encoding, $convertTo);
90
91
        return self::fromUTF32($converted);
92
    }
93
94
    /**
95
     * @param string $value
96
     * @return self
97
     * @throws InvalidArgumentException
98
     */
99
    public static function fromUTF32($value)
100
    {
101
        if (strlen($value) !== 4) {
102
            throw new InvalidArgumentException('Single UTF-32 encoded character must be provided');
103
        }
104
105
        $unpacked = unpack('N', $value);
106
107
        return self::fromInt(
108
            array_shift($unpacked)
109
        );
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function toUTF8()
116
    {
117
        return $this->toEncodedCharacter(
118
            TransformationFormat::ofType(TransformationFormat::EIGHT)
119
        );
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function toUTF16()
126
    {
127
        return $this->toEncodedCharacter(
128
            TransformationFormat::ofType(TransformationFormat::SIXTEEN_BIG_ENDIAN)
129
        );
130
    }
131
132
    /**
133
     * @param TransformationFormat $encoding
134
     * @return string
135
     * @throws InvalidArgumentException
136
     */
137 View Code Duplication
    public function toEncodedCharacter(TransformationFormat $encoding)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $character = $this->toUTF32();
140
        $convertFrom = TransformationFormat::ofType(TransformationFormat::THIRTY_TWO);
141
142
        return TransformationFormat\StringUtility::convertCharacter($character, $convertFrom, $encoding);
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public function toUTF32()
149
    {
150
        return pack('N', $this->value);
151
    }
152
153
    /**
154
     * @return int
155
     */
156
    public function getValue()
157
    {
158
        return $this->value;
159
    }
160
161
    /**
162
     * @param mixed $other
163
     * @return bool
164
     */
165 View Code Duplication
    public function equals($other)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
    {
167
        if ($this === $other) {
168
            return true;
169
        }
170
171
        return $other instanceof self
172
            && $this->value === $other->value;
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function __toString()
179
    {
180
        return sprintf('U+%X', $this->value);
181
    }
182
}