Skip to content

Commit

Permalink
Merge pull request #195 from voku/analysis-7W6nYj
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
voku authored Sep 4, 2023
2 parents b8ce36b + 76e9c3b commit 6ee5a2a
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 64 deletions.
26 changes: 8 additions & 18 deletions src/voku/helper/UTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,6 @@ public static function chr($code_point, string $encoding = 'UTF-8')
}

if ($code_point <= 0x80) { // only for "simple"-chars

if (self::$CHR === null) {
self::$CHR = self::getData('chr');
}
Expand Down Expand Up @@ -8177,7 +8176,6 @@ public static function str_split(
\preg_match_all('/./us', $str, $return_array);
$ret = $return_array[0] ?? [];
} else {

// fallback

$ret = [];
Expand Down Expand Up @@ -9197,13 +9195,13 @@ public static function str_truncate(
}

return (
(string) self::substr(
$str,
0,
$length,
$encoding
)
) . $substring;
(string) self::substr(
$str,
0,
$length,
$encoding
)
) . $substring;
}

/**
Expand Down Expand Up @@ -12805,9 +12803,7 @@ public static function to_utf8_string(string $str, bool $decode_html_entity_to_u
$c1 = $str[$i];

if ($c1 >= "\xC0") { // should be converted to UTF8, if it's not UTF8 already

if ($c1 <= "\xDF") { // looks like 2 bytes UTF8

$c2 = $i + 1 >= $max ? "\x00" : $str[$i + 1];

if ($c2 >= "\x80" && $c2 <= "\xBF") { // yeah, almost sure it's UTF8 already
Expand All @@ -12817,7 +12813,6 @@ public static function to_utf8_string(string $str, bool $decode_html_entity_to_u
$buf .= self::to_utf8_convert_helper($c1);
}
} elseif ($c1 >= "\xE0" && $c1 <= "\xEF") { // looks like 3 bytes UTF8

$c2 = $i + 1 >= $max ? "\x00" : $str[$i + 1];
$c3 = $i + 2 >= $max ? "\x00" : $str[$i + 2];

Expand All @@ -12828,7 +12823,6 @@ public static function to_utf8_string(string $str, bool $decode_html_entity_to_u
$buf .= self::to_utf8_convert_helper($c1);
}
} elseif ($c1 >= "\xF0" && $c1 <= "\xF7") { // looks like 4 bytes UTF8

$c2 = $i + 1 >= $max ? "\x00" : $str[$i + 1];
$c3 = $i + 2 >= $max ? "\x00" : $str[$i + 2];
$c4 = $i + 3 >= $max ? "\x00" : $str[$i + 3];
Expand All @@ -12840,14 +12834,11 @@ public static function to_utf8_string(string $str, bool $decode_html_entity_to_u
$buf .= self::to_utf8_convert_helper($c1);
}
} else { // doesn't look like UTF8, but should be converted

$buf .= self::to_utf8_convert_helper($c1);
}
} elseif (($c1 & "\xC0") === "\x80") { // needs conversion

$buf .= self::to_utf8_convert_helper($c1);
} else { // it doesn't need conversion

$buf .= $c1;
}
}
Expand Down Expand Up @@ -13262,7 +13253,7 @@ public static function utf8_decode(string $str, bool $keep_utf8_chars = false):
case "\xF0":
++$i;

// no break
// no break

case "\xE0":
$str[$j] = $no_char_found;
Expand Down Expand Up @@ -13642,7 +13633,6 @@ private static function is_utf8_string(string $str, bool $strict = false)
return false;
}
} elseif ((0xC0 & $in) === 0x80) {

// When mState is non-zero, we expect a continuation of the multi-octet
// sequence

Expand Down
23 changes: 11 additions & 12 deletions tests/ShimIconvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public function testIconv()
// Native iconv() behavior varies between versions and OS for these two tests
// See e.g. https://bugs.php.net/52211
if (
!\defined('HHVM_VERSION')
&&
(
\PHP_VERSION_ID >= 50610
||
(\PHP_VERSION_ID >= 50526 && \PHP_VERSION_ID < 50600)
||
'\\' === \DIRECTORY_SEPARATOR
)
) {
!\defined('HHVM_VERSION')
&&
(
\PHP_VERSION_ID >= 50610
||
(\PHP_VERSION_ID >= 50526 && \PHP_VERSION_ID < 50600)
||
'\\' === \DIRECTORY_SEPARATOR
)
) {
/** @noinspection PhpUsageOfSilenceOperatorInspection */
static::assertSame(\PHP_VERSION_ID >= 50400 ? false : 'n', @\iconv('UTF-8', 'ISO-8859-1', 'nœud'));
static::assertSame('nud', \iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud'));
Expand All @@ -45,8 +45,7 @@ public function testIconv()
static::assertSame('nud', @\iconv('UTF-8', 'ISO-8859-1//IGNORE', 'nœud'));
}
} else {

// See e.g. https://bugs.php.net/52211
// See e.g. https://bugs.php.net/52211
/** @noinspection PhpUndefinedConstantInspection */
if (\defined('HHVM_VERSION') && HHVM_VERSION_ID >= 30901) {
/** @noinspection PhpUsageOfSilenceOperatorInspection */
Expand Down
10 changes: 0 additions & 10 deletions tests/Utf8GlobalNonStrictPart1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ public function testChar()
}

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -959,7 +958,6 @@ public function testFileGetContents()
// ---

if (UTF8::mbstring_loaded()) { // only with "mbstring"

static::assertTrue(UTF8::is_binary_file(__DIR__ . '/fixtures/utf-16-be.txt'));
$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
if (\method_exists(__CLASS__, 'assertStringContainsString')) {
Expand Down Expand Up @@ -1012,7 +1010,6 @@ public function testFileGetContents()
}

if (UTF8::mbstring_loaded()) { // only with "mbstring"

$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
if (\method_exists(__CLASS__, 'assertStringContainsString')) {
static::assertStringContainsString(
Expand Down Expand Up @@ -1358,7 +1355,6 @@ public function testFixSimpleUtf8()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -2614,7 +2610,6 @@ public function testNormalizeWhitespace()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -2656,7 +2651,6 @@ public function testOrd()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -2712,7 +2706,6 @@ public function testOrd()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3082,7 +3075,6 @@ public function testSplit()
{
$oldSupportArray = null;
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3132,7 +3124,6 @@ public function testStrDetectEncoding()
];

for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3690,7 +3681,6 @@ public function testStripWhitespace($expected, $str)
public function testStripos()
{
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down
3 changes: 0 additions & 3 deletions tests/Utf8GlobalNonStrictPart2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public function testStrlen()
];

for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -282,7 +281,6 @@ public function testStrposInByte()
public function testStrpos()
{
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -366,7 +364,6 @@ public function testStrpos()
// --- invalid UTF-8

if (UTF8::getSupportInfo('mbstring') === true) { // only with "mbstring"

static::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', ''));

if (Bootup::is_php('7.1')) {
Expand Down
1 change: 0 additions & 1 deletion tests/Utf8GlobalNonStrictPart3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,6 @@ public function testToUtf8V3()
public function testTrim($input, $output)
{
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down
15 changes: 2 additions & 13 deletions tests/Utf8GlobalPart1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public function testChar()
}

for ($i = 0; $i < 200; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -965,7 +964,6 @@ public function testFileGetContents()
// ---

if (UTF8::mbstring_loaded()) { // only with "mbstring"

static::assertTrue(UTF8::is_binary_file(__DIR__ . '/fixtures/utf-16-be.txt'));
$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
static::assertStringContainsString(
Expand All @@ -992,7 +990,6 @@ public function testFileGetContents()
static::assertStringContainsString('Iñtërnâtiônàlizætiøn', $testString);

if (UTF8::mbstring_loaded()) { // only with "mbstring"

$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
static::assertStringContainsString(
'<p>Today’s Internet users are not the same users who were online a decade ago. There are better connections.',
Expand Down Expand Up @@ -1069,7 +1066,6 @@ public function testFileGetContents()
// ---

if (UTF8::mbstring_loaded()) { // only with "mbstring"

static::assertTrue(UTF8::is_binary_file(__DIR__ . '/fixtures/utf-16-be.txt'));
$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
static::assertContains(
Expand All @@ -1096,7 +1092,6 @@ public function testFileGetContents()
static::assertContains('Iñtërnâtiônàlizætiøn', $testString);

if (UTF8::mbstring_loaded()) { // only with "mbstring"

$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/utf-16-be.txt');
static::assertContains(
'<p>Today’s Internet users are not the same users who were online a decade ago. There are better connections.',
Expand Down Expand Up @@ -1354,7 +1349,6 @@ public function testFixSimpleUtf8()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -2609,7 +2603,6 @@ public function testNormalizeWhitespace()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -2651,7 +2644,6 @@ public function testOrd()
];

for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3131,7 +3123,6 @@ public function testSplit()
{
$oldSupportArray = null;
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3181,7 +3172,6 @@ public function testStrDetectEncoding()
];

for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -3372,9 +3362,9 @@ public function testStrPad()
static::assertSame('___中文空白___', UTF8::str_pad('中文空白', 10, '_', \STR_PAD_BOTH));

$toPad = '<IñtërnëT>'; // 10 characters
$padding = 'ø__'; // 4 characters
$padding = 'ø__'; // 4 characters

static::assertSame($toPad . ' ', UTF8::str_pad($toPad, 20));
static::assertSame($toPad . ' ', UTF8::str_pad($toPad, 20));
static::assertSame(' ' . $toPad, UTF8::str_pad($toPad, 20, ' ', \STR_PAD_LEFT));
static::assertSame(' ' . $toPad . ' ', UTF8::str_pad($toPad, 20, ' ', \STR_PAD_BOTH));

Expand Down Expand Up @@ -3861,7 +3851,6 @@ public function testStripWhitespace($expected, $str)
public function testStripos()
{
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down
3 changes: 0 additions & 3 deletions tests/Utf8GlobalPart2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function testStrlen()
];

for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -282,7 +281,6 @@ public function testStrposInByte()
public function testStrpos()
{
for ($i = 0; $i <= 2; ++$i) { // keep this loop for simple performance tests

if ($i === 0) {
$this->disableNativeUtf8Support();
} elseif ($i > 0) {
Expand Down Expand Up @@ -346,7 +344,6 @@ public function testStrpos()
// --- invalid UTF-8

if (UTF8::getSupportInfo('mbstring') === true) { // only with "mbstring"

static::assertSame(15, UTF8::strpos('ABC-ÖÄÜ-💩-' . "\xc3\x28" . '中文空白-中文空白' . "\xf0\x28\x8c\x28" . 'abc', ''));

if (Bootup::is_php('7.1')) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Utf8StrPadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ final class Utf8StrPadTest extends \PHPUnit\Framework\TestCase
public function testStrPad()
{
$toPad = '<IñtërnëT>'; // 10 characters
$padding = 'ø__'; // 4 characters
$padding = 'ø__'; // 4 characters

static::assertSame($toPad . ' ', u::str_pad($toPad, 20));
static::assertSame($toPad . ' ', u::str_pad($toPad, 20));
static::assertSame(' ' . $toPad, u::str_pad($toPad, 20, ' ', \STR_PAD_LEFT));
static::assertSame(' ' . $toPad . ' ', u::str_pad($toPad, 20, ' ', \STR_PAD_BOTH));

Expand Down
1 change: 0 additions & 1 deletion tests/Utf8SubstrReplaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function testEmptyString()
public function testNegative()
{
for ($i = 0; $i < 2; ++$i) { // keep this loop for simple performance tests

$str = 'testing';
$replaced = \substr_replace($str, 'foo', 0, -2);
static::assertSame($replaced, u::substr_replace($str, 'foo', 0, -2));
Expand Down
1 change: 0 additions & 1 deletion tests/Utf8ToAsciiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function testToASCII()
{
$testsStrict = [];
if (UTF8::intl_loaded()) {

// ---

$testString = UTF8::file_get_contents(__DIR__ . '/fixtures/sample-unicode-chart.txt');
Expand Down

0 comments on commit 6ee5a2a

Please sign in to comment.