Skip to content

Instantly share code, notes, and snippets.

@ChMat
Created January 17, 2024 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChMat/20b47bed82cf0846b3f2e3efb23ff605 to your computer and use it in GitHub Desktop.
Save ChMat/20b47bed82cf0846b3f2e3efb23ff605 to your computer and use it in GitHub Desktop.
Configuration Rector pour migrer vers PHP 8 & Symfony 6

Migrer vers Symfony 6 et PHP 8.1

Un embryon de code pour faciliter la montée en version avec Rector.

Installation de rector

composer require rector/rector --dev

Déposer le fichier rector.php à la racine du projet puis lancer Rector :

D’abord en mode prévisualisation avec l’option --dry-run :

vendor/bin/rector process src --dry-run

Puis, pour appliquer les changements proposés, retirer l’option --dry-run :

vendor/bin/rector process src

Les lignes de commandes ci-dessus partent du principe que le code PHP de l’application se trouve dans le dossier src.

Dans l’état actuel, il restera sans doute encore quelques refactorisations à mettre en place, mais ça fait déjà pas mal de travail :)

Aller plus loin

Consulter la documentation complète de Rector

<?php
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\JMSSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
$rectorConfig->rules([
InlineConstructorDefaultToPropertyRector::class,
ReturnTypeFromStrictNativeCallRector::class,
ReturnTypeFromStrictScalarReturnExprRector::class
]);
// Ajouter des use et renseigner le nom raccourci des classes
$rectorConfig->importNames();
// Ne pas importer les classes natives de PHP comme \Exception
$rectorConfig->importShortClasses(false);
// define sets of rules
$rectorConfig->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
JMSSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);
// Update your `Collection` syntax for PHPStorm and PHPStan friendly?
$rectorConfig->import(DoctrineSetList::DOCTRINE_CODE_QUALITY);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment