durumis AI가 요약한 글
- php 버전 업그레이드 후 지원중단된 문법과 버전 문제를 해결하기 위해 Rector라는 마이그레이션 도구를 소개했다.
- Rector는 PHP 5.3~8.2 코드를 원하는 버전에 맞게 문법을 검사하고 수정해준다.
- Rector 사용을 위해 컴포저 설치, rector.php 설정파일 작성, 적용하고 싶지 않은 규칙과 폴더 지정, 실행 등의 과정이 필요하다.
한국의 php cms 인 그누보드5 사이트를 보다가 php 버전 업그레이드 후 지원중단된 문법과 버전으로
어려움을 호소하는 글들이 몇개 있었습니다. 그래서 찾다가..
reddit 에 가보니 rector 라는 툴을 쓰면 마이그레이션을 도와준다고 해서 사용법을 소개합니다.
PHP 7.0 이 출시된 2015년 이후 php 마이그레이션 툴이 나왔는데요 php cs-fixer , rector 등등
rector 가 좀더 사용이 편리한거 같습니다.
rector (발음: 렉터) 는 PHP 5.3 ~ 8.2 의 코드를 원하는 버전에 맞게 문법검사를 하고 고쳐줍니다.
한가지 아쉬운건 rector 는 정적분석기라서 런타임에 일어나는 오류들은 감지하지 못합니다.
Rector 는 php 패키지 관리도구인 컴포저가 설치되어있어야합니다.
1. 컴포저를 설치합니다.
2. 현재 프로젝트 폴더에 rector 를 설치합니다.
composer require rector/rector --dev
3. 설정파일인 rector.php 를 만듭니다.
rector.php 설정
이해를 돕기위해 상황을 정하겠습니다.
대상 소스코드는 php5.3 코드이고 업그레이드 목표버전은 php 7.4 이라고 가정하고 진행합니다.
Rector 버전은 1.0.3 입니다.
<?php use Rector\Config\RectorConfig; use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector; use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector; return RectorConfig::configure() ->withPhpSets(php74: true) ->withParallel(1200, 8, 8) ->withPaths([ __DIR__ . '/src', // 소스코드 경로 ]) ->withSkip([ Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector::class, Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector::class, Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector::class, Rector\Php53\Rector\Ternary\TernaryToElvisRector::class, Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class, Rector\Php80\Rector\FunctionLike\MixedTypeRector::class, Rector\CodeQuality\Rector\If_\SimplifyIfNullableReturnRector::class, Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector::class, Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector::class, Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector::class, ]);
RectorConfig::config() 는 소스코드 폴더, PHP 버전 설정을 합니다.
withPhpSets 함수는 업그레이드 하려는 PHP 함수를 설정합니다.
현재 rector 를 실행하는 php 버전이 php 8.0 인경우
->withPhpSets(php74: true)
이렇게 php버전 : true 할수있습니다.
그러나 실행중인 PHP 버전이 php 7.4 이하인경우
함수에 이름있는 파라미터 문법을 쓸 수없으므로 원하는 버전이 나올때 true 를 설정해야합니다.
rector 는 php 8.3 을 지원하므로 php 74 를 지정하려면 아래와 같습니다.
->withPhpSets( false, // php 8.3 false, // php 8.2 false, // php 8.1 false, // php 8.0 false, // php 8.1 true // php 7.4 )
->withPaths([
__DIR__ , // rector.php 가 있는 디렉토리입니다.
])
->withSkip([ Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector::class, Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector::class, Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector::class, Rector\Php53\Rector\Ternary\TernaryToElvisRector::class, Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class, '/data/*', ])
적용하고 싶지않은 규칙과 폴더를 지정합니다.
폴더는 '/폴더이름/*'
파일은 '파일이름' 입니다.
규칙은 "SensitiveConstantNameRector" 를 추가했는데요
rector 는 PHP 배열에서 undefined index 가 있을 때 이것을 찾지 못하고 상수로만 인식해서 엉뚱하게 바꾸려고 합니다. 그래서 제외 규칙에 추가해줍니다.
AddLiteralSeparatorToNumberRector //array -> [] SimplifyIfElseToTernaryRector TernaryToElvisRector ClassPropertyAssignToConstructorPromotionRector
이 규칙들은 코드스타일이기 때문에 제외합니다.
실행 하기
두가지가 있습니다.
vendor/bin/rector --dry-run --memory-limit=2G
이 명령어는 변경해야할 코드들을 콘솔에 출력하기만 합니다.
소스 코드가 많을수록 램이 많이 필요하기때문에 --memory-limit=2G 라는 옵션으로 사용램을 지정하셔야됩니다.
vendor/bin/rector --memory-limit=2G
이 명령어는 소스코드를 바로 수정합니다.
원하지 않는 옵션이 있거나 오류가 있을 수 있으므로 git 으로 확인을 해야합니다.