掌握程式碼重構:使用 Rector PHP 的完整指南

WBOY
發布: 2024-08-26 06:31:32
原創
225 人瀏覽過

Mastering Code Refactoring: A Complete Guide to Using Rector PHP
照片由 Matteo del Piano 在 Unsplash 上拍攝

PHP 校長簡介

在不斷發展的 PHP 開發世界中,保持程式碼庫乾淨、最新且高效至關重要。這就是 Rector PHP 發揮作用的地方。如果您一直想知道如何使用 Rector PHP、如何安裝它或 Rector PHP 到底是什麼,那麼這份綜合指南非常適合您。我們將引導您了解基礎知識,提供詳細的 Rector PHP 教程,並提供有關 PHP Rector 以及如何有效使用它的見解。讀完本文後,您將牢牢掌握 Rector PHP 以及它如何增強您的開發工作流程。

PHP 校長是什麼?

Rector 於 2020 年啟動,但在 2024 年才發布 1.0 版本。它是一個對程式碼庫執行靜態分析的命令列工具。根據該分析可以應用變更。我想一個很好的例子是,如果你的程式碼庫充滿了 array() 調用,而這現在被認為是一種舊的做法,現在被 [] 短數組語法取代。

透過程式碼庫來替換它是很乏味的。我們可以使用一個簡單的查找和替換工具,但是如果有一個 array() 封裝了一個字串或一個不應更改的註釋,該怎麼辦?現在我們必須檢查每個被替換的實例。

這類問題正是Rector所擅長的。相反,Rector 能夠查看程式碼並明確知道它是否是要替換的陣列。

你可能會問,PHP CS Fixer 也可以做到這一點,這是事實。但 Rector 也讓 PHPStan 在後台工作,不僅可以辨識語法,還可以分析類型。這意味著 Rector 可以偵測某個類別何時具有特定的父類別、變數何時為特定類型或函數的預期回傳類型。賦予它更大的範圍來對程式碼庫進行大量更改。

如何安裝 Rector PHP

對於經驗豐富的 PHP 開發人員來說,這似乎是顯而易見的,但有兩種主要方法,這實際上取決於您想要如何使用 Rector。

如果您想使用 Rector 作為持續整合的一部分,那麼透過 Composer 將不同的 Rector 安裝到您的專案中是有意義的。

composer require --dev rector/rector
登入後複製

但是,如果您想在單一或多個專案上嘗試使用 Rector 來執行升級,那麼最好使用
全域安裝 Rector

composer global require rector/rector
登入後複製

無論您選擇哪一步,下一步就是在專案的根目錄中建立一個配置,並包含所有包含您希望升級的 PHP 程式碼的資料夾。當然,這不包括供應商資料夾,因為我們不想修改它。

這就是 Laravel 專案的配置可能如下所示:

use Rector\Config\RectorConfig;
use Rector\Php71\Rector\List_\ListToArrayDestructRector;

return RectorConfig::configure()
    ->withPaths([__DIR__. '/config', __DIR__. '/src', __DIR__. '/tests', __DIR__. '/routes'])
    ->withImportNames(removeUnusedImports: true);
登入後複製

PHP Rector:如何有效地使用它

就像上一節一樣,使用 Rector 可以根據你想如何使用它來決定。校長透過選擇規則來應用變更。這與 PHP CS Fixer 相同。這些規則都會檢測問題,然後嘗試解決它們旨在解決的問題。

如果我們希望以持續整合的方式使用 Rector,因為我們希望所有程式碼在開發時都盡可能最佳化,我們可能只使用一組特定的規則。

Rector 有一組規則,通常被描述為死程式碼或程式碼質量,分別刪除程式碼或增強和最佳化。堅持使用這些設定是非常有利的,因為我們可以確信它們在大多數情況下都有效。但您絕對應該意識到 Rector 編寫程式碼的方式從來都不是完美的。通常,在編寫規則時,它們可能會涵蓋典型場景,並且可能會遺漏一些在您的程式碼庫中找到的情況。這可能會導致代碼損壞。

如果您想使用 Rector,您絕對應該為您的應用程式編寫測試。沒有它們,Rector 很容易導致引入錯誤,而這些錯誤在成為問題之前你不會發現。

使用 Rector 時要考慮的另一件事是您應該使用像 PHP CS Fixer 這樣的 linting 工具。 Rector 不會查看空格、方法呼叫之間的空格和換行符及其參數等。在 Rector 應用其修復後,使用 linting 工具應將程式碼格式保持為您期望的標準。

Rector PHP 逐步教程

現在我們已經安裝了 Rector,我們準備好嘗試一下,讓我們嘗試應用一個規則。讓我們從更新之前製作的設定檔開始。

use Rector\Config\RectorConfig;
use Rector\Php71\Rector\List_\ListToArrayDestructRector;

return RectorConfig::configure()
    ->withPaths([__DIR__. '/config', __DIR__. '/src', __DIR__. '/tests', __DIR__. '/routes'])
    ->withImportNames(removeUnusedImports: true)
    ->withRules([
        ListToArrayDestructRector::class,
    ]);
登入後複製

在此配置中,我們將取代清單的使用,從而使用陣列重組。我們預期做出的改變如下:

-list($a, $b) = ['a', 'b'];
+[$a, $b] = ['a', 'b'];
登入後複製

Now we can test this config by performing a dry run of Rector.

rector --dry-run
登入後複製

If everything has been successful we should now see an output that contains a diff of all the changes to each file, with a list of the rules that made the changes. Our config only lists one rule so only one rule is applied but if more rules are added we’ll see a list of one or more here.

Mastering Code Refactoring: A Complete Guide to Using Rector PHP

This is then a great time to review the changes and make sure the rule is performing as expected. Once we’re happy we can run Rector again. Without the dry run flag, changes will be written to the files.

rector
登入後複製

After it’s completed we should run our favourite linting tool and then run our tests. That’s it. We’ve now used Rector.

Conclusion: Boosting Your PHP Development with Rector

Rector PHP is a powerful tool that can significantly enhance your PHP development workflow by automating code refactoring and ensuring your codebase remains clean and modern. By understanding what Rector PHP is, learning how to install it, and mastering how to use it effectively, you can leverage its capabilities to streamline code updates, improve code quality, and reduce the risk of code feeling like a “legacy” project. Whether you’re integrating Rector into your continuous integration pipeline or using it for large-scale codebase upgrades, Rector is an indispensable asset for any PHP developer looking to maintain a high standard of code excellence.

If you want to take you knowledge of Rector further I suggest going to https://getrector.com/.

I’m Peter Fox, a software developer in the UK who works with Laravel. Thank you for reading my article, I’ve got many more to read at https://articles.peterfox.me. I’m also now Sponsorable on GitHub. If you’d like to encourage me to write more articles like this please do consider dropping a small one-off donation.

以上是掌握程式碼重構:使用 Rector PHP 的完整指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!