Skip to content

Commit 80091f9

Browse files
committed
TestMethodsHelper - speed-up by asking for immediate methods only
1 parent 5e30669 commit 80091f9

File tree

3 files changed

+8
-27
lines changed

3 files changed

+8
-27
lines changed

src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPUnit\Framework\TestCase;
1111
use function count;
1212
use function is_numeric;
13-
use function method_exists;
1413
use function sprintf;
1514

1615
/**
@@ -56,13 +55,8 @@ public function processNode(Node $node, Scope $scope): array
5655
return [];
5756
}
5857

59-
/** @phpstan-ignore function.alreadyNarrowedType */
60-
if (!method_exists($reflectionMethod, 'getAttributes')) {
61-
return [];
62-
}
63-
6458
$errors = [];
65-
foreach ($reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) {
59+
foreach ($reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp') as $attr) {
6660
$args = $attr->getArguments();
6761
if (count($args) !== 1) {
6862
continue;

src/Rules/PHPUnit/DataProviderHelper.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PhpParser\NodeFinder;
1313
use PHPStan\Analyser\Scope;
14+
use PHPStan\BetterReflection\Reflection\ReflectionMethod;
1415
use PHPStan\Parser\Parser;
1516
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
1617
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
@@ -20,11 +21,9 @@
2021
use PHPStan\Rules\IdentifierRuleError;
2122
use PHPStan\Rules\RuleErrorBuilder;
2223
use PHPStan\Type\FileTypeMapper;
23-
use ReflectionMethod;
2424
use function array_merge;
2525
use function count;
2626
use function explode;
27-
use function method_exists;
2827
use function preg_match;
2928
use function sprintf;
3029

@@ -282,21 +281,13 @@ private function yieldDataProviderAttributes($node, ClassReflection $classReflec
282281
if (
283282
$node instanceof ReflectionMethod
284283
) {
285-
/** @phpstan-ignore function.alreadyNarrowedType */
286-
if (!method_exists($node, 'getAttributes')) {
287-
return;
288-
}
289-
290-
foreach ($node->getAttributes('PHPUnit\Framework\Attributes\DataProvider') as $attr) {
284+
foreach ($node->getAttributesByName('PHPUnit\Framework\Attributes\DataProvider') as $attr) {
291285
$args = $attr->getArguments();
292286
if (count($args) !== 1) {
293287
continue;
294288
}
295289

296290
$startLine = $node->getStartLine();
297-
if ($startLine === false) {
298-
$startLine = -1;
299-
}
300291

301292
yield [$classReflection, $args[0], $startLine];
302293
}
@@ -329,7 +320,7 @@ private function yieldDataProviderAttributes($node, ClassReflection $classReflec
329320
private function yieldDataProviderAnnotations($node, Scope $scope, ClassReflection $classReflection): iterable
330321
{
331322
$docComment = $node->getDocComment();
332-
if ($docComment === null || $docComment === false) {
323+
if ($docComment === null) {
333324
return;
334325
}
335326

@@ -348,10 +339,6 @@ private function yieldDataProviderAnnotations($node, Scope $scope, ClassReflecti
348339
}
349340

350341
$startLine = $node->getStartLine();
351-
if ($startLine === false) {
352-
$startLine = -1;
353-
}
354-
355342
$dataProviderMethod = $this->parseDataProviderAnnotationValue($scope, $dataProviderValue);
356343
$dataProviderMethod[] = $startLine;
357344

src/Rules/PHPUnit/TestMethodsHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace PHPStan\Rules\PHPUnit;
44

55
use PHPStan\Analyser\Scope;
6+
use PHPStan\BetterReflection\Reflection\ReflectionMethod;
67
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
78
use PHPStan\Reflection\ClassReflection;
89
use PHPStan\Reflection\MethodReflection;
910
use PHPStan\Type\FileTypeMapper;
1011
use PHPUnit\Framework\TestCase;
11-
use ReflectionMethod;
1212
use function str_starts_with;
1313
use function strtolower;
1414

@@ -49,7 +49,7 @@ public function getTestMethods(ClassReflection $classReflection, Scope $scope):
4949
}
5050

5151
$testMethods = [];
52-
foreach ($classReflection->getNativeReflection()->getMethods() as $reflectionMethod) {
52+
foreach ($classReflection->getNativeReflection()->getBetterReflection()->getImmediateMethods() as $reflectionMethod) {
5353
if (!$reflectionMethod->isPublic()) {
5454
continue;
5555
}
@@ -60,7 +60,7 @@ public function getTestMethods(ClassReflection $classReflection, Scope $scope):
6060
}
6161

6262
$docComment = $reflectionMethod->getDocComment();
63-
if ($docComment !== false) {
63+
if ($docComment !== null) {
6464
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
6565
$scope->getFile(),
6666
$classReflection->getName(),
@@ -79,7 +79,7 @@ public function getTestMethods(ClassReflection $classReflection, Scope $scope):
7979
continue;
8080
}
8181

82-
$testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\Test'); // @phpstan-ignore argument.type
82+
$testAttributes = $reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\Test'); // @phpstan-ignore argument.type
8383
if ($testAttributes === []) {
8484
continue;
8585
}

0 commit comments

Comments
 (0)