Find the best Rector rule to solve your problem
Add return type from symfony serializer
final class SomeClass
{
private \Symfony\Component\Serializer\Serializer $serializer;
- public function resolveEntity($data)
+ public function resolveEntity($data): SomeType
{
return $this->serializer->deserialize($data, SomeType::class, 'json');
}
}
Downgrade Symfony Command Attribute
#[AsCommand(name: 'app:create-user', description: 'some description')]
class CreateUserCommand extends Command
{
+ protected function configure(): void
+ {
+ $this->setName('app:create-user');
+ $this->setDescription('some description');
+ }
}
Change form option "max_length" to a form "attr" > "max_length"
$formBuilder = new Symfony\Component\Form\FormBuilder();
$form = $formBuilder->create('name', 'text', [
- 'max_length' => 123,
+ 'attr' => ['maxlength' => 123],
]);
Replace Sensio @Route annotation with Symfony one
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Symfony\Component\Routing\Annotation\Route;
final class SomeClass
{
/**
* @Route()
*/
public function run()
{
}
}
Converts typical Symfony services like $this->get("validator") in commands/controllers to constructor injection (step 3/x)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Validator\Validator\ValidatorInterface;
final class SomeController extends Controller
{
+ public function __construct(private ValidatorInterface $validator)
+
public function someMethod()
{
- $someType = $this->get('validator');
+ $someType = $this->validator;
}
}
Add type to property by added rules, mostly public/property by parent type
class SomeClass extends ParentClass
{
- public $name;
+ public string $name;
}
Turns string Form Type references to their CONSTANT alternatives in FormTypes in Form in Symfony. To enable custom types, add link to your container XML dump in "$rectorConfig->symfonyContainerXml(...)"
$formBuilder = new Symfony\Component\Form\FormBuilder;
-$formBuilder->add('name', 'form.type.text');
+$formBuilder->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class);
Change $service->set() string names to class-type-based names, to allow $container->get() by types in Symfony 2.8. Provide XML config via $rectorConfig->symfonyContainerXml(...);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
- $services->set('some_name', App\SomeClass::class);
+ $services->set('app\\someclass', App\SomeClass::class);
};
Convert string validation rules into arrays for Laravel's Validator.
Validator::make($data, [
- 'field' => 'required|nullable|string|max:255',
+ 'field' => ['required', 'nullable', 'string', 'max:255'],
]);