diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index e29b393904e..d127a71a793 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 418 Rules Overview +# 417 Rules Overview
@@ -62,7 +62,7 @@ - [Strict](#strict) (6) -- [Transform](#transform) (34) +- [Transform](#transform) (33) - [TypeDeclaration](#typedeclaration) (40) @@ -4041,12 +4041,12 @@ Return early prepared value in ifs { - $var = null; - - if (rand(0,1)) { + if (rand(0, 1)) { - $var = 1; + return 1; } - if (rand(0,1)) { + if (rand(0, 1)) { - $var = 2; + return 2; } @@ -7865,57 +7865,6 @@ return static function (RectorConfig $rectorConfig): void {
-### ArgumentFuncCallToMethodCallRector - -Move help facade-like function calls to constructor injection - -:wrench: **configure it!** - -- class: [`Rector\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector`](../rules/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector.php) - -```php -ruleWithConfiguration(ArgumentFuncCallToMethodCallRector::class, [ - new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make'), - ]); -}; -``` - -↓ - -```diff - class SomeController - { -+ /** -+ * @var \Illuminate\Contracts\View\Factory -+ */ -+ private $viewFactory; -+ -+ public function __construct(\Illuminate\Contracts\View\Factory $viewFactory) -+ { -+ $this->viewFactory = $viewFactory; -+ } -+ - public function action() - { -- $template = view('template.blade'); -- $viewFactory = view(); -+ $template = $this->viewFactory->make('template.blade'); -+ $viewFactory = $this->viewFactory; - } - } -``` - -
- ### AttributeKeyToClassConstFetchRector Replace key value on specific attribute to class constant diff --git a/packages-tests/PhpAttribute/Printer/PhpAttributeGroupFactoryTest.php b/packages-tests/PhpAttribute/Printer/PhpAttributeGroupFactoryTest.php index 45743a4b6db..9cda722974f 100644 --- a/packages-tests/PhpAttribute/Printer/PhpAttributeGroupFactoryTest.php +++ b/packages-tests/PhpAttribute/Printer/PhpAttributeGroupFactoryTest.php @@ -9,7 +9,6 @@ use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode; use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; use Rector\Testing\PHPUnit\AbstractTestCase; -use Rector\Tests\Transform\Rector\FuncCall\ArgumentFuncCallToMethodCallRector\Fixture\Route; final class PhpAttributeGroupFactoryTest extends AbstractTestCase { @@ -39,7 +38,7 @@ public function testCreateArgsFromItems(): void $args = $this->phpAttributeGroupFactory->createArgsFromItems([ new ArrayItemNode('/path', 'path'), new ArrayItemNode('action', 'name'), - ], Route::class); + ], 'SomeClass'); $this->assertCount(2, $args); $this->assertContainsOnlyInstancesOf(Arg::class, $args); diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/ArgumentFuncCallToMethodCallRectorTest.php b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/ArgumentFuncCallToMethodCallRectorTest.php deleted file mode 100644 index eb70a72d597..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/ArgumentFuncCallToMethodCallRectorTest.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/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/back.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/back.php.inc deleted file mode 100644 index 16254cc5023..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/back.php.inc +++ /dev/null @@ -1,40 +0,0 @@ - ------ -redirector->back(); - } - - public function actionWithParams() - { - return $this->redirector->back(200); - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/broadcast.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/broadcast.php.inc deleted file mode 100644 index 7eb0ac54a42..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/broadcast.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ -broadcastingFactory->event('template.blade'); - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/config.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/config.php.inc deleted file mode 100644 index 43730c7e823..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/config.php.inc +++ /dev/null @@ -1,40 +0,0 @@ - $value]); - } -} - -?> ------ -configRepository->get('value'); - } - - public function actionSet($value) - { - $this->configRepository->set(['value' => $value]); - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/route.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/route.php.inc deleted file mode 100644 index e44e6e2f7e5..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/route.php.inc +++ /dev/null @@ -1,30 +0,0 @@ - ------ -urlGenerator->route('template.blade'); - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/session.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/session.php.inc deleted file mode 100644 index 2643861968d..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/session.php.inc +++ /dev/null @@ -1,34 +0,0 @@ - ------ -sessionManager; - $this->sessionManager->put(['key']); - $this->sessionManager->get('key', 'value'); - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/skip_static_method.php.inc b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/skip_static_method.php.inc deleted file mode 100644 index 9f1b2ceb9b6..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/Fixture/skip_static_method.php.inc +++ /dev/null @@ -1,11 +0,0 @@ - ------ -viewFactory->make('template.blade'); - $viewFactory = $this->viewFactory; - } -} - -?> diff --git a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/config/configured_rule.php b/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/config/configured_rule.php deleted file mode 100644 index 9d21a1dae38..00000000000 --- a/rules-tests/Transform/Rector/FuncCall/ArgumentFuncCallToMethodCallRector/config/configured_rule.php +++ /dev/null @@ -1,21 +0,0 @@ -ruleWithConfiguration(ArgumentFuncCallToMethodCallRector::class, [ - new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make'), - new ArgumentFuncCallToMethodCall('route', 'Illuminate\Routing\UrlGenerator', 'route'), - new ArgumentFuncCallToMethodCall('back', 'Illuminate\Routing\Redirector', 'back', 'back'), - new ArgumentFuncCallToMethodCall('broadcast', 'Illuminate\Contracts\Broadcasting\Factory', 'event'), - - new ArrayFuncCallToMethodCall('config', 'Illuminate\Contracts\Config\Repository', 'set', 'get'), - new ArrayFuncCallToMethodCall('session', 'Illuminate\Session\SessionManager', 'put', 'get'), - ]); -}; diff --git a/rules/Transform/Contract/ValueObject/ArgumentFuncCallToMethodCallInterface.php b/rules/Transform/Contract/ValueObject/ArgumentFuncCallToMethodCallInterface.php deleted file mode 100644 index f2b0b789a19..00000000000 --- a/rules/Transform/Contract/ValueObject/ArgumentFuncCallToMethodCallInterface.php +++ /dev/null @@ -1,10 +0,0 @@ -viewFactory = $viewFactory; - } - - public function action() - { - $template = $this->viewFactory->make('template.blade'); - $viewFactory = $this->viewFactory; - } -} -CODE_SAMPLE - , - [new ArgumentFuncCallToMethodCall('view', 'Illuminate\Contracts\View\Factory', 'make')] - ), - ]); - } - - /** - * @return array> - */ - public function getNodeTypes(): array - { - return [FuncCall::class]; - } - - /** - * @param FuncCall $node - */ - public function refactor(Node $node): ?Node - { - if ($this->shouldSkipFuncCall($node)) { - return null; - } - - /** @var Class_ $classLike */ - $classLike = $this->betterNodeFinder->findParentType($node, Class_::class); - - foreach ($this->argumentFuncCallToMethodCalls as $argumentFuncCallToMethodCall) { - if (! $this->isName($node, $argumentFuncCallToMethodCall->getFunction())) { - continue; - } - - if ($argumentFuncCallToMethodCall instanceof ArgumentFuncCallToMethodCall) { - return $this->refactorFuncCallToMethodCall($argumentFuncCallToMethodCall, $classLike, $node); - } - - if ($argumentFuncCallToMethodCall instanceof ArrayFuncCallToMethodCall) { - return $this->refactorArrayFunctionToMethodCall($argumentFuncCallToMethodCall, $node, $classLike); - } - } - - return null; - } - - /** - * @param mixed[] $configuration - */ - public function configure(array $configuration): void - { - Assert::allIsAOf($configuration, ArgumentFuncCallToMethodCallInterface::class); - - $this->argumentFuncCallToMethodCalls = $configuration; - } - - private function shouldSkipFuncCall(FuncCall $funcCall): bool - { - // we can inject only in injectable class method context - /** @var ClassMethod|null $classMethod */ - $classMethod = $this->betterNodeFinder->findParentType($funcCall, ClassMethod::class); - if (! $classMethod instanceof ClassMethod) { - return true; - } - - return $classMethod->isStatic(); - } - - /** - * @return MethodCall|PropertyFetch|null - */ - private function refactorFuncCallToMethodCall( - ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall, - Class_ $class, - FuncCall $funcCall - ): ?Node { - $fullyQualifiedObjectType = new FullyQualifiedObjectType($argumentFuncCallToMethodCall->getClass()); - $expectedName = $this->propertyNaming->getExpectedNameFromType($fullyQualifiedObjectType); - - if (! $expectedName instanceof ExpectedName) { - throw new ShouldNotHappenException(); - } - - $propertyMetadata = new PropertyMetadata( - $expectedName->getName(), - $fullyQualifiedObjectType, - Class_::MODIFIER_PRIVATE - ); - $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); - - $propertyFetchNode = $this->nodeFactory->createPropertyFetch('this', $expectedName->getName()); - - if ($funcCall->args === []) { - return $this->refactorEmptyFuncCallArgs($argumentFuncCallToMethodCall, $propertyFetchNode); - } - - if ($this->isFunctionToMethodCallWithArgs($funcCall, $argumentFuncCallToMethodCall)) { - $methodName = $argumentFuncCallToMethodCall->getMethodIfArgs(); - if (! is_string($methodName)) { - throw new ShouldNotHappenException(); - } - - return new MethodCall($propertyFetchNode, $methodName, $funcCall->args); - } - - return null; - } - - /** - * @return PropertyFetch|MethodCall|null - */ - private function refactorArrayFunctionToMethodCall( - ArrayFuncCallToMethodCall $arrayFuncCallToMethodCall, - FuncCall $funcCall, - Class_ $class - ): ?Node { - $propertyName = $this->propertyNaming->fqnToVariableName($arrayFuncCallToMethodCall->getClass()); - $propertyFetch = $this->nodeFactory->createPropertyFetch('this', $propertyName); - - $fullyQualifiedObjectType = new FullyQualifiedObjectType($arrayFuncCallToMethodCall->getClass()); - - $propertyMetadata = new PropertyMetadata($propertyName, $fullyQualifiedObjectType, Class_::MODIFIER_PRIVATE); - $this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata); - - return $this->createMethodCallArrayFunctionToMethodCall( - $funcCall, - $arrayFuncCallToMethodCall, - $propertyFetch - ); - } - - private function refactorEmptyFuncCallArgs( - ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall, - PropertyFetch $propertyFetch - ): MethodCall | PropertyFetch { - if ($argumentFuncCallToMethodCall->getMethodIfNoArgs() !== null) { - $methodName = $argumentFuncCallToMethodCall->getMethodIfNoArgs(); - return new MethodCall($propertyFetch, $methodName); - } - - return $propertyFetch; - } - - private function isFunctionToMethodCallWithArgs( - FuncCall $funcCall, - ArgumentFuncCallToMethodCall $argumentFuncCallToMethodCall - ): bool { - if ($argumentFuncCallToMethodCall->getMethodIfArgs() === null) { - return false; - } - - return count($funcCall->args) >= 1; - } - - /** - * @return PropertyFetch|MethodCall|null - */ - private function createMethodCallArrayFunctionToMethodCall( - FuncCall $funcCall, - ArrayFuncCallToMethodCall $arrayFuncCallToMethodCall, - PropertyFetch $propertyFetch - ): ?Node { - if ($funcCall->getArgs() === []) { - return $propertyFetch; - } - - if ($this->arrayTypeAnalyzer->isArrayType($funcCall->getArgs()[0]->value)) { - return new MethodCall($propertyFetch, $arrayFuncCallToMethodCall->getArrayMethod(), $funcCall->getArgs()); - } - - if ($arrayFuncCallToMethodCall->getNonArrayMethod() === '') { - return null; - } - - return new MethodCall($propertyFetch, $arrayFuncCallToMethodCall->getNonArrayMethod(), $funcCall->getArgs()); - } -} diff --git a/rules/Transform/ValueObject/ArgumentFuncCallToMethodCall.php b/rules/Transform/ValueObject/ArgumentFuncCallToMethodCall.php deleted file mode 100644 index 3707cb8f0ec..00000000000 --- a/rules/Transform/ValueObject/ArgumentFuncCallToMethodCall.php +++ /dev/null @@ -1,41 +0,0 @@ -function; - } - - public function getClass(): string - { - return $this->class; - } - - public function getMethodIfNoArgs(): ?string - { - return $this->methodIfNoArgs; - } - - public function getMethodIfArgs(): ?string - { - return $this->methodIfArgs; - } -} diff --git a/rules/Transform/ValueObject/ArrayFuncCallToMethodCall.php b/rules/Transform/ValueObject/ArrayFuncCallToMethodCall.php deleted file mode 100644 index e08538d3085..00000000000 --- a/rules/Transform/ValueObject/ArrayFuncCallToMethodCall.php +++ /dev/null @@ -1,49 +0,0 @@ -function; - } - - public function getClass(): string - { - return $this->class; - } - - public function getArrayMethod(): string - { - return $this->arrayMethod; - } - - public function getNonArrayMethod(): string - { - return $this->nonArrayMethod; - } -}