From e4bcd5aded814cca6e6d60d69ef9a5ec2a9b391e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 19 Mar 2025 20:07:52 +0100 Subject: [PATCH] [deprecated] Remove deprecated AssertCompareToSpecificMethodRector --- .../AssertCompareToSpecificMethodRector.php | 150 ------------------ 1 file changed, 150 deletions(-) delete mode 100644 rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php diff --git a/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php deleted file mode 100644 index de471cf0..00000000 --- a/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php +++ /dev/null @@ -1,150 +0,0 @@ -functionNamesWithAssertMethods = [ - new FunctionNameWithAssertMethods('count', self::ASSERT_COUNT, self::ASSERT_NOT_COUNT), - new FunctionNameWithAssertMethods('sizeof', self::ASSERT_COUNT, self::ASSERT_NOT_COUNT), - new FunctionNameWithAssertMethods('iterator_count', self::ASSERT_COUNT, self::ASSERT_NOT_COUNT), - new FunctionNameWithAssertMethods('get_class', 'assertInstanceOf', 'assertNotInstanceOf'), - ]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Turns vague php-only method in PHPUnit TestCase to more specific', - [ - new CodeSample( - '$this->assertSame(10, count($anything), "message");', - '$this->assertCount(10, $anything, "message");' - ), - new CodeSample( - '$this->assertNotEquals(SomeInstance::class, get_class($value));', - '$this->assertNotInstanceOf(SomeInstance::class, $value);' - ), - ] - ); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [MethodCall::class, StaticCall::class]; - } - - /** - * @param MethodCall|StaticCall $node - */ - public function refactor(Node $node): ?Node - { - if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames( - $node, - ['assertSame', 'assertNotSame', 'assertEquals', 'assertNotEquals'] - )) { - return null; - } - - if ($node->isFirstClassCallable()) { - return null; - } - - // we need 2 args - if (! isset($node->args[1])) { - return null; - } - - $firstArgument = $node->getArgs()[0]; - $secondArgument = $node->getArgs()[1]; - $secondArgumentValue = $secondArgument->value; - - if ($secondArgumentValue instanceof FuncCall) { - return $this->processFuncCallArgumentValue($node, $secondArgumentValue, $firstArgument); - } - - return null; - } - - /** - * @return MethodCall|StaticCall|null - */ - private function processFuncCallArgumentValue( - MethodCall|StaticCall $node, - FuncCall $funcCall, - Arg $requiredArg - ): ?Node { - foreach ($this->functionNamesWithAssertMethods as $functionNameWithAssertMethod) { - if (! $this->isName($funcCall, $functionNameWithAssertMethod->getFunctionName())) { - continue; - } - - $this->renameMethod($node, $functionNameWithAssertMethod); - $this->moveFunctionArgumentsUp($node, $funcCall, $requiredArg); - - return $node; - } - - return null; - } - - private function renameMethod( - MethodCall|StaticCall $node, - FunctionNameWithAssertMethods $functionNameWithAssertMethods - ): void { - if ($this->isNames($node->name, ['assertSame', 'assertEquals'])) { - $node->name = new Identifier($functionNameWithAssertMethods->getAssetMethodName()); - } elseif ($this->isNames($node->name, ['assertNotSame', 'assertNotEquals'])) { - $node->name = new Identifier($functionNameWithAssertMethods->getNotAssertMethodName()); - } - } - - /** - * Handles custom error messages to not be overwrite by function with multiple args. - */ - private function moveFunctionArgumentsUp(StaticCall|MethodCall $node, FuncCall $funcCall, Arg $requiredArg): void - { - $node->args[1] = $funcCall->getArgs()[0]; - $node->args[0] = $requiredArg; - } -}