13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHP_CodeSnifferを用いたphpアプリケーションの互換性チェック

Last updated at Posted at 2018-02-06

はじめに

  • phpで書かれたコードが特定のphpバージョンでどの程度動くのか把握したい
  • errorだけじゃなくdeprecatedな警告とかも把握しておきたい
  • そうだ,PHP_CodeSnifferを使おう

環境

  • OS : macOS Sierra 10.12.6 (16G1212)
  • Homebrew : 1.5.3
shell
$ brew --version
Homebrew 1.5.3
Homebrew/homebrew-core (git revision 6fa36; last commit 2018-02-06)
  • php : 7.2.2 with ext-xdebug
shell
$ 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
shell
$ composer --version
Composer version 1.6.3 2018-01-31 16:28:17

phpの設定

PHP_CodeSnifferを使う為に各種設定をします。

メモリ使用上限の拡張

コード解析時にメモリ上限に引っかかることが多いため,メモリ上限を2048Mに設定します。(別に-dオプションで実行時に指定しても良いのだけど忘れがちなので...)

.iniファイルのパスは--iniオプションで取得できます。Homebrewで導入している場合,php-memory-limits.iniと別ファイルに分けられた設定があるはずなのでそちらを編集します。

shell
$ 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
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

設定が反映されていることを確認します。

shell
$ php -r 'echo ini_get("memory_limit");'
2048M

PHPCompatibilityのインストール

以下の通りインストールします。wimg/php-compatibilityの依存関係にあるため,同時にsquizlabs/php_codesnifferもインストールされます。

shell
$ 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_CodeSnifferの環境設定

whichにてphpcsPATHが通っているか確認します。

shell
$ which phpcs
/Users/zeriyoshi/.composer/vendor/bin/phpcs

PATHが通っていないようであれば,PATHcomposerbinディレクトリを追加します。

shell
$ echo -e 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bash_profile; source ~/.bash_profile
$ which phpcs
/Users/zeriyoshi/.composer/vendor/bin/phpcs

次にPHP_CodeSnifferPHPCompatibilityのパスを設定します。

shell
$ 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

実際に使ってみる

環境設定が完了したので,実際に解析を行ってみます。

ZendFramework 1php 7.2でテスト

既にEOLしているZendFramework 1(1.12.20 Release)をphp 7.2環境でテストしてみます。

shell
$ 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%)

めっちゃ草生えてる。

この後エラー,警告が発生したファイルと内容が表示されるのだけれど多すぎるので省略...
WarningだけじゃなくErrorも出てしまっていますね...そのまま動かすのは無理そう。

その他

基本的にphpのバージョンは--runtime-set testVersionで指定します。
--runtime-set testVersion 5.3-5.5のように範囲指定も可能みたい。

参考文献

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?