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.

StringUtility   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertString() 0 11 2
A convertCharacter() 0 10 2
A mapFormat() 0 8 2
A split() 0 12 2
1
<?php
2
3
namespace UCD\Unicode\TransformationFormat;
4
5
use UCD\Exception\InvalidArgumentException;
6
use UCD\Unicode\TransformationFormat;
7
8
class StringUtility
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $iconvMap = [
14
        TransformationFormat::EIGHT => 'UTF-8',
15
        TransformationFormat::SIXTEEN => 'UTF-16',
16
        TransformationFormat::SIXTEEN_BIG_ENDIAN => 'UTF-16BE',
17
        TransformationFormat::SIXTEEN_LITTLE_ENDIAN => 'UTF-16LE',
18
        TransformationFormat::THIRTY_TWO => 'UTF-32',
19
        TransformationFormat::THIRTY_TWO_BIG_ENDIAN => 'UTF-32BE',
20
        TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN => 'UTF-32LE'
21
    ];
22
23
    /**
24
     * @param string $string
25
     * @param TransformationFormat $from
26
     * @param TransformationFormat $to
27
     * @return string
28
     */
29
    public static function convertString($string, TransformationFormat $from, TransformationFormat $to)
30
    {
31
        if ($from->equals($to)) {
32
            return $string;
33
        }
34
35
        $in = self::mapFormat($from);
36
        $out = self::mapFormat($to);
37
38
        return iconv($in, $out, $string);
39
    }
40
41
    /**
42
     * @param string $character
43
     * @param TransformationFormat $from
44
     * @param TransformationFormat $to
45
     * @throws InvalidArgumentException
46
     * @return string
47
     */
48
    public static function convertCharacter($character, TransformationFormat $from, TransformationFormat $to)
49
    {
50
        $in = self::mapFormat($from);
51
52
        if (iconv_strlen($character, $in) !== 1) {
53
            throw new InvalidArgumentException('Single character must be provided');
54
        }
55
56
        return self::convertString($character, $from, $to);
57
    }
58
59
    /**
60
     * @param TransformationFormat $format
61
     * @return string
62
     * @throws InvalidArgumentException
63
     */
64
    private static function mapFormat(TransformationFormat $format)
65
    {
66
        if (!array_key_exists($format->getType(), self::$iconvMap)) {
67
            throw new InvalidArgumentException();
68
        }
69
70
        return self::$iconvMap[$format->getType()];
71
    }
72
73
    /**
74
     * @param string $string
75
     * @param TransformationFormat $format
76
     * @return string[]
77
     * @throws InvalidArgumentException
78
     */
79
    public static function split($string, TransformationFormat $format)
80
    {
81
        $encoding = self::mapFormat($format);
82
        $characters = [];
83
84
        for ($i = 0; $i < iconv_strlen($string, $encoding); $i++) {
85
            $character = iconv_substr($string, $i, 1, $encoding);
86
            array_push($characters, $character);
87
        }
88
89
        return $characters;
90
    }
91
}