diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/BehatPHPUnitAssertToWebmozartRectorTest.php b/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/BehatPHPUnitAssertToWebmozartRectorTest.php deleted file mode 100644 index befb33dd..00000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/BehatPHPUnitAssertToWebmozartRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/Fixture/skip_phpunit_context.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/Fixture/skip_phpunit_context.php.inc deleted file mode 100644 index 39a22f16..00000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/Fixture/skip_phpunit_context.php.inc +++ /dev/null @@ -1,16 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/config/configured_rule.php deleted file mode 100644 index b7301f53..00000000 --- a/rules-tests/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([BehatPHPUnitAssertToWebmozartRector::class]); diff --git a/rules/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector.php b/rules/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector.php index b04b3312..4e72cd99 100644 --- a/rules/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector.php +++ b/rules/CodeQuality/Rector/ClassMethod/BehatPHPUnitAssertToWebmozartRector.php @@ -5,123 +5,20 @@ namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; use PhpParser\Node; -use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Identifier; -use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\ClassMethod; -use PHPStan\Reflection\ReflectionProvider; -use Rector\PHPStan\ScopeFetcher; -use Rector\PHPUnit\Enum\BehatClassName; -use Rector\PHPUnit\Enum\PHPUnitClassName; -use Rector\PHPUnit\Enum\WebmozartClassName; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\BehatPHPUnitAssertToWebmozartRector\BehatPHPUnitAssertToWebmozartRectorTest + * @deprecated This rule is deprecated as too narrow, it only targets Behat context files. Implement it as a custom rule instead. */ -final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector +final class BehatPHPUnitAssertToWebmozartRector extends AbstractRector implements DeprecatedInterface { - /** - * @var array - */ - private const array PHPUNIT_TO_WEBMOZART_METHODS = [ - // Boolean - 'assertTrue' => 'true', - 'assertFalse' => 'false', - - // Null / empty - 'assertNull' => 'null', - 'assertNotNull' => 'notNull', - 'assertEmpty' => 'isEmpty', - 'assertNotEmpty' => 'notEmpty', - - // Type checks - 'assertIsString' => 'string', - 'assertIsInt' => 'integer', - 'assertIsFloat' => 'float', - 'assertIsBool' => 'boolean', - 'assertIsArray' => 'isArray', - 'assertIsObject' => 'object', - 'assertIsCallable' => 'isCallable', - 'assertIsResource' => 'resource', - 'assertIsIterable' => 'isIterable', - 'assertInstanceOf' => 'isInstanceOf', - - // array - 'assertContains' => 'oneOf', - 'assertNotContains' => 'notOneOf', - - // Comparison / equality - 'assertSame' => 'same', - 'assertNotSame' => 'notSame', - 'assertEquals' => 'eq', - 'assertNotEquals' => 'notEq', - 'assertGreaterThan' => 'greaterThan', - 'assertGreaterThanOrEqual' => 'greaterThanEq', - 'assertLessThan' => 'lessThan', - 'assertLessThanOrEqual' => 'lessThanEq', - - // Strings - 'assertStringContainsString' => 'contains', - 'assertStringNotContainsString' => 'notContains', - 'assertStringStartsWith' => 'startsWith', - 'assertStringStartsNotWith' => 'notStartsWith', - 'assertStringEndsWith' => 'endsWith', - 'assertStringEndsNotWith' => 'notEndsWith', - 'assertMatchesRegularExpression' => 'regex', - 'assertDoesNotMatchRegularExpression' => 'notRegex', - - // Bool - 'assertNotTrue' => 'false', - 'assertNotFalse' => 'true', - - // Arrays / count - 'assertCount' => 'count', - 'assertArrayHasKey' => 'keyExists', - 'assertArrayNotHasKey' => 'keyNotExists', - - // Misc / less direct - 'assertFileExists' => 'fileExists', - 'assertFileIsReadable' => 'readable', - 'assertDirectoryExists' => 'directory', - - // Instance of - 'assertNotInstanceOf' => 'notInstanceOf', - ]; - - /** - * @var string[] - */ - private const array FLIPPED_ARGS = [ - 'assertSame', - 'assertNotSame', - 'assertEquals', - 'assertNotEquals', - 'assertGreaterThan', - 'assertGreaterThanOrEqual', - 'assertLessThan', - 'assertLessThanOrEqual', - 'assertCount', - 'assertInstanceOf', - 'assertNotInstanceOf', - 'assertArrayHasKey', - 'assertArrayNotHasKey', - 'assertStringContainsString', - 'assertStringStartsWith', - 'assertMatchesRegularExpression', - 'assertDoesNotMatchRegularExpression', - ]; - - public function __construct( - private readonly ReflectionProvider $reflectionProvider - ) { - } - public function getRuleDefinition(): RuleDefinition { - return new RuleDefinition( 'Change PHPUnit assert in Behat context files to Webmozart Assert, as first require a TestCase instance', [ @@ -152,13 +49,12 @@ public function someMethod() } CODE_SAMPLE ), - ] ); } /** - * @return array + * @return array> */ public function getNodeTypes(): array { @@ -168,63 +64,11 @@ public function getNodeTypes(): array /** * @param ClassMethod $node */ - public function refactor(Node $node): ?ClassMethod + public function refactor(Node $node): ?Node { - $scope = ScopeFetcher::fetch($node); - if (! $scope->isInClass()) { - return null; - } - - $classReflection = $scope->getClassReflection(); - if (! $classReflection->is(BehatClassName::CONTEXT)) { - return null; - } - - if (! $this->reflectionProvider->hasClass(WebmozartClassName::ASSERT)) { - return null; - } - - $hasChanged = false; - - $this->traverseNodesWithCallable($node, function (Node $node) use (&$hasChanged): ?StaticCall { - if (! $node instanceof StaticCall) { - return null; - } - - if (! $this->isName($node->class, PHPUnitClassName::ASSERT)) { - return null; - } - - $phpunitMethodName = $this->getName($node->name); - if ($phpunitMethodName === null) { - return null; - } - - // changed method name - $webmozartMethodName = self::PHPUNIT_TO_WEBMOZART_METHODS[$phpunitMethodName] ?? null; - if ($webmozartMethodName === null) { - return null; - } - - if (in_array($phpunitMethodName, self::FLIPPED_ARGS, true) && count($node->args) >= 2) { - // flip first 2 args - $temp = $node->args[0]; - $node->args[0] = $node->args[1]; - $node->args[1] = $temp; - } - - $node->class = new FullyQualified(WebmozartClassName::ASSERT); - $node->name = new Identifier($webmozartMethodName); - - $hasChanged = true; - - return $node; - }); - - if (! $hasChanged) { - return null; - } - - return $node; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated as too narrow. Implement it as a custom rule instead.', + self::class, + )); } } diff --git a/src/Enum/WebmozartClassName.php b/src/Enum/WebmozartClassName.php deleted file mode 100644 index 48cd9799..00000000 --- a/src/Enum/WebmozartClassName.php +++ /dev/null @@ -1,10 +0,0 @@ -