Skip to content

Latest commit

 

History

History
263 lines (182 loc) · 6.43 KB

rector_rules_overview.md

File metadata and controls

263 lines (182 loc) · 6.43 KB

11 Rules Overview

AddArgumentToClassWithoutDefaultRector

This Rector adds new default arguments in calls of defined methods and class types.

🔧 configure it!

<?php

declare(strict_types=1);

use Frosh\Rector\Rule\ClassMethod\AddArgumentToClassWithoutDefault;
use Frosh\Rector\Rule\ClassMethod\AddArgumentToClassWithoutDefaultRector;
use PHPStan\Type\ObjectType;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(AddArgumentToClassWithoutDefaultRector::class, [
        new AddArgumentToClassWithoutDefault('SomeExampleClass', 'someMethod', 0, new ObjectType('SomeType'), 'someArgument'),
    ]);
};

 $someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);

 class MyCustomClass extends SomeExampleClass
 {
-    public function someMethod()
+    public function someMethod($value)
     {
     }
 }

AddBanAllToReverseProxyRector

Adds banAll method to reverse proxy

 class Test extends \Shopware\Core\Framework\Cache\ReverseProxy\AbstractReverseProxyGateway {
-
+    public function banAll(): void
+    {
+    }
 }

ContextMetadataExtensionToStateRector

Migrate extension metadata to state rector

-$context->addExtension(EntityIndexerRegistry::USE_INDEXING_QUEUE, new ArrayEntity());
+$context->addState(EntityIndexerRegistry::USE_INDEXING_QUEUE);

FakerPropertyToMethodCallRector

Move faker property to method call

-$this->faker->randomDigit
+$this->faker->randomDigit()

InterfaceReplacedWithAbstractClassRector

Replace UrlProviderInterface with AbstractClass

🔧 configure it!

<?php

declare(strict_types=1);

use Frosh\Rector\Rule\Class_\InterfaceReplacedWithAbstractClass;
use Frosh\Rector\Rule\Class_\InterfaceReplacedWithAbstractClassRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(InterfaceReplacedWithAbstractClassRector::class, [
        new InterfaceReplacedWithAbstractClass('Foo', 'AbstractTest'),
    ]);
};

-class Foo implements Test {
+class Foo extends AbstractTest {

 }

MakeClassConstructorArgumentRequiredRector

NAME

🔧 configure it!

<?php

declare(strict_types=1);

use Frosh\Rector\Rule\ClassConstructor\MakeClassConstructorArgumentRequired;
use Frosh\Rector\Rule\ClassConstructor\MakeClassConstructorArgumentRequiredRector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\StringType;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(MakeClassConstructorArgumentRequiredRector::class, [
        new MakeClassConstructorArgumentRequired('Foo', 0, new ArrayType(new StringType(), new StringType())),
    ]);
};

 class Foo {
-    public function __construct(array $foo = [])
+    public function __construct(array $foo)
     {
     }
 }

MigrateCaptchaAnnotationToRouteRector

NAME

 class Foo
 {
     /**
-     * @Route("/form/contact", name="frontend.form.contact.send", methods={"POST"}, defaults={"XmlHttpRequest"=true})
-     * @Captcha
+     * @Route("/form/contact", name="frontend.form.contact.send", methods={"POST"}, defaults={"XmlHttpRequest"=true, "_captcha"=true})
      */
-    public function sendContactForm()
+    public function sendContactForm(): Response
     {
     }
 }

MigrateLoginRequiredAnnotationToRouteRector

Migrates Annotations to Route annotation

-@LoginRequired
-@Route("/store-api/product", name="store-api.product.search", methods={"GET", "POST"})
+@Route("/store-api/product", name="store-api.product.search", methods={"GET", "POST"}, defaults={"_loginRequired"=true})
 public function myAction()

MigrateRouteScopeToRouteDefaults

NAME

 /**
- * @RouteScope(scopes={"storefront"})
+ * @Route(defaults={"_routeScope"={"storefront"}})
  */
 class Controller
 {
 }

RemoveArgumentFromClassConstructRector

This Rector removes an argument in the defined class construct.

🔧 configure it!

<?php

declare(strict_types=1);

use Frosh\Rector\Rule\ClassConstructor\RemoveArgumentFromClassConstruct;
use Frosh\Rector\Rule\ClassConstructor\RemoveArgumentFromClassConstructRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
    $rectorConfig->ruleWithConfiguration(RemoveArgumentFromClassConstructRector::class, [
        new RemoveArgumentFromClassConstruct('SomeExampleClass', 0),
    ]);
};

-$someObject = new SomeExampleClass($example);
+$someObject = new SomeExampleClass();

ThumbnailGenerateSingleToMultiGenerateRector

Move single thumbnail generation call to batch

-$thumbnail->generateThumbnails($media, $context);
+$thumbnail->generate(new MediaCollection([$media]), $context);