diff --git a/src/Logging/TestDox/NamePrettifier.php b/src/Logging/TestDox/NamePrettifier.php index 23426b2a810..861d02b434c 100644 --- a/src/Logging/TestDox/NamePrettifier.php +++ b/src/Logging/TestDox/NamePrettifier.php @@ -19,11 +19,9 @@ use function explode; use function gettype; use function implode; -use function in_array; use function is_bool; use function is_float; use function is_int; -use function is_numeric; use function is_object; use function is_scalar; use function method_exists; @@ -55,7 +53,7 @@ final class NamePrettifier { /** - * @psalm-var list + * @psalm-var array */ private static array $strings = []; @@ -109,20 +107,19 @@ public function prettifyTestClassName(string $className): string return $result; } + // NOTE: this method is on a hot path and very performance sensitive. change with care. public function prettifyTestMethodName(string $name): string { - $buffer = ''; - if ($name === '') { - return $buffer; + return ''; } $string = (string) preg_replace('#\d+$#', '', $name, -1, $count); - if (in_array($string, self::$strings, true)) { + if (array_key_exists($string, self::$strings)) { $name = $string; } elseif ($count === 0) { - self::$strings[] = $string; + self::$strings[$string] = 1; } if (str_starts_with($name, 'test_')) { @@ -132,22 +129,26 @@ public function prettifyTestMethodName(string $name): string } if ($name === '') { - return $buffer; + return ''; } $name[0] = strtoupper($name[0]); - if (str_contains($name, '_')) { - return trim(str_replace('_', ' ', $name)); + $noUnderscore = str_replace('_', ' ', $name); + + if ($noUnderscore !== $name) { + return trim($noUnderscore); } $wasNumeric = false; + $buffer = ''; + foreach (range(0, strlen($name) - 1) as $i) { if ($i > 0 && $name[$i] >= 'A' && $name[$i] <= 'Z') { $buffer .= ' ' . strtolower($name[$i]); } else { - $isNumeric = is_numeric($name[$i]); + $isNumeric = $name[$i] >= '0' && $name[$i] <= '9'; if (!$wasNumeric && $isNumeric) { $buffer .= ' '; diff --git a/tests/unit/Logging/TestDox/NamePrettifierTest.php b/tests/unit/Logging/TestDox/NamePrettifierTest.php index c6c2b637f80..19698421f08 100644 --- a/tests/unit/Logging/TestDox/NamePrettifierTest.php +++ b/tests/unit/Logging/TestDox/NamePrettifierTest.php @@ -33,6 +33,7 @@ public function testTitleHasSensibleDefaults(): void public function testTestNameIsConvertedToASentence(): void { + $this->assertEquals('', (new NamePrettifier)->prettifyTestMethodName('')); $this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest')); $this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest2')); $this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('this_is_a_test'));