From 6dde7c97cc67168ea121be872db46c0f99bc12db Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 13 Mar 2024 20:12:57 +0100 Subject: [PATCH 1/5] Faster `NamePrettifier->prettifyTestMethodName()` --- src/Logging/TestDox/NamePrettifier.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Logging/TestDox/NamePrettifier.php b/src/Logging/TestDox/NamePrettifier.php index 23426b2a810..1e5515884b6 100644 --- a/src/Logging/TestDox/NamePrettifier.php +++ b/src/Logging/TestDox/NamePrettifier.php @@ -23,7 +23,6 @@ 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; @@ -147,7 +146,7 @@ public function prettifyTestMethodName(string $name): string 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 .= ' '; From 6a18614f340a9087a5cbad7caeaa9df6c40c79eb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 13 Mar 2024 20:20:28 +0100 Subject: [PATCH 2/5] remove unnecessary str_contains() --- src/Logging/TestDox/NamePrettifier.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Logging/TestDox/NamePrettifier.php b/src/Logging/TestDox/NamePrettifier.php index 1e5515884b6..d0c894c0e07 100644 --- a/src/Logging/TestDox/NamePrettifier.php +++ b/src/Logging/TestDox/NamePrettifier.php @@ -108,6 +108,7 @@ 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 = ''; @@ -136,8 +137,9 @@ public function prettifyTestMethodName(string $name): string $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; From 4abeb3a0edbb8386ab01aa4df22a91adb9567c04 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 13 Mar 2024 20:23:25 +0100 Subject: [PATCH 3/5] simplify --- src/Logging/TestDox/NamePrettifier.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Logging/TestDox/NamePrettifier.php b/src/Logging/TestDox/NamePrettifier.php index d0c894c0e07..ec8391723b7 100644 --- a/src/Logging/TestDox/NamePrettifier.php +++ b/src/Logging/TestDox/NamePrettifier.php @@ -111,10 +111,8 @@ public function prettifyTestClassName(string $className): string // 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); @@ -132,7 +130,7 @@ public function prettifyTestMethodName(string $name): string } if ($name === '') { - return $buffer; + return ''; } $name[0] = strtoupper($name[0]); @@ -144,6 +142,7 @@ public function prettifyTestMethodName(string $name): string $wasNumeric = false; + $buffer = ''; foreach (range(0, strlen($name) - 1) as $i) { if ($i > 0 && $name[$i] >= 'A' && $name[$i] <= 'Z') { $buffer .= ' ' . strtolower($name[$i]); From 2deea9f77b1b7fff57cb59f6050865c6e81a93f7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 13 Mar 2024 20:30:23 +0100 Subject: [PATCH 4/5] use faster array_key_exists() --- src/Logging/TestDox/NamePrettifier.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Logging/TestDox/NamePrettifier.php b/src/Logging/TestDox/NamePrettifier.php index ec8391723b7..861d02b434c 100644 --- a/src/Logging/TestDox/NamePrettifier.php +++ b/src/Logging/TestDox/NamePrettifier.php @@ -19,7 +19,6 @@ 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; @@ -54,7 +53,7 @@ final class NamePrettifier { /** - * @psalm-var list + * @psalm-var array */ private static array $strings = []; @@ -117,10 +116,10 @@ public function prettifyTestMethodName(string $name): string $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_')) { @@ -136,6 +135,7 @@ public function prettifyTestMethodName(string $name): string $name[0] = strtoupper($name[0]); $noUnderscore = str_replace('_', ' ', $name); + if ($noUnderscore !== $name) { return trim($noUnderscore); } @@ -143,6 +143,7 @@ public function prettifyTestMethodName(string $name): string $wasNumeric = false; $buffer = ''; + foreach (range(0, strlen($name) - 1) as $i) { if ($i > 0 && $name[$i] >= 'A' && $name[$i] <= 'Z') { $buffer .= ' ' . strtolower($name[$i]); From ada59662a597c033b523f7eee7c6eb9a19cb68e5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 13 Mar 2024 20:44:38 +0100 Subject: [PATCH 5/5] added missing test coverage --- tests/unit/Logging/TestDox/NamePrettifierTest.php | 1 + 1 file changed, 1 insertion(+) 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'));