Skip to content

Commit ddc34b0

Browse files
Merge branch '11.0'
2 parents fe9f3b1 + 106fdd2 commit ddc34b0

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

.psalm/baseline.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,6 @@
475475
</RedundantCondition>
476476
</file>
477477
<file src="src/Metadata/Api/Groups.php">
478-
<LessSpecificReturnStatement>
479-
<code><![CDATA[array_unique($groups)]]></code>
480-
<code><![CDATA[array_unique($groups)]]></code>
481-
</LessSpecificReturnStatement>
482-
<MoreSpecificReturnType>
483-
<code><![CDATA[list<string>]]></code>
484-
</MoreSpecificReturnType>
485478
<RedundantCondition>
486479
<code><![CDATA[$metadata instanceof CoversFunction]]></code>
487480
<code><![CDATA[$metadata instanceof UsesFunction]]></code>

src/Logging/TestDox/NamePrettifier.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
use function explode;
2020
use function gettype;
2121
use function implode;
22-
use function in_array;
2322
use function is_bool;
2423
use function is_float;
2524
use function is_int;
26-
use function is_numeric;
2725
use function is_object;
2826
use function is_scalar;
2927
use function method_exists;
@@ -55,7 +53,7 @@
5553
final class NamePrettifier
5654
{
5755
/**
58-
* @psalm-var list<string>
56+
* @psalm-var array<string, int>
5957
*/
6058
private static array $strings = [];
6159

@@ -109,20 +107,19 @@ public function prettifyTestClassName(string $className): string
109107
return $result;
110108
}
111109

110+
// NOTE: this method is on a hot path and very performance sensitive. change with care.
112111
public function prettifyTestMethodName(string $name): string
113112
{
114-
$buffer = '';
115-
116113
if ($name === '') {
117-
return $buffer;
114+
return '';
118115
}
119116

120117
$string = (string) preg_replace('#\d+$#', '', $name, -1, $count);
121118

122-
if (in_array($string, self::$strings, true)) {
119+
if (array_key_exists($string, self::$strings)) {
123120
$name = $string;
124121
} elseif ($count === 0) {
125-
self::$strings[] = $string;
122+
self::$strings[$string] = 1;
126123
}
127124

128125
if (str_starts_with($name, 'test_')) {
@@ -132,22 +129,26 @@ public function prettifyTestMethodName(string $name): string
132129
}
133130

134131
if ($name === '') {
135-
return $buffer;
132+
return '';
136133
}
137134

138135
$name[0] = strtoupper($name[0]);
139136

140-
if (str_contains($name, '_')) {
141-
return trim(str_replace('_', ' ', $name));
137+
$noUnderscore = str_replace('_', ' ', $name);
138+
139+
if ($noUnderscore !== $name) {
140+
return trim($noUnderscore);
142141
}
143142

144143
$wasNumeric = false;
145144

145+
$buffer = '';
146+
146147
foreach (range(0, strlen($name) - 1) as $i) {
147148
if ($i > 0 && $name[$i] >= 'A' && $name[$i] <= 'Z') {
148149
$buffer .= ' ' . strtolower($name[$i]);
149150
} else {
150-
$isNumeric = is_numeric($name[$i]);
151+
$isNumeric = $name[$i] >= '0' && $name[$i] <= '9';
151152

152153
if (!$wasNumeric && $isNumeric) {
153154
$buffer .= ' ';

src/Metadata/Api/Groups.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPUnit\Metadata\Api;
1111

1212
use function array_flip;
13+
use function array_key_exists;
1314
use function array_unique;
1415
use function assert;
1516
use function strtolower;
@@ -27,16 +28,27 @@
2728
/**
2829
* @internal This class is not covered by the backward compatibility promise for PHPUnit
2930
*/
30-
final readonly class Groups
31+
final class Groups
3132
{
33+
/**
34+
* @var array<string, array<int, string>>
35+
*/
36+
private static array $groupCache = [];
37+
3238
/**
3339
* @psalm-param class-string $className
3440
* @psalm-param non-empty-string $methodName
3541
*
36-
* @psalm-return list<string>
42+
* @psalm-return array<int, string>
3743
*/
3844
public function groups(string $className, string $methodName, bool $includeVirtual = true): array
3945
{
46+
$key = $className . '::' . $methodName . '::' . $includeVirtual;
47+
48+
if (array_key_exists($key, self::$groupCache)) {
49+
return self::$groupCache[$key];
50+
}
51+
4052
$groups = [];
4153

4254
foreach (Registry::parser()->forClassAndMethod($className, $methodName)->isGroup() as $group) {
@@ -50,7 +62,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu
5062
}
5163

5264
if (!$includeVirtual) {
53-
return array_unique($groups);
65+
return self::$groupCache[$key] = array_unique($groups);
5466
}
5567

5668
foreach (Registry::parser()->forClassAndMethod($className, $methodName) as $metadata) {
@@ -85,7 +97,7 @@ public function groups(string $className, string $methodName, bool $includeVirtu
8597
}
8698
}
8799

88-
return array_unique($groups);
100+
return self::$groupCache[$key] = array_unique($groups);
89101
}
90102

91103
/**

tests/unit/Logging/TestDox/NamePrettifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testTitleHasSensibleDefaults(): void
3333

3434
public function testTestNameIsConvertedToASentence(): void
3535
{
36+
$this->assertEquals('', (new NamePrettifier)->prettifyTestMethodName(''));
3637
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest'));
3738
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('testThisIsATest2'));
3839
$this->assertEquals('This is a test', (new NamePrettifier)->prettifyTestMethodName('this_is_a_test'));

0 commit comments

Comments
 (0)