diff --git a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/fixture.php.inc b/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/fixture.php.inc deleted file mode 100644 index 38380e7659d..00000000000 --- a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,39 +0,0 @@ - ------ -classWithStaticMethods = self::$container->get(\Rector\Tests\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector\Source\ClassWithStaticMethods::class); - } - public function test() - { - $this->classWithStaticMethods->create('product'); - } -} - -?> diff --git a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists.php.inc b/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists.php.inc deleted file mode 100644 index 103969417d6..00000000000 --- a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists.php.inc +++ /dev/null @@ -1,56 +0,0 @@ -legitimation = 'GO'; - } - - public function test() - { - ClassWithStaticMethods::create('product'); - } -} - -?> ------ -classWithStaticMethods = self::$container->get(\Rector\Tests\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector\Source\ClassWithStaticMethods::class); - $this->legitimation = 'GO'; - } - - public function test() - { - $this->classWithStaticMethods->create('product'); - } -} - -?> diff --git a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists_with_parent_setup.php.inc b/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists_with_parent_setup.php.inc deleted file mode 100644 index 571ab0f796c..00000000000 --- a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Fixture/setup_already_exists_with_parent_setup.php.inc +++ /dev/null @@ -1,59 +0,0 @@ -legitimation = 'GO'; - - parent::setUp(); - } - - public function test() - { - ClassWithStaticMethods::create('product'); - } -} - -?> ------ -legitimation = 'GO'; - - parent::setUp(); - $this->classWithStaticMethods = self::$container->get(\Rector\Tests\RemovingStatic\Rector\Class_\PHPUnitStaticToKernelTestCaseGetRector\Source\ClassWithStaticMethods::class); - } - - public function test() - { - $this->classWithStaticMethods->create('product'); - } -} - -?> diff --git a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/PHPUnitStaticToKernelTestCaseGetRectorTest.php b/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/PHPUnitStaticToKernelTestCaseGetRectorTest.php deleted file mode 100644 index c1427dd5695..00000000000 --- a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/PHPUnitStaticToKernelTestCaseGetRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Source/ClassWithStaticMethods.php b/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Source/ClassWithStaticMethods.php deleted file mode 100644 index e670d76e11c..00000000000 --- a/rules-tests/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector/Source/ClassWithStaticMethods.php +++ /dev/null @@ -1,13 +0,0 @@ -services(); - $services->set(PHPUnitStaticToKernelTestCaseGetRector::class) - ->call('configure', [[ - PHPUnitStaticToKernelTestCaseGetRector::STATIC_CLASS_TYPES => [ClassWithStaticMethods::class], - ]]); -}; diff --git a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/fixture.php.inc b/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/fixture.php.inc deleted file mode 100644 index fe33439b3c5..00000000000 --- a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -deny(); - } -} - -?> ------ -deny(); - } -} - -?> diff --git a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/skip_already_return.php.inc b/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/skip_already_return.php.inc deleted file mode 100644 index abf9c84e031..00000000000 --- a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Fixture/skip_already_return.php.inc +++ /dev/null @@ -1,20 +0,0 @@ -deny(); - } - - public function whatIf(ReturnDeny $returnDeny) - { - if (true) { - return $returnDeny->deny(); - } - } -} diff --git a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/MethodCallToReturnRectorTest.php b/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/MethodCallToReturnRectorTest.php deleted file mode 100644 index 2ef8bca6ec8..00000000000 --- a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/MethodCallToReturnRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Source/ReturnDeny.php b/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Source/ReturnDeny.php deleted file mode 100644 index a8fbd0bd9c7..00000000000 --- a/rules-tests/Transform/Rector/Expression/MethodCallToReturnRector/Source/ReturnDeny.php +++ /dev/null @@ -1,13 +0,0 @@ -services(); - $services->set(MethodCallToReturnRector::class) - ->call('configure', [[ - MethodCallToReturnRector::METHOD_CALL_WRAPS => ValueObjectInliner::inline([ - new MethodCallToReturn(ReturnDeny::class, 'deny'), - ]), - ]]); -}; diff --git a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Fixture/static_functions.php.inc b/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Fixture/static_functions.php.inc deleted file mode 100644 index 3c25e53e125..00000000000 --- a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Fixture/static_functions.php.inc +++ /dev/null @@ -1,24 +0,0 @@ - ------ - diff --git a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/FunctionToStaticMethodRectorTest.php b/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/FunctionToStaticMethodRectorTest.php deleted file mode 100644 index fe655523178..00000000000 --- a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/FunctionToStaticMethodRectorTest.php +++ /dev/null @@ -1,42 +0,0 @@ -doTestFileInfo($smartFileInfo); - - $addedFileWithContent = new AddedFileWithContent( - $this->originalTempFileInfo->getRealPathDirectory() . '/StaticFunctions.php', - FileSystem::read(__DIR__ . '/Source/ExpectedStaticFunctions.php') - ); - - $this->assertFileWasAdded($addedFileWithContent); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Source/ExpectedStaticFunctions.php b/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Source/ExpectedStaticFunctions.php deleted file mode 100644 index ddb40fc37bb..00000000000 --- a/rules-tests/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector/Source/ExpectedStaticFunctions.php +++ /dev/null @@ -1,11 +0,0 @@ -services(); - $services->set(FunctionToStaticMethodRector::class); -}; diff --git a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/Fixture/some_class.php.inc b/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/Fixture/some_class.php.inc deleted file mode 100644 index c09f0a05284..00000000000 --- a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/Fixture/some_class.php.inc +++ /dev/null @@ -1,30 +0,0 @@ -getAttribute('php_doc_info'); - } -} - -?> ------ -phpDocInfoFactory->createFromNodeOrEmpty($node); - } -} - -?> diff --git a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/VariableMethodCallToServiceCallRectorTest.php b/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/VariableMethodCallToServiceCallRectorTest.php deleted file mode 100644 index 1a0710854d3..00000000000 --- a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/VariableMethodCallToServiceCallRectorTest.php +++ /dev/null @@ -1,33 +0,0 @@ -doTestFileInfo($fileInfo); - } - - /** - * @return Iterator - */ - public function provideData(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/config/configured_rule.php b/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/config/configured_rule.php deleted file mode 100644 index a932d5298de..00000000000 --- a/rules-tests/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector/config/configured_rule.php +++ /dev/null @@ -1,30 +0,0 @@ -services(); - - $configuration = ValueObjectInliner::inline([ - new VariableMethodCallToServiceCall( - 'PhpParser\Node', - 'getAttribute', - 'php_doc_info', - 'Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory', - 'createFromNodeOrEmpty' - ), - ]); - - $services->set(VariableMethodCallToServiceCallRector::class) - ->call( - 'configure', - [[ - VariableMethodCallToServiceCallRector::VARIABLE_METHOD_CALLS_TO_SERVICE_CALLS => $configuration, - ]] - ); -}; diff --git a/rules/RemovingStatic/NodeAnalyzer/SetUpClassMethodUpdater.php b/rules/RemovingStatic/NodeAnalyzer/SetUpClassMethodUpdater.php deleted file mode 100644 index f5274f8374f..00000000000 --- a/rules/RemovingStatic/NodeAnalyzer/SetUpClassMethodUpdater.php +++ /dev/null @@ -1,63 +0,0 @@ -getParentSetUpStaticCallPosition($setupClassMethod); - if ($parentSetUpStaticCallPosition === null) { - $setupClassMethod->stmts = array_merge([$parentSetupStaticCall, $assign], (array) $setupClassMethod->stmts); - } else { - assert($setupClassMethod->stmts !== null); - array_splice($setupClassMethod->stmts, $parentSetUpStaticCallPosition + 1, 0, [$assign]); - } - } - - private function getParentSetUpStaticCallPosition(ClassMethod $setupClassMethod): ?int - { - foreach ((array) $setupClassMethod->stmts as $position => $methodStmt) { - if ($methodStmt instanceof Expression) { - $methodStmt = $methodStmt->expr; - } - - if (! $methodStmt instanceof StaticCall) { - continue; - } - - if (! $methodStmt->class instanceof Name) { - continue; - } - - if (! $this->nodeNameResolver->isName($methodStmt->class, 'parent')) { - continue; - } - - if (! $this->nodeNameResolver->isName($methodStmt->name, MethodName::SET_UP)) { - continue; - } - - return $position; - } - - return null; - } -} diff --git a/rules/RemovingStatic/NodeFactory/SelfContainerFactory.php b/rules/RemovingStatic/NodeFactory/SelfContainerFactory.php deleted file mode 100644 index 50d35178a31..00000000000 --- a/rules/RemovingStatic/NodeFactory/SelfContainerFactory.php +++ /dev/null @@ -1,38 +0,0 @@ -staticTypeMapper->mapPHPStanTypeToPhpParserNode($objectType, TypeKind::RETURN()); - if (! $className instanceof Name) { - throw new ShouldNotHappenException(); - } - - $getMethodCall->args[] = new Arg(new ClassConstFetch($className, 'class')); - - return $getMethodCall; - } -} diff --git a/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php b/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php deleted file mode 100644 index d08a3799c6b..00000000000 --- a/rules/RemovingStatic/Rector/Class_/PHPUnitStaticToKernelTestCaseGetRector.php +++ /dev/null @@ -1,300 +0,0 @@ -entityFactory = $this->getService(EntityFactory::class); - } - - public function test() - { - $product = $this->entityFactory->create('product'); - } -} -CODE_SAMPLE - , - [ - self::STATIC_CLASS_TYPES => ['EntityFactory'], - ] - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [StaticCall::class, Class_::class]; - } - - /** - * @param StaticCall|Class_ $node - */ - public function refactor(Node $node): ?Node - { - // skip yourself - $this->newPropertyObjectTypes = []; - - if ($node instanceof Class_) { - if ($this->nodeTypeResolver->isObjectTypes($node, $this->staticObjectTypes)) { - return null; - } - - return $this->processClass($node); - } - - return $this->processStaticCall($node); - } - - /** - * @param array $configuration - */ - public function configure(array $configuration): void - { - $staticClassTypes = $configuration[self::STATIC_CLASS_TYPES] ?? []; - foreach ($staticClassTypes as $staticClassType) { - $this->staticObjectTypes[] = new ObjectType($staticClassType); - } - } - - private function processClass(Class_ $class): ?Class_ - { - if ($this->isObjectType($class, new ObjectType('PHPUnit\Framework\TestCase'))) { - return $this->processPHPUnitClass($class); - } - - // add property with the object - $newPropertyObjectTypes = $this->collectNewPropertyObjectTypes($class); - if ($newPropertyObjectTypes === []) { - return null; - } - - // add via constructor - foreach ($newPropertyObjectTypes as $newPropertyObjectType) { - $newPropertyName = $this->propertyNaming->fqnToVariableName($newPropertyObjectType); - - $propertyMetadata = new PropertyMetadata( - $newPropertyName, - $newPropertyObjectType, - Class_::MODIFIER_PRIVATE - ); - $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); - } - - return $class; - } - - private function processStaticCall(StaticCall $staticCall): ?MethodCall - { - $classLike = $staticCall->getAttribute(AttributeKey::CLASS_NODE); - if (! $classLike instanceof Class_) { - return null; - } - - foreach ($this->staticObjectTypes as $staticObjectType) { - if (! $this->isObjectType($staticCall->class, $staticObjectType)) { - continue; - } - - return $this->convertStaticCallToPropertyMethodCall($staticCall, $staticObjectType); - } - - return null; - } - - private function processPHPUnitClass(Class_ $class): ?Class_ - { - // add property with the object - $newPropertyTypes = $this->collectNewPropertyObjectTypes($class); - if ($newPropertyTypes === []) { - return null; - } - - // add all properties to class - $class = $this->addNewPropertiesToClass($class, $newPropertyTypes); - - $parentSetUpStaticCallExpression = $this->setUpFactory->createParentStaticCall(); - foreach ($newPropertyTypes as $newPropertyType) { - // container fetch assign - $assign = $this->createContainerGetTypeToPropertyAssign($newPropertyType); - - $setupClassMethod = $class->getMethod(MethodName::SET_UP); - - // get setup or create a setup add add it there - if ($setupClassMethod !== null) { - $this->setUpClassMethodUpdater->updateSetUpMethod( - $setupClassMethod, - $parentSetUpStaticCallExpression, - $assign - ); - } else { - $setUpMethod = $this->setUpClassMethodFactory->createSetUpMethod([$assign]); - $this->classInsertManipulator->addAsFirstMethod($class, $setUpMethod); - } - } - - // update parent clsas if not already - if (! $this->isObjectType($class, new ObjectType('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'))) { - $class->extends = new FullyQualified('Symfony\Bundle\FrameworkBundle\Test\KernelTestCase'); - } - - return $class; - } - - /** - * @return ObjectType[] - */ - private function collectNewPropertyObjectTypes(Class_ $class): array - { - $this->newPropertyObjectTypes = []; - - $this->traverseNodesWithCallable($class->stmts, function (Node $node): void { - if (! $node instanceof StaticCall) { - return; - } - - foreach ($this->staticObjectTypes as $staticObjectType) { - if (! $this->isObjectType($node->class, $staticObjectType)) { - continue; - } - - $this->newPropertyObjectTypes[] = $staticObjectType; - } - }); - - $this->newPropertyObjectTypes = array_unique($this->newPropertyObjectTypes); - - return $this->newPropertyObjectTypes; - } - - private function convertStaticCallToPropertyMethodCall(StaticCall $staticCall, ObjectType $objectType): MethodCall - { - // create "$this->someService" instead - $propertyName = $this->propertyNaming->fqnToVariableName($objectType); - $propertyFetch = new PropertyFetch(new Variable('this'), $propertyName); - - // turn static call to method on property call - $methodCall = new MethodCall($propertyFetch, $staticCall->name); - $methodCall->args = $staticCall->args; - - return $methodCall; - } - - /** - * @param ObjectType[] $propertyTypes - */ - private function addNewPropertiesToClass(Class_ $class, array $propertyTypes): Class_ - { - $properties = []; - foreach ($propertyTypes as $propertyType) { - $propertyName = $this->propertyNaming->fqnToVariableName($propertyType); - $properties[] = $this->nodeFactory->createPrivatePropertyFromNameAndType($propertyName, $propertyType); - } - - // add property to the start of the class - $class->stmts = array_merge($properties, $class->stmts); - - return $class; - } - - private function createContainerGetTypeToPropertyAssign(ObjectType $objectType): Expression - { - $getMethodCall = $this->selfContainerFactory->createGetTypeMethodCall($objectType); - - $propertyName = $this->propertyNaming->fqnToVariableName($objectType); - $propertyFetch = new PropertyFetch(new Variable('this'), $propertyName); - - $assign = new Assign($propertyFetch, $getMethodCall); - - return new Expression($assign); - } -} diff --git a/rules/Transform/Naming/FullyQualifiedNameResolver.php b/rules/Transform/Naming/FullyQualifiedNameResolver.php deleted file mode 100644 index 1eb7b88fe3a..00000000000 --- a/rules/Transform/Naming/FullyQualifiedNameResolver.php +++ /dev/null @@ -1,37 +0,0 @@ -betterNodeFinder->findFirstInstanceOf($nodes, Namespace_::class); - if (! $namespace instanceof Namespace_) { - return $shortClassName; - } - - $namespaceName = $this->nodeNameResolver->getName($namespace); - if ($namespaceName === null) { - return $shortClassName; - } - - return $namespaceName . '\\' . $shortClassName; - } -} diff --git a/rules/Transform/NodeFactory/StaticMethodClassFactory.php b/rules/Transform/NodeFactory/StaticMethodClassFactory.php deleted file mode 100644 index 09abf29365b..00000000000 --- a/rules/Transform/NodeFactory/StaticMethodClassFactory.php +++ /dev/null @@ -1,42 +0,0 @@ -makeFinal(); - - foreach ($functions as $function) { - $staticClassMethod = $this->createStaticMethod($function); - $classBuilder->addStmt($staticClassMethod); - } - - return $classBuilder->getNode(); - } - - private function createStaticMethod(Function_ $function): ClassMethod - { - $methodName = $this->classNaming->createMethodNameFromFunction($function); - return $this->classMethodFactory->createClassMethodFromFunction($methodName, $function); - } -} diff --git a/rules/Transform/Rector/Expression/MethodCallToReturnRector.php b/rules/Transform/Rector/Expression/MethodCallToReturnRector.php deleted file mode 100644 index 45eeed1c34d..00000000000 --- a/rules/Transform/Rector/Expression/MethodCallToReturnRector.php +++ /dev/null @@ -1,131 +0,0 @@ -deny(); - } - - public function deny() - { - return 1; - } -} -CODE_SAMPLE -, - <<<'CODE_SAMPLE' -class SomeClass -{ - public function run() - { - return $this->deny(); - } - - public function deny() - { - return 1; - } -} -CODE_SAMPLE - - , - [ - self::METHOD_CALL_WRAPS => [new MethodCallToReturn('SomeClass', 'deny')], - ] - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [Expression::class]; - } - - /** - * @param Expression $node - */ - public function refactor(Node $node): ?Node - { - if (! $node->expr instanceof MethodCall) { - return null; - } - - $methodCall = $node->expr; - - return $this->refactorMethodCall($methodCall); - } - - /** - * @param array $configuration - */ - public function configure(array $configuration): void - { - $methodCallWraps = $configuration[self::METHOD_CALL_WRAPS] ?? []; - Assert::allIsInstanceOf($methodCallWraps, MethodCallToReturn::class); - $this->methodCallWraps = $methodCallWraps; - } - - private function refactorMethodCall(MethodCall $methodCall): ?Node - { - $parent = $methodCall->getAttribute(AttributeKey::PARENT_NODE); - - foreach ($this->methodCallWraps as $methodCallWrap) { - if (! $this->isObjectType($methodCall->var, $methodCallWrap->getObjectType())) { - continue; - } - - if (! $this->isName($methodCall->name, $methodCallWrap->getMethod())) { - continue; - } - - // already wrapped - if ($parent instanceof Return_) { - continue; - } - - return new Return_($methodCall); - } - - return null; - } -} diff --git a/rules/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector.php b/rules/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector.php deleted file mode 100644 index 395faa87a2b..00000000000 --- a/rules/Transform/Rector/FileWithoutNamespace/FunctionToStaticMethodRector.php +++ /dev/null @@ -1,178 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [FileWithoutNamespace::class, Namespace_::class]; - } - - /** - * @param FileWithoutNamespace|Namespace_ $node - */ - public function refactor(Node $node): ?Node - { - /** @var Function_[] $functions */ - $functions = $this->betterNodeFinder->findInstanceOf($node, Function_::class); - if ($functions === []) { - return null; - } - - $smartFileInfo = $this->file->getSmartFileInfo(); - - $shortClassName = $this->classNaming->getNameFromFileInfo($smartFileInfo); - $class = $this->staticMethodClassFactory->createStaticMethodClass($shortClassName, $functions); - - $stmts = $node->stmts; - $this->removeNodes($functions); - - // replace function calls with class static call - $functionsToStaticCalls = $this->resolveFunctionsToStaticCalls($stmts, $shortClassName, $functions); - $node->stmts = $this->replaceFuncCallsWithStaticCalls($stmts, $functionsToStaticCalls); - - $this->printStaticMethodClass($smartFileInfo, $shortClassName, $node, $class); - - return $node; - } - - /** - * @param Node[] $stmts - * @param Function_[] $functions - * @return FunctionToStaticCall[] - */ - private function resolveFunctionsToStaticCalls(array $stmts, string $shortClassName, array $functions): array - { - $functionsToStaticCalls = []; - - $className = $this->fullyQualifiedNameResolver->resolveFullyQualifiedName($stmts, $shortClassName); - foreach ($functions as $function) { - $functionName = $this->getName($function); - if ($functionName === null) { - continue; - } - - $methodName = $this->classNaming->createMethodNameFromFunction($function); - $functionsToStaticCalls[] = new FunctionToStaticCall($functionName, $className, $methodName); - } - - return $functionsToStaticCalls; - } - - /** - * @param Node[] $stmts - * @param FunctionToStaticCall[] $functionsToStaticCalls - * @return Node[] - */ - private function replaceFuncCallsWithStaticCalls(array $stmts, array $functionsToStaticCalls): array - { - $this->traverseNodesWithCallable($stmts, function (Node $node) use ($functionsToStaticCalls): ?StaticCall { - if (! $node instanceof FuncCall) { - return null; - } - - foreach ($functionsToStaticCalls as $functionToStaticCall) { - if (! $this->isName($node, $functionToStaticCall->getFunction())) { - continue; - } - - $staticCall = $this->nodeFactory->createStaticCall( - $functionToStaticCall->getClass(), - $functionToStaticCall->getMethod() - ); - $staticCall->args = $node->args; - - return $staticCall; - } - - return null; - }); - - return $stmts; - } - - private function printStaticMethodClass( - SmartFileInfo $smartFileInfo, - string $shortClassName, - Namespace_ | FileWithoutNamespace $node, - Class_ $class - ): void { - $classFileDestination = $smartFileInfo->getPath() . DIRECTORY_SEPARATOR . $shortClassName . '.php'; - - $nodesToPrint = [$this->resolveNodeToPrint($node, $class)]; - - $addedFileWithNodes = new AddedFileWithNodes($classFileDestination, $nodesToPrint); - $this->removedAndAddedFilesCollector->addAddedFile($addedFileWithNodes); - } - - private function resolveNodeToPrint(Namespace_ | FileWithoutNamespace $node, Class_ $class): Namespace_ | Class_ - { - if ($node instanceof Namespace_) { - return new Namespace_($node->name, [$class]); - } - - return $class; - } -} diff --git a/rules/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector.php b/rules/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector.php deleted file mode 100644 index 53f8e5e4a03..00000000000 --- a/rules/Transform/Rector/MethodCall/VariableMethodCallToServiceCallRector.php +++ /dev/null @@ -1,165 +0,0 @@ -getAttribute('php_doc_info'); - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; -use PhpParser\Node; - -class SomeClass -{ - public function __construct(PhpDocInfoFactory $phpDocInfoFactory) - { - $this->phpDocInfoFactory = $phpDocInfoFactory; - } - public function run(Node $node) - { - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); - } -} -CODE_SAMPLE - , - [ - self::VARIABLE_METHOD_CALLS_TO_SERVICE_CALLS => [ - new VariableMethodCallToServiceCall( - 'PhpParser\Node', - 'getAttribute', - 'php_doc_info', - 'Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory', - 'createFromNodeOrEmpty' - ), - ], - ] - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [MethodCall::class]; - } - - /** - * @param MethodCall $node - */ - public function refactor(Node $node): ?Node - { - foreach ($this->variableMethodCallsToServiceCalls as $variableMethodCallToServiceCall) { - if (! $node->var instanceof Variable) { - continue; - } - - if (! $this->isObjectType($node->var, $variableMethodCallToServiceCall->getVariableObjectType())) { - continue; - } - - if (! $this->isName($node->name, $variableMethodCallToServiceCall->getMethodName())) { - continue; - } - - $firstArgValue = $node->args[0]->value; - if (! $this->valueResolver->isValue( - $firstArgValue, - $variableMethodCallToServiceCall->getArgumentValue() - )) { - continue; - } - - $classLike = $node->getAttribute(AttributeKey::CLASS_NODE); - if (! $classLike instanceof Class_) { - continue; - } - - $serviceObjectType = new ObjectType($variableMethodCallToServiceCall->getServiceType()); - - $propertyName = $this->propertyNaming->fqnToVariableName($serviceObjectType); - $propertyMetadata = new PropertyMetadata($propertyName, $serviceObjectType, Class_::MODIFIER_PRIVATE); - $this->propertyToAddCollector->addPropertyToClass($classLike, $propertyMetadata); - - return $this->createServiceMethodCall( - $serviceObjectType, - $variableMethodCallToServiceCall->getServiceMethodName(), - $node - ); - } - - return null; - } - - /** - * @param mixed[] $configuration - */ - public function configure(array $configuration): void - { - $this->variableMethodCallsToServiceCalls = $configuration[self::VARIABLE_METHOD_CALLS_TO_SERVICE_CALLS] ?? []; - } - - private function createServiceMethodCall(ObjectType $objectType, string $methodName, MethodCall $node): MethodCall - { - $propertyName = $this->propertyNaming->fqnToVariableName($objectType); - $propertyFetch = new PropertyFetch(new Variable('this'), $propertyName); - $methodCall = new MethodCall($propertyFetch, $methodName); - $methodCall->args[] = new Arg($node->var); - - return $methodCall; - } -} diff --git a/rules/Transform/ValueObject/MethodCallToReturn.php b/rules/Transform/ValueObject/MethodCallToReturn.php deleted file mode 100644 index 422d29b228e..00000000000 --- a/rules/Transform/ValueObject/MethodCallToReturn.php +++ /dev/null @@ -1,26 +0,0 @@ -class); - } - - public function getMethod(): string - { - return $this->method; - } -}