使用PHP_CodeSniffer进行PHP应用程序的兼容性检查
首先
phpで書かれたコードが特定のphpバージョンでどの程度動くのか把握したい
errorだけじゃなくdeprecatedな警告とかも把握しておきたい
そうだ,PHP_CodeSnifferを使おう
环境
-
- OS : macOS Sierra 10.12.6 (16G1212)
Homebrew : 1.5.3
$ brew --version
Homebrew 1.5.3
Homebrew/homebrew-core (git revision 6fa36; last commit 2018-02-06)
php : 7.2.2 with ext-xdebug
$ php --version
PHP 7.2.2 (cli) (built: Feb 1 2018 11:47:34) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans
composer : 1.6.3
$ composer --version
Composer version 1.6.3 2018-01-31 16:28:17
PHP配置
为了使用PHP_CodeSniffer,需要进行各种设置。
扩展内存使用上限
由于在代码分析时经常遇到内存限制问题,所以将内存限制设置为2048M。(虽然可以使用-d选项在运行时指定,但经常忘记,所以…)
如果使用Homebrew进行安装,应该可以通过–ini选项获取.ini文件的路径。因此,我们需要编辑php-memory-limits.ini和其他文件中分隔的设置。
$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.2
Loaded Configuration File: /usr/local/etc/php/7.2/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.2/conf.d/ext-smbclient.ini,
/usr/local/etc/php/7.2/conf.d/ext-xdebug.ini,
/usr/local/etc/php/7.2/conf.d/php-memory-limits.ini
; Max memory per instance
memory_limit = 2048M
;The maximum size of an uploaded file.
upload_max_filesize = 128M
;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize
post_max_size = 128M
请确认设置已经生效。
$ php -r 'echo ini_get("memory_limit");'
2048M
安装PHPCompatibility
将按照以下方式进行安装。由于其依赖于wimg/php-compatibility,因此还会同时安装squizlabs/php_codesniffer。
$ composer global require wimg/php-compatibility
Changed current directory to /Users/zeriyoshi/.composer
Using version ^8.1 for wimg/php-compatibility
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing squizlabs/php_codesniffer (3.2.2): Loading from cache
- Installing wimg/php-compatibility (8.1.0): Loading from cache
wimg/php-compatibility suggests installing dealerdirect/phpcodesniffer-composer-installer (^0.4.3)
Writing lock file
Generating autoload files
PHP代码嗅探器的环境配置。
用which命令检查是否已经设置phpcs的路径。
$ which phpcs
/Users/zeriyoshi/.composer/vendor/bin/phpcs
如果PATH找不到,请将composer的bin目录添加到PATH中。
$ echo -e 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile; source ~/.bash_profile
$ which phpcs
/Users/zeriyoshi/.composer/vendor/bin/phpcs
接下来,我们需要将PHP_CodeSniffer配置为PHPCompatibility的路径。
$ phpcs --config-set installed_paths `echo $HOME/.composer/vendor/wimg/php-compatibility/PHPCompatibility`
Using config file: /Users/zeriyoshi/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer.conf
Config value "installed_paths" added successfully
亲自尝试使用一下
由于环境设置已经完成,接下来进行实际分析。
用PHP 7.2版本对ZendFramework 1进行测试
我将在php 7.2环境下测试已经停止支持的Zend Framework 1(1.12.20 Release)。
$ phpcs -p zendframework/ --standard=PHPCompatibility --runtime-set testVersion 7.2
E........................E.W.......W........................ 60 / 2282 (3%)
............................................................ 120 / 2282 (5%)
.....E.........W............................................ 180 / 2282 (8%)
.............................................W.............. 240 / 2282 (11%)
....................................................E....... 300 / 2282 (13%)
............................................................ 360 / 2282 (16%)
............................................................ 420 / 2282 (18%)
....................................W.......WWW............. 480 / 2282 (21%)
.............................W.............................. 540 / 2282 (24%)
W.........................E................................. 600 / 2282 (26%)
............................................................ 660 / 2282 (29%)
......W..........................................W.......... 720 / 2282 (32%)
............................................................ 780 / 2282 (34%)
........................................W................... 840 / 2282 (37%)
............................................................ 900 / 2282 (39%)
......................................W........W..........W. 960 / 2282 (42%)
.................................E.......................... 1020 / 2282 (45%)
.......W.................................................... 1080 / 2282 (47%)
............................................................ 1140 / 2282 (50%)
............................................................ 1200 / 2282 (53%)
..............................W............................. 1260 / 2282 (55%)
............................................................ 1320 / 2282 (58%)
......W..................................................... 1380 / 2282 (60%)
............................................................ 1440 / 2282 (63%)
............................................................ 1500 / 2282 (66%)
.............W.W............................................ 1560 / 2282 (68%)
............................................................ 1620 / 2282 (71%)
................................................E........... 1680 / 2282 (74%)
............................................................ 1740 / 2282 (76%)
.....................................................W...... 1800 / 2282 (79%)
............................................................ 1860 / 2282 (82%)
...................W........................................ 1920 / 2282 (84%)
............................................................ 1980 / 2282 (87%)
............................................................ 2040 / 2282 (89%)
............................................................ 2100 / 2282 (92%)
.................W.......................................... 2160 / 2282 (95%)
............................................................ 2220 / 2282 (97%)
.............................................W.............. 2280 / 2282 (100%)
.. 2282 / 2282 (100%)
草长得很茂盛。
后面会显示出发生错误和警告的文件及其内容,但是数量太多所以省略了…
不仅有警告,还出现了错误吗… 看来这样是无法正常运行的。
其他
一种基本的方法是使用 –runtime-set testVersion 指定 PHP 的版本。也可以像 –runtime-set testVersion 5.3-5.5 这样指定一个范围。
参考资料
wimg/PHPCompatibility: PHP Compatibility check for PHP_CodeSniffer
squizlabs/PHP_CodeSniffer: PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.