diff --git a/config/sets/phpunit90.php b/config/sets/phpunit90.php index 34e15137..5cbaf8ff 100644 --- a/config/sets/phpunit90.php +++ b/config/sets/phpunit90.php @@ -3,9 +3,9 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\AssertRegExpRector; use Rector\PHPUnit\PHPUnit100\Rector\StmtsAwareInterface\WithConsecutiveRector; use Rector\PHPUnit\PHPUnit90\Rector\Class_\TestListenerToHooksRector; +use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\AssertRegExpRector; use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\ExplicitPhpErrorApiRector; use Rector\PHPUnit\PHPUnit90\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector; use Rector\Renaming\Rector\MethodCall\RenameMethodRector; @@ -28,13 +28,8 @@ 'expectExceptionMessageMatches' ), - // @see https://github.com/sebastianbergmann/phpunit/issues/4086 - new MethodCallRename( - 'PHPUnit\Framework\TestCase', - 'assertRegExp', - 'assertMatchesRegularExpression' - ), + new MethodCallRename('PHPUnit\Framework\TestCase', 'assertRegExp', 'assertMatchesRegularExpression'), // @see https://github.com/sebastianbergmann/phpunit/issues/4089 new MethodCallRename( diff --git a/rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/Fixture/skip_non_phpunit_type.php.inc b/rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/Fixture/skip_non_phpunit_type.php.inc new file mode 100644 index 00000000..37a0ec8b --- /dev/null +++ b/rules-tests/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector/Fixture/skip_non_phpunit_type.php.inc @@ -0,0 +1,17 @@ +isType('string'); + $else->isType('array'); + $else->isType(); + } +} diff --git a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php index 5b9e7ae0..c7e3d88e 100644 --- a/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php @@ -4,6 +4,7 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; +use function in_array; use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Stmt\ClassMethod; @@ -15,7 +16,6 @@ use Rector\Util\NewLineSplitter; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; -use function in_array; /** * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest @@ -87,7 +87,7 @@ public function refactor(Node $node): ?Node } $hasAnnotation = false; - foreach(NewLineSplitter::split($docComment->getText()) as $row) { + foreach (NewLineSplitter::split($docComment->getText()) as $row) { if (in_array(trim($row), ['*@test', '* @test'], true)) { $hasAnnotation = true; break; diff --git a/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php b/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php index d9bd9f59..528d7a74 100644 --- a/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php +++ b/rules/PHPUnit120/Rector/Class_/AssertIsTypeMethodCallRector.php @@ -22,6 +22,9 @@ */ final class AssertIsTypeMethodCallRector extends AbstractRector { + /** + * @var array + */ private const array IS_TYPE_VALUE_TO_METHOD = [ 'array' => 'isArray', 'bool' => 'isBool', diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index 7b0c7c1b..493f5878 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ClassReflection; use PHPStan\Type\ObjectType; +use PHPStan\Type\StaticType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\NodeTypeResolver; @@ -101,7 +102,22 @@ public function isPHPUnitMethodCallNames(Node $node, array $names): bool public function isPHPUnitTestCaseCall(Node $node): bool { if ($node instanceof MethodCall) { - return $this->isInTestClass($node); + $callerType = $this->nodeTypeResolver->getType($node->var); + if ($callerType instanceof StaticType) { + $callerType = $callerType->getStaticObjectType(); + } + + if ($callerType instanceof ObjectType) { + if ($callerType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) { + return true; + } + + if ($callerType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) { + return true; + } + } + + return false; } if ($node instanceof StaticCall) {