ERROR: Trying to access array offset on value of type null(egulias/email-validator)
2022/04/15
php
問題
egulias/email-validator で、エラーになります。
変更したのは環境で、phpのバージョンを変えたらエラーになりました。
もとはphp7.1で変更後は7.4です。
ERROR: Trying to access array offset on value of type null {"exception":"[object] (ErrorException(code: 0): Trying to access array offset on value of type null at /xxxxxxxxxx/vendor/egulias/email-validator/EmailValidator/Parser/Parser.php:147)
答え
egulias/email-validator の2.1系はphp7.4に対応できていないらしい。
可能なら、 composer update する。
無理なら、以下のように少し追記すると、問題は回避できる。
変更前
protected function escaped()
{
$previous = $this->lexer->getPrevious();
if ($previous['type'] === EmailLexer::S_BACKSLASH // <- ここでエラー
&&
$this->lexer->token['type'] !== EmailLexer::GENERIC
) {
return true;
}
return false;
}
変更後
protected function escaped()
{
$previous = $this->lexer->getPrevious();
if (isset($previous['type']) // <- ここ追加
&&
$previous['type'] === EmailLexer::S_BACKSLASH
&&
$this->lexer->token['type'] !== EmailLexer::GENERIC
) {
return true;
}
return false;
}