PHP兼容性检查,PHP升级语法检查(PHPCompatibility+PHP_CodeSniffer)

和飞翔
2023-12-01

当我们进行PHP版本升级的时候,比如从5.x升级到7.x会遇到项目代码不兼容的情况。如果通过人工检查,工作量比较大,我们可以使用PHPCompatibility+PHP_CodeSniffer进行兼容性检查。

1)先安装PHP_CodeSniffer,建议使用composer安装

composer global require "squizlabs/php_codesniffer=*"

2)安装PHPCompatibility

这里建议使用下载zip包的方式,因为PHPCompatibility仅仅作为PHP_CodeSniffer的插件使用。

https://github.com/PHPCompatibility/PHPCompatibility#installation-via-a-git-check-out-to-an-arbitrary-directory-method-2

Download the latest PHPCompatibility release and unzip/untar it into an arbitrary directory.

phpcs --config-set installed_paths /path/to/PHPCompatibility

3)运行兼容性检查

phpcs --standard=PHPCompatibility [path]

运行的结果如下:可以看到文件名,不兼容的line。

FILE: Crypt.php
----------------------------------------------------------------------
FOUND 15 ERRORS AFFECTING 8 LINES
----------------------------------------------------------------------
 30 | ERROR | The constant "MCRYPT_RIJNDAEL_256" is deprecated since
    |       | PHP 7.1 and removed since PHP 7.2

 类似资料: