Magento2:php8.1環境コンテンツDeployエラー回避方法

2022年11月8日

イントロダクション

php8.1環境で CLIコンソールからコンテンツデプロイコマンド “bin/magento setup:static-content:deploy" を実行すると下記のようなエラーが表示されてデプロイ処理が正常に行われない場合があります。本記事では、この問題への対処方法を紹介します。

Error happened during deploy process: Deprecated Functionality: pathinfo(): Passing null to parameter #1 ($path) of type string is deprecated in /web/shop.example.com/www/vendor/magento/framework/View/Asset/PreProcessor/FileNameResolver.php on line 44

(※)本記事執筆時のMagentoバージョンは2.4.5になります。

エラー原因

原因は、PHP8.1における引数nullパラメータ指定時の評価厳密化によるものです。
参考記事:[php.watch]PHP 8.1: Passing null to non-nullable internal function parameters is deprecated
特定の条件下で起こる問題のようです。筆者の経験だとThemeファイルの構成フォルダにおいて、ファイルを配下に持たない空のフォルダが含まれる場合に起きました。
この問題に対する修正は今後Magento本体のバージョンアップにより修正されるものと思われますが、バージョン2.4.5の時点ではまだ修正対処されていません。

エラー回避手順

修正が必要なソースファイルは、composer管理のファイルですが、下記のようにMagento本体ソースコードを修正することにより解消されますので、公式配布ソースが修正されるまでの間は直接編集更新することで対処できます。

修正対象ファイル: ./vender/magento/framework/App/Utility/Files.php
accumulateThemeStaticFiles() 関数内
[修正前]

private function accumulateThemeStaticFiles($area, $locale, $filePattern, &$result)
    {...(省略)...
                if (!$files) {
                    $result[] = [
                        $themeArea,
                        $themePackage->getVendor() . '/' . $themePackage->getName(),
                        null,
                        null,
                        null,
                        null
                    ];
                }
    ...(省略)...
    }


【修正後】

private function accumulateThemeStaticFiles($area, $locale, $filePattern, &$result)
    {...(省略)...
                if (!$files) {
                    $result[] = [
                        $themeArea,
                        $themePackage->getVendor() . '/' . $themePackage->getName(),
                        '',
                        '',
                        '',
                        ''
                    ];
                }
    ...(省略)...
    }

(*)null指定部分を空文字 " に変更