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); |
|
|
|
|
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
|
|
|
|
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.