Passed
Push — master ( ae9515...0e1b0c )
by Alec
01:58
created

UCode::isB()   B

Complexity

Conditions 10
Paths 31

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 7
c 1
b 0
f 0
nc 31
nop 1
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 10
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Helpers;
4
5
class UCode
6
{
7
    /**
8
     * @param string $subject
9
     * @param null|int $n
10
     * @return int
11
     */
12 10
    public static function wcswidth(string $subject, ?int $n = null): int
13
    {
14 10
        $end = $n ?? mb_strlen($subject);
15 10
        $chrArray = array_slice(
16 10
            static::split($subject),
17 10
            0,
18 10
            $end
19
        );
20 10
        $width = 0;
21 10
        foreach ($chrArray as $char) {
22 10
            $wcw = static::wcwidth($char);
23 10
            if ($wcw < 0) {
24 1
                return -1;
25
            }
26 9
            $width += $wcw;
27
        }
28 9
        return $width;
29
    }
30
31
    /**
32
     * @param string $wc
33
     * @return int
34
     */
35 10
    public static function wcwidth(string $wc): int
36
    {
37 10
        $ucs = mb_ord($wc);
0 ignored issues
show
Bug introduced by
The call to mb_ord() has too few arguments starting with encoding. ( Ignorable by Annotation )

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

37
        $ucs = /** @scrutinizer ignore-call */ mb_ord($wc);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
38
39 10
        if (self::isB($ucs)) {
40 1
            return 0;
41
        }
42
43
        # C0/C1 control characters
44 10
        if ($ucs < 32 || (0x07F <= $ucs && $ucs < 0x0A0)) {
45 1
            return -1;
46
        }
47
48
        # combining characters with zero width
49 10
        if (static::bisearch($ucs, ZERO_WIDTH)) {
50 3
            return 0;
51
        }
52
53 10
        return 1 + static::bisearch($ucs, WIDE_EASTASIAN);
54
    }
55
56
    /**
57
     * @param string $subject
58
     * @return array
59
     */
60 10
    protected static function split(string $subject): array
61
    {
62 10
        $_split = preg_split('//u', $subject, -1, PREG_SPLIT_NO_EMPTY);
63
        // @codeCoverageIgnoreStart
64
        if (!\is_array($_split)) {
65
            // Should never happen
66
            throw new \RuntimeException('Failed to split string.');
67
        }
68
        // @codeCoverageIgnoreEnd
69 10
        return $_split;
70
    }
71
72 10
    protected static function bisearch(int $ucs, array $table): int
73
    {
74 10
        $lbound = 0;
75 10
        $ubound = count($table) - 1;
76
77 10
        if ($ucs < $table[0][0] || $ucs > $table[$ubound][1]) {
78 8
            return 0;
79
        }
80 8
        while ($ubound >= $lbound) {
81 8
            $mid = ($lbound + $ubound); // 2
82 8
            if ($ucs > $table[$mid][1]) {
83 6
                $lbound = $mid + 1;
84 8
            } elseif ($ucs < $table[$mid][0]) {
85 8
                $ubound = $mid - 1;
86
            } else {
87 7
                return 1;
88
            }
89
        }
90 6
        return 0;
91
    }
92
93
    /**
94
     * @param int $ucs
95
     * @return bool
96
     */
97 10
    protected static function isB(int $ucs): bool
98
    {
99 10
        return $ucs === 0 ||
100 10
            $ucs === 0x034F ||
101 10
            (0x200B <= $ucs && $ucs <= 0x200F) ||
102 10
            $ucs === 0x2028 ||
103 10
            $ucs === 0x2029 ||
104 10
            (0x202A <= $ucs && $ucs <= 0x202E) ||
105 10
            (0x2060 <= $ucs && $ucs <= 0x2063);
106
    }
107
}
108