Skip to content

Commit 662ce7c

Browse files
Merge branch '10.5' into 11.0
2 parents e0ddfd2 + 729330a commit 662ce7c

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
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>

ChangeLog-11.0.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes of the PHPUnit 11.0 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [11.0.7] - 2024-MM-DD
6+
7+
### Changed
8+
9+
* [#5747](https://github.com/sebastianbergmann/phpunit/pull/5747): Cache result of `Groups::groups()`
10+
* [#5748](https://github.com/sebastianbergmann/phpunit/pull/5748): Improve performance of `NamePrettifier::prettifyTestMethodName()`
11+
512
## [11.0.6] - 2024-03-12
613

714
### Changed
@@ -130,6 +137,7 @@ All notable changes of the PHPUnit 11.0 release series are documented in this fi
130137
* `PHPUnit\TextUI\Configuration\Configuration::registerMockObjectsFromTestArgumentsRecursively()`
131138
* `PHPUnit\Framework\Constraint\Constraint::exporter()`
132139

140+
[11.0.7]: https://github.com/sebastianbergmann/phpunit/compare/11.0.6...11.0
133141
[11.0.6]: https://github.com/sebastianbergmann/phpunit/compare/11.0.5...11.0.6
134142
[11.0.5]: https://github.com/sebastianbergmann/phpunit/compare/11.0.4...11.0.5
135143
[11.0.4]: https://github.com/sebastianbergmann/phpunit/compare/11.0.3...11.0.4

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: 15 additions & 3 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;
@@ -29,14 +30,25 @@
2930
*/
3031
final readonly 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)