PHP7 兼容性检测

PHP 7 都出到7.4 了,是时候把手中的discuz论坛升级到PHP 7了,考虑了半天兼容性的问题,准备升级到php 7.3

discuz 的主程序已经升级到最新的discuz 20191201版本了,兼容PHP 7.3应该是没有问题,剩下的就是安装的那些插件的兼容性的问题.

检测PHP7 的兼容性,我们这里使用主流的PHPCompatibility 配合PHP Code_Sniffer

https://github.com/squizlabs/PHP_CodeSniffer
https://github.com/PHPCompatibility/PHPCompatibility

PHPCompatibility 是PHP Code_Sniffer 的插件,因此我们应该先安装PHP Code_Sniffer, 在github上有很多中的安装方式,这里我们选择最近的wget 或者curl下载二进制程序,然后把二进制程序放到全局去

# Download using curl
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Or download using wget
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# register as global commands
mv phpcs.phar phpcs
mv phpcbf.phar phpcbf
chmod 755 phpcs
chmod 755 phpcbf
mv phpcs /usr/local/bin/
mv phpcbf /usr/local/bin/

这样就安装完了phpcs,下一步我们需要下载PHPCompatibility这个插件,并且让phpcs使用这个插件

# Download PHPCompability
cd ~
wget https://github.com/PHPCompatibility/PHPCompatibility/archive/9.3.5.zip
unzip 9.3.5.zip
# path is /root/PHPCompability9.3.5
# config phpcs to use PHPCompatibility
phpcs --config-set installed_paths /root/PHPCompability9.3.5

这样phpcs就可以使用PHPCompatibility9.3.5了,当然了也可以使用git的形式来下载,这样更新PHPCompatibility比较方便

加入说我们需要检查的文件folder 是/home/plugin, 那么我们就可以用

phpcs -p --standard=PHPCompatibility --runtime-set testVersion 7.3 --report-full=/home/php.log /home/plugin

-p: 打印progress到console上面

–standard: 表示使用哪个标准

–runtime-set testVersion 7.3 : 表示用PHP7.3的标准来检查

–report-full: 表示将结果输出到某一文件

 

检查完毕,删掉了不兼容php7.3 的插件,然后就开始准备升级discuz到7.3了