iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 23
0
自我挑戰組

花式PHP系列 第 24

PHP:升級7.0前的語法檢查

read me senpai

這篇文章適合以下的觀眾:

  • 你手上有專案想要升級到 PHP 7,但怕語法不相容
  • 有 composer 使用經驗
  • 有 commandline 經驗

語法的問題其實很好解決

用工具就好拉!
前陣子找到wimg/PHPCompatibility可以幫忙爬完整個專案的檔案,
分析其中有沒有與 php7 不相容的語法~

即使這個工具並不完美

  • PHP 5.3+ 以上請使用 PHP CodeSniffer 1.x and 2.x.
  • PHP 5.4+ 以上請使用 PHP CodeSniffer 3.x.

底下內容摘自 wimg/PHPCompatibility requirements
「雖然這個檢查工具設計成相容性報告不應受到執行時 php 版本的影響,
 但最好還是使用 php 5.4 以上,以確保得到最一致的報告。」
 
「PHP CodeSniffer 1.5.6 被要求至少要能檢查 90% 的規則,
 而 PHP CodeSniffer 2.6.0 以上都能執行完整的檢查」

composer.json

首先先在 composer.json 加入底下這些:

"require-dev": {
    "squizlabs/php_codesniffer": "^2.2 || ^3.0.2",
    "wimg/php-compatibility": "*",
    "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3"
},
"prefer-stable" : true

squizlabs/php_codesniffer 是檢查工具的主體;
wimg/php-compatibility 是規則集;
phpcodesniffer-composer-installer 則是「讓規則集能被檢查工具使用」的工具。

commandline

安裝工具

然後執行:

composer update --lock

and there you go~

執行檢查

該安裝的工具都安裝完了,就可以先 cd 到專案目錄底下,
然後執行底下這行了~

./vendor/bin/phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 7.0- --report-full=./update.log

參數解釋

fragment 意思
./vendor/bin/phpcs 執行 squizlabs/php_codesniffer
-p 顯示執行進度
. 要被檢查的資料夾
--standard 使用的規則集
--runtime-set testVersion 7.0- 設定目標版本是 php 7.0 以上。你也可以用「7.0」、「7.0-7.1」
--report-full=./update.log 把相容性報告寫進「目前所在資料夾的 update.log」

然後就

耐心等待、仔細尋找

等到執行完成就用報告來改程式碼就行了!


上一篇
PHP:yield
下一篇
PHP:Xdebug搭配Kcachegrind做效能分析
系列文
花式PHP31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言