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

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

# 環境
* OS : macOS Sierra 10.12.6 (16G1212)
* [``Homebrew``](https://brew.sh/) : ``1.5.3``

```bash:shell
$ brew --version
Homebrew 1.5.3
Homebrew/homebrew-core (git revision 6fa36; last commit 2018-02-06)
```

* [``php``](https://php.net/) : ``7.2.2`` with ``ext-xdebug``

```bash: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
```

* [``composer``](https://getcomposer.org/) : ``1.6.3``

```bash: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``と別ファイルに分けられた設定があるはずなのでそちらを編集します。

```bash: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
```

```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
```

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

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

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

```bash: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``にて``phpcs``の``PATH``が通っているか確認します。

```bash:shell
$ which phpcs
/Users/zeriyoshi/.composer/vendor/bin/phpcs
```

``PATH``が通っていないようであれば，``PATH``に``composer``の``bin``ディレクトリを追加します。

```bash:shell
$ 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``のパスを設定します。

```bash: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 1``を``php 7.2``でテスト

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

```bash: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``のように範囲指定も可能みたい。

# 参考文献
* [wimg/PHPCompatibility](https://github.com/wimg/PHPCompatibility): PHP Compatibility check for PHP_CodeSniffer
* [squizlabs/PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer): PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.