Skip to content

Commit f279499

Browse files
authored
fix assert call type resolving (#675)
1 parent 29d203f commit f279499

5 files changed

Lines changed: 41 additions & 10 deletions

File tree

config/sets/phpunit90.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\AssertRegExpRector;
76
use Rector\PHPUnit\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector;
87
use Rector\PHPUnit\PHPUnit90\Rector\Class_\TestListenerToHooksRector;
8+
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\AssertRegExpRector;
99
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ExplicitPhpErrorApiRector;
1010
use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector;
1111
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
@@ -28,13 +28,8 @@
2828
'expectExceptionMessageMatches'
2929
),
3030

31-
3231
// @see https://github.com/sebastianbergmann/phpunit/issues/4086
33-
new MethodCallRename(
34-
'PHPUnit\Framework\TestCase',
35-
'assertRegExp',
36-
'assertMatchesRegularExpression'
37-
),
32+
new MethodCallRename('PHPUnit\Framework\TestCase', 'assertRegExp', 'assertMatchesRegularExpression'),
3833

3934
// @see https://github.com/sebastianbergmann/phpunit/issues/4089
4035
new MethodCallRename(
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
// skip: non-string argument (enum case) should not crash
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AssertIsTypeMethodCallRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class SkipNonPHPUnitType extends TestCase
10+
{
11+
public function testMethod(\stdClass $else): void
12+
{
13+
$else->isType('string');
14+
$else->isType('array');
15+
$else->isType();
16+
}
17+
}

rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;
66

7+
use function in_array;
78
use PhpParser\Comment\Doc;
89
use PhpParser\Node;
910
use PhpParser\Node\Stmt\ClassMethod;
@@ -15,7 +16,6 @@
1516
use Rector\Util\NewLineSplitter;
1617
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1718
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18-
use function in_array;
1919

2020
/**
2121
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest
@@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node
8787
}
8888

8989
$hasAnnotation = false;
90-
foreach(NewLineSplitter::split($docComment->getText()) as $row) {
90+
foreach (NewLineSplitter::split($docComment->getText()) as $row) {
9191
if (in_array(trim($row), ['*@test', '* @test'], true)) {
9292
$hasAnnotation = true;
9393
break;

rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
final class AssertIsTypeMethodCallRector extends AbstractRector
2424
{
25+
/**
26+
* @var array<string, string>
27+
*/
2528
private const array IS_TYPE_VALUE_TO_METHOD = [
2629
'array' => 'isArray',
2730
'bool' => 'isBool',

src/NodeAnalyzer/TestsNodeAnalyzer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use PHPStan\Reflection\ClassReflection;
1212
use PHPStan\Type\ObjectType;
13+
use PHPStan\Type\StaticType;
1314
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1415
use Rector\NodeNameResolver\NodeNameResolver;
1516
use Rector\NodeTypeResolver\NodeTypeResolver;
@@ -101,7 +102,22 @@ public function isPHPUnitMethodCallNames(Node $node, array $names): bool
101102
public function isPHPUnitTestCaseCall(Node $node): bool
102103
{
103104
if ($node instanceof MethodCall) {
104-
return $this->isInTestClass($node);
105+
$callerType = $this->nodeTypeResolver->getType($node->var);
106+
if ($callerType instanceof StaticType) {
107+
$callerType = $callerType->getStaticObjectType();
108+
}
109+
110+
if ($callerType instanceof ObjectType) {
111+
if ($callerType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) {
112+
return true;
113+
}
114+
115+
if ($callerType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) {
116+
return true;
117+
}
118+
}
119+
120+
return false;
105121
}
106122

107123
if ($node instanceof StaticCall) {

0 commit comments

Comments
 (0)