絖綛 N@i.jp  昨日:00049792
 今日:00003219
 総計:00812675
keywords
管理者専用
  Post   Add link   Control Panel 































新しいトピック
最新:07/03 19:28


新しいコメント
最新:07/28 16:47






管理人へMAIL

プライバシーポリシー

PHP 8系に移行できるだろうか?(5)

先にBlogのPHPスクリプトをPHP 8互換に修正しておこう


 PHP 8にすると Apacheの子プロセスがSIGBUSで異常終了する原因が全然分からないので、先にウチの BlogのPHPスクリプトにある非互換や非サポート(廃止)となった機能を使用している箇所がないか調査・修正してしまおうと思います(異常終了の原因が廃止機能を使っているからでは?という淡い期待もありまして・・・)。
 とは言え、闇雲に PHPスクリプトを眺めていても埒が明かないので、互換性チェックツールを使用します。以前PHP 7に移行した際には php7ccというツールを使用しましたが、これの PHP 8版は無いものかと調べたところ、どうやら PHP_CodeSniffer + PHPCompatibility という組み合わせを使うのが主流になっているっぽい。PHP_CodeSniffer というのは PHP用の lintツールみたいなものかな?それに PHPCompatibility という互換性チェックのルールを追加するみたい。なのでワタシも色々な記事を参考に、この方法でやってみることにしました。

 手順としては大まかに、

  1. Composer をインストール
  2. Composer を使って PHP_CodeSniffer をインストール
  3. Composer を使って PHPCompatibility をインストール
  4. PHP_CodeSniffer のルールに PHPCompatibility を追加
  5. PHP_CodeSniffer のルールに PHPCompatibility を指定して互換性チェック

となるようです。では、やって行きましょう。

1. Composer をインストールする

 以前インストールした Composerは使い終わった時に消してしまったのか何処にも見当たらないので、再インストールします。インストール手順はDownload Composerに従って行います。

$ /usr/local/php7/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

$ usr/local/php7/bin/php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

Installer verified
$ /usr/local/php7/bin/php composer-setup.php
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

All settings correct for using Composer
Downloading...
The "https://getcomposer.org/versions" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
error:14FFF086:SSL routines:(UNKNOWN)SSL_internal:certificate verify failed
Failed to enable crypto
failed to open stream: operation failed
Retrying...
The "https://getcomposer.org/versions" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
error:14FFF086:SSL routines:(UNKNOWN)SSL_internal:certificate verify failed
Failed to enable crypto
failed to open stream: operation failed
Retrying...
The "https://getcomposer.org/versions" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
error:14FFF086:SSL routines:(UNKNOWN)SSL_internal:certificate verify failed
Failed to enable crypto
failed to open stream: operation failed
The download failed repeatedly, aborting.

 あれ?SSL認証のエラーか?ダウンロードが失敗して Composerのセットアップができない様子。そこで "/usr/local/php7/lib/php.ini" を以下のように修正。

[openssl]
;openssl.cafile=
  ↓
openssl.cafile=/usr/local/libressl/etc/ssl/cert.pem

 もう一度 Composerのセットアップを試みます。

$ /usr/local/php7/bin/php composer-setup.php
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

All settings correct for using Composer
Downloading...

Composer (version 2.5.2) successfully installed to: /home/hoge/tmp/composer.phar
Use it: php composer.phar

$ ls -l
total 5656
-rw-r--r--   1 hoge hoge   58140  2  6 09:31 composer-setup.php
-rwxr-xr-x   1 hoge hoge 2831358  2  6 09:40 composer.phar

Composerのセットアップが成功。今回は消さずに取っておくようにしましょうかね。

$ /usr/local/php7/bin/php -r "unlink('composer-setup.php');"
$ sudo mv ./composer.phar /usr/local/php7/bin/composer
$ PATH="/usr/local/php7/bin:$PATH"
$ composer --help
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

Description:
  List commands

Usage:
  list [options] [--] []

Arguments:
  namespace                      The namespace name

Options:
      --raw                      To output raw command list
      --format=FORMAT            The output format (txt, xml, json, or md) [default: "txt"]
      --short                    To skip describing commands' arguments
  -h, --help                     Display help for the given command. When no command is given display help for the list command
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi|--no-ansi           Force (or disable --no-ansi) ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
      --no-scripts               Skips the execution of all scripts defined in composer.json file.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  The list command lists all commands:

    /usr/local/php7/bin/composer list

  You can also display the commands for a specific namespace:

    /usr/local/php7/bin/composer list test

  You can also output the information in other formats by using the --format option:

    /usr/local/php7/bin/composer list --format=xml

  It's also possible to get raw list of commands (useful for embedding command runner):

    /usr/local/php7/bin/composer list --raw

2. Composer を使って PHP_CodeSniffer をインストール

$ composer global require squizlabs/php_codesniffer
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

Changed current directory to /home/hoge/.composer
The package you required is recommended to be placed in require-dev (because it is tagged as "static analysis") but you did not use --dev.
Do you want to re-run the command with --dev? [yes]? yes
./composer.json has been updated
Running composer update squizlabs/php_codesniffer
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking squizlabs/php_codesniffer (3.7.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading squizlabs/php_codesniffer (3.7.1)
  - Installing squizlabs/php_codesniffer (3.7.1): Extracting archive
Package sstalle/php7cc is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
No security vulnerability advisories found
Using version ^3.7 for squizlabs/php_codesniffer

$ ls -l ~/.composer/vendor/bin
total 24
-rwxr-xr-x   1 hoge hoge 3336  2  6 10:11 php7cc
-rwxr-xr-x   1 hoge hoge 3369  2  6 10:11 phpcbf
-rwxr-xr-x   1 hoge hoge 3366  2  6 10:11 phpcs
$ ~/.composer/vendor/bin/phpcs -h
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.


Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]
  [--cache[=]] [--no-cache] [--tab-width=]
  [--report=] [--report-file=] [--report-=]
  [--report-width=] [--basepath=] [--bootstrap=]
  [--severity=] [--error-severity=] [--warning-severity=]
  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
  [--standard=] [--sniffs=] [--exclude=]
  [--encoding=] [--parallel=] [--generator=]
  [--extensions=] [--ignore=] [--ignore-annotations]
  [--stdin-path=] [--file-list=] [--filter=]  - ...

〜長いので以下略〜

3. Composer を使って PHPCompatibility をインストール

$ composer global require phpcompatibility/php-compatibility
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

Changed current directory to /home/hoge/.composer
The package you required is recommended to be placed in require-dev (because it is tagged as "static analysis") but you did not use --dev.
Do you want to re-run the command with --dev? [yes]? yes
./composer.json has been updated
Running composer update phpcompatibility/php-compatibility
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking phpcompatibility/php-compatibility (9.3.5)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading phpcompatibility/php-compatibility (9.3.5)
  - Installing phpcompatibility/php-compatibility (9.3.5): Extracting archive
2 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package sstalle/php7cc is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
No security vulnerability advisories found
Using version ^9.3 for phpcompatibility/php-compatibility

 PHPCompatibility が使えるようになっているか確認します。

$ ls -l ~/.composer/vendor/phpcompatibility/php-compatibility/
total 464
-rw-r--r--    1 hoge hoge 192773 12 27  2019 CHANGELOG.md
-rw-r--r--    1 hoge hoge   7651 12 27  2019 LICENSE
-rw-r--r--    1 hoge hoge   2838 12 27  2019 PHPCSAliases.php
drwxr-xr-x   11 hoge hoge    374 12 27  2019 PHPCompatibility
-rw-r--r--    1 hoge hoge  18901 12 27  2019 README.md
-rw-r--r--    1 hoge hoge   1588 12 27  2019 composer.json
-rw-r--r--    1 hoge hoge   2859 12 27  2019 phpunit-bootstrap.php
$ ~/.composer/vendor/bin/phpcs -i
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz and Zend

Standards に PHPCompatibirity が無いですなぁ。インストールしただけじゃ使えるようにはならないのですね。

4. PHP_CodeSniffer のルールに PHPCompatibility を追加

$ ~/.composer/vendor/bin/phpcs --config-set installed_paths ~/.composer/vendor/phpcompatibility/php-compatibility/PHPCompatibility/
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

Using config file: /home/hoge/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

Config value "installed_paths" added successfully
$ ~/.composer/vendor/bin/phpcs -i
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend and PHPCompatibility

PHPCompatibility が追加されましたね。これなら大丈夫そうだね。

5. PHP_CodeSniffer のルールに PHPCompatibility を指定して互換性チェック

 とりあえず "index.php" で試してみましょうかね?

$ ~/.composer/vendor/bin/phpcs -p index.php --standard=PHPCompatibility --runtime-set testVersion 8.0
Zend OPcache requires Zend Engine API version 320180731.
The Zend Engine API version 320190902 which is installed, is newer.
Contact Zend Technologies at http://www.zend.com/ for a later version of Zend OPcache.

. 1 / 1 (100%)


Time: 627ms; Memory: 6MB

うん、使えているみたいだけど、使い方はこれで合っているのかなぁ?"--runtime-set"オプションはこれで良いのか?全く自信ナシ。まぁいいや、やれるだけやってみましょう。

$ ~/.composer/vendor/bin/phpcs -p *.php --standard=PHPCompatibility --runtime-set testVersion 8.0

WWWWWWW.W 9 / 9 (100%)

FILE: /home/hoge/htdocs/link_category.php
----------------------------------------------------------------------
FOUND 0 ERRORS AND 3 WARNINGS AFFECTING 3 LINES
----------------------------------------------------------------------
 398 | WARNING | Function each() is deprecated since PHP 7.2; Use a
     |         | foreach loop instead
 412 | WARNING | Function each() is deprecated since PHP 7.2; Use a
     |         | foreach loop instead
 449 | WARNING | Function each() is deprecated since PHP 7.2; Use a
     |         | foreach loop instead
----------------------------------------------------------------------

〜長いので以下略〜

出力されたのは each()関数を使っているという WARNING(警告)ですが、調べたところ each()関数は PHP 8.0で削除されたそうです。これは修正しなければなりません。
 ウチの Blogでの each()関数の使い方は全て以下のような定番ループでした。

while(list($key,$value) = each($array))
{
    ループ内の処理
}

これは機械的に以下のように書き換えてやればOK。

foreach($array as $key => $value)
{
    ループ内の処理
}

each()関数以外には、たぶん使っていない機能だと思うけど、幾つか "<?" が残っていましたね。念のため "<?php" に修正しておきました。さて、これで異常終了しなくなってくれれば良いのだけど・・・


< 過去の記事 [ 2月の 全てのカテゴリ リスト ] 新しい記事 >

2023 calendar
2月
1234
567891011
12131415161718
19202122232425
262728


掲示板
最新:08/15 17:19


GsBlog was developed by GUSTAV, Copyright(C) 2003, Web Application Factory All Rights Reserved.