ไทย
เครื่องมือปรับปรุงเวอร์ชัน PHP rector แนะนำ
- ภาษาที่เขียน:ภาษาเกาหลี
- •
- ประเทศอ้างอิง:ทุกประเทศ
- •
- เทคโนโลยีสารสนเทศ
สรุปโดย AI ของ durumis
- แนะนำเครื่องมือย้ายถ่าน Rector เพื่อแก้ไขไวยากรณ์และปัญหาเวอร์ชันที่ถูกเลิกใช้หลังจากอัปเกรด PHP version
- Rector สามารถตรวจสอบและแก้ไขไวยากรณ์ของรหัส PHP 5.3 ~ 8.2 ให้ตรงกับเวอร์ชันที่ต้องการ
- ขั้นตอนในการใช้ Rector คือ การติดตั้ง Composer, เขียนไฟล์การตั้งค่า rector.php, ระบุกฎและโฟลเดอร์ที่ไม่ต้องการนำมาปรับปรุง และเรียกใช้
และเปิดเว็บไซต์คนที่เกาหลี GNUboard5 เป็น PHP cms ที่อัปเกรดเวอร์ชันของ PHP คณะกรรมการให้ความช่วยเหลือแล้วมีการใช้ไวยากรณ์ที่ไม่หลับนอนและรุ่น
หลังจากอ่านพวกเราก็เจอปัญหา
เราจะแนะนำวิธีใช้งานเพราะคุณสามารถใช้ได้กับเครื่องมือ rector บน reddit ที่จะช่วยจัดการย้ายข้อมูล
เครื่องมือ PHP ย้ายข้อมูลจะออกมา 2015 ซึ่งเป็นปีที่เปิดตัวเวอร์ชัน PHP 7.0 เช่น php cs-fixer, rector และอื่นๆ
rector เป็นเครื่องมือที่ใช้ได้ง่ายกว่า
rector (การออกเสียง: เร็คเตอร์) จะช่วยวิเคราะห์และแก้ไวยากรณ์ของโค้ดของ PHP 5.3~8.2 ให้ตรงกับเวอร์ชันที่คุณต้องการ
มีข้อเสียคือ rector เป็นโปรแกรมวิเคราะห์แบบคงที่ ซึ่งไม่มีการตรวจพบข้อผิดพลาดที่เกิดขึ้นในขณะรันไทม์
Rector จะต้องติดตั้ง Composer ซึ่งเป็นเครื่องมือจัดการแพ็คเกจ PHP
1. ติดตั้ง Composer
2. ติดตั้ง rector ในโฟลเดอร์โปรเจ็กต์ที่กำลังทำงานอยู่
```php composer require rector/rector --dev
3. สร้างไฟล์ rector.php ซึ่งเป็นไฟล์การตั้งค่า
การตั้งค่า rector.php
เพื่อความเข้าใจ เราจะกำหนดเงื่อนไข
สมมติว่าซอร์สโค้ดที่กำหนดเป้าหมายเป็นโค้ด php5.3 และเป้าหมายของการอัปเกรดคือ php 7.4
เวอร์ชัน Rector คือ 1.0.3
```php <?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 ที่คุณต้องการอัปเกรด
หากเวอร์ชัน PHP ที่ใช้รัน rector คือ 8.0
```php ->withPhpSets(php74: true)
ทำแบบนี้แล้วตั้งค่า เวอร์ชัน php: true ได้เลย
แต่ถ้าเวอร์ชัน PHP ที่รันอยู่ต่ำกว่า 7.4
เนื่องจากคุณใช้ไวยากรณ์ชื่อพารามิเตอร์ในฟังก์ชันไม่ได้ เราจึงต้องตั้งค่า true เมื่อต้องการเวอร์ชันที่ต้องการ
rector รองรับ php 8.3 ดังนั้น หากต้องการกำหนดให้เป็น php 74 ให้ทำดังนี้
```php ->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
])
```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
AddLiteralSeparatorToNumberRector //array -> []
SimplifyIfElseToTernaryRector
TernaryToElvisRector
ClassPropertyAssignToConstructorPromotionRector
เราจึงยกเว้นกฎเหล่านี้เนื่องจากเป็นสไตล์โค้ด
ใช้งาน
มี