Skip to content

Commit c11dd51

Browse files
committed
Update benches to pass Class_ nodes, fetch scope after abstract guard
1 parent 5eb614e commit c11dd51

4 files changed

Lines changed: 27 additions & 56 deletions

‎bench/does_not_perform_assertion_abstract_class_bench.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
/**
66
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on abstract and final test classes.
77
*
8-
* Half of the test classes are abstract, so the abstract class check runs on every test method.
8+
* Half of the test classes are abstract, so the abstract class check runs on every test class.
99
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1010
*/
1111

12-
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Class_;
1313
use Rector\DependencyInjection\LazyContainerFactory;
1414
use Rector\PhpParser\Node\BetterNodeFinder;
1515
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
@@ -68,10 +68,10 @@
6868

6969
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
7070

71-
$classMethods = [];
71+
$classes = [];
7272
foreach ($testFilePaths as $testFilePath) {
7373
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
74-
$classMethods = [...$classMethods, ...$betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
74+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
7575
}
7676

7777
$cpuSeconds = static function (): float {
@@ -84,8 +84,8 @@
8484
$cpuStart = $cpuSeconds();
8585

8686
$changedCount = 0;
87-
foreach ($classMethods as $classMethod) {
88-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
87+
foreach ($classes as $class) {
88+
if ($rector->refactor($class) instanceof Class_) {
8989
++$changedCount;
9090
}
9191
}
@@ -96,8 +96,8 @@
9696
array_map('unlink', glob($directory . '/*.php'));
9797
@rmdir($directory);
9898

99-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), count($classMethods), $changedCount, PHP_EOL);
100-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / count($classMethods) * 1000, PHP_EOL);
99+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
100+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
101101
printf('wall: %.4f s%s', $wall, PHP_EOL);
102102

103103
if ($cpu > MAX_CPU_SECONDS) {

‎bench/does_not_perform_assertion_lifecycle_bench.php‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1212
*/
1313

14-
use PhpParser\Node\Stmt\ClassMethod;
15-
use Rector\Application\Provider\CurrentFileProvider;
14+
use PhpParser\Node\Stmt\Class_;
1615
use Rector\DependencyInjection\LazyContainerFactory;
1716
use Rector\PhpParser\Node\BetterNodeFinder;
1817
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
1918
use Rector\Testing\TestingParser\TestingParser;
20-
use Rector\ValueObject\Application\File;
2119

2220
require __DIR__ . '/../vendor/autoload.php';
2321

@@ -100,17 +98,11 @@
10098

10199
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
102100

103-
/** @var CurrentFileProvider $currentFileProvider */
104-
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
105-
106101
// parse test files up front, parent files are left for the rule to resolve
107-
// the rule finds the class of a method in the stmts of the current file, as in a real run
108-
$classMethodsByFile = [];
102+
$classes = [];
109103
foreach ($testFilePaths as $testFilePath) {
110104
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
111-
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
112-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
113-
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
105+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
114106
}
115107

116108
$cpuSeconds = static function (): float {
@@ -123,15 +115,9 @@
123115
$cpuStart = $cpuSeconds();
124116

125117
$changedCount = 0;
126-
$classMethodCount = 0;
127-
foreach ($classMethodsByFile as [$file, $classMethods]) {
128-
$currentFileProvider->setFile($file);
129-
130-
foreach ($classMethods as $classMethod) {
131-
++$classMethodCount;
132-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
133-
++$changedCount;
134-
}
118+
foreach ($classes as $class) {
119+
if ($rector->refactor($class) instanceof Class_) {
120+
++$changedCount;
135121
}
136122
}
137123

@@ -141,8 +127,8 @@
141127
array_map('unlink', glob($directory . '/*.php'));
142128
@rmdir($directory);
143129

144-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
145-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
130+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
131+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
146132
printf('wall: %.4f s%s', $wall, PHP_EOL);
147133

148134
if ($cpu > MAX_CPU_SECONDS) {

‎bench/does_not_perform_assertion_own_lifecycle_bench.php‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
/**
66
* Benchmark for AddDoesNotPerformAssertionToNonAssertingTestRector on test classes with own lifecycle methods.
77
*
8-
* Every test class has its own setUp() and tearDown(), every other one asserts in setUp(). The class is taken from
9-
* the current file, so the rule looks into its lifecycle methods for every test method.
8+
* Every test class has its own setUp() and tearDown(), every other one asserts in setUp().
109
* Run on both branches to compare. CPU (user+sys) is the reliable metric; wall is noisy.
1110
*/
1211

13-
use PhpParser\Node\Stmt\ClassMethod;
14-
use Rector\Application\Provider\CurrentFileProvider;
12+
use PhpParser\Node\Stmt\Class_;
1513
use Rector\DependencyInjection\LazyContainerFactory;
1614
use Rector\PhpParser\Node\BetterNodeFinder;
1715
use Rector\PHPUnit\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector;
1816
use Rector\Testing\TestingParser\TestingParser;
19-
use Rector\ValueObject\Application\File;
2017

2118
require __DIR__ . '/../vendor/autoload.php';
2219

@@ -72,16 +69,10 @@
7269

7370
$rector = $rectorConfig->make(AddDoesNotPerformAssertionToNonAssertingTestRector::class);
7471

75-
/** @var CurrentFileProvider $currentFileProvider */
76-
$currentFileProvider = $rectorConfig->make(CurrentFileProvider::class);
77-
78-
// the rule finds the class of a method in the stmts of the current file, as in a real run
79-
$classMethodsByFile = [];
72+
$classes = [];
8073
foreach ($testFilePaths as $testFilePath) {
8174
$stmts = $testingParser->parseFileToDecoratedNodes($testFilePath);
82-
$file = new File($testFilePath, (string) file_get_contents($testFilePath));
83-
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
84-
$classMethodsByFile[] = [$file, $betterNodeFinder->findInstanceOf($stmts, ClassMethod::class)];
75+
$classes = [...$classes, ...$betterNodeFinder->findInstanceOf($stmts, Class_::class)];
8576
}
8677

8778
$cpuSeconds = static function (): float {
@@ -94,15 +85,9 @@
9485
$cpuStart = $cpuSeconds();
9586

9687
$changedCount = 0;
97-
$classMethodCount = 0;
98-
foreach ($classMethodsByFile as [$file, $classMethods]) {
99-
$currentFileProvider->setFile($file);
100-
101-
foreach ($classMethods as $classMethod) {
102-
++$classMethodCount;
103-
if ($rector->refactor($classMethod) instanceof ClassMethod) {
104-
++$changedCount;
105-
}
88+
foreach ($classes as $class) {
89+
if ($rector->refactor($class) instanceof Class_) {
90+
++$changedCount;
10691
}
10792
}
10893

@@ -112,8 +97,8 @@
11297
array_map('unlink', glob($directory . '/*.php'));
11398
@rmdir($directory);
11499

115-
printf('test classes: %d, test methods: %d, changed: %d%s', count($testFilePaths), $classMethodCount, $changedCount, PHP_EOL);
116-
printf('CPU: %.4f s (%.3f ms/method)%s', $cpu, $cpu / $classMethodCount * 1000, PHP_EOL);
100+
printf('test classes: %d, changed: %d%s', count($classes), $changedCount, PHP_EOL);
101+
printf('CPU: %.4f s (%.3f ms/class)%s', $cpu, $cpu / count($classes) * 1000, PHP_EOL);
117102
printf('wall: %.4f s%s', $wall, PHP_EOL);
118103

119104
if ($cpu > MAX_CPU_SECONDS) {

‎rules/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ public function refactor(Node $node): ?Node
115115
return null;
116116
}
117117

118-
$scope = ScopeFetcher::fetch($node);
119-
120118
// a child class can add asserting lifecycle methods, that count toward the inherited tests
121119
if ($node->isAbstract()) {
122120
return null;
123121
}
124122

123+
$scope = ScopeFetcher::fetch($node);
124+
125125
// the parent test case asserts in its own integration methods
126126
if ($this->isInTwigIntegrationTestCase($scope)) {
127127
return null;

0 commit comments

Comments
 (0)