PHPソースのバージョン互換状況をチェック(静的解析)する
PHP_CodeSniffer と PHPCompatibility を使う。
インストール
先ず、PHP_CodeSniffer をインストールする。下のどちらかの方法で・・・
1 2 |
pear install PHP_CodeSniffer ← pear ならこんな感じ composer global require "squizlabs/php_codesniffer=*" ← composer を使う場合はこんな感じ |
pear でインストールすると CentOS7 ではここに落ちるので cd で移動する
1 |
cd /usr/share/tests/pear/PHP_CodeSniffer/CodeSniffer |
PHPCompatibility を Github からダウンロード、展開・リネームして Standards/MySource に配置
1 2 3 4 |
cd Standards/MySource wget https://github.com/wimg/PHPCompatibility/archive/master.zip unzip master.zip mv PHPCompatibility-master PHPCompatibility |
これで phpcs と PHPCompatibility が使えるようになる。
解析
/var/www/html 以下の拡張子 php を互換性チェックするという指定。
ファイル数やステップ数にもよるが、解析には結構時間はかかる。
1 |
phpcs --standard=PHPCompatibility /var/www/html --extensions=php |
警告(WARNING) を含めた以内ならば -n を付ける。CSV形式で出力したい場合は –report=csv を付ける。
コラム:PHPCompatibilityがインストールされていない!?
1 2 |
# phpcs --standard=PHPCompatibility /var/www/html --extensions=php ERROR: the "PHPCompatibility" coding standard is not installed. The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend |
とか表示されてしまった場合は、とりあえず、PHPCompatibility の場所をフルパスで指定すればなんとかなる。
1 |
# phpcs --standard=/PHPCompatibilityのフルパス/PHPCompatibility /var/www/html --extensions=php |
でも、これが表示された場合は、git から最新を持ってこいという解決策が正攻法みたい。
コラム:メモリが足りない
メモリが足りなくて落ちてしまう場合は、php.ini で memory_limit を増やす。
結果(抜粋)
こんな感じで表示される。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
FILE: /var/www/html/wp-includes/Text/Diff.php ---------------------------------------------------------------------- FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 128 | ERROR | 'clone' is a reserved keyword introduced in PHP | | version 5.0 and cannot be invoked as a function | | (T_CLONE) ---------------------------------------------------------------------- FILE: /var/www/html/wp-includes/load.php ---------------------------------------------------------------------- FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES ---------------------------------------------------------------------- 17 | WARNING | INI directive 'register_globals' is deprecated from | | PHP 5.3 and forbidden from PHP 5.4. 627 | ERROR | 'clone' is a reserved keyword introduced in PHP | | version 5.0 and cannot be invoked as a function | | (T_CLONE) ---------------------------------------------------------------------- |