tboothman / imdbphp

PHP library for retrieving film and tv information from IMDb
247 stars 84 forks source link

PHP8.1 deprecated str_replace() in Title.php #288

Closed jcvignoli closed 1 year ago

jcvignoli commented 1 year ago

Just updated to PHP8.1 and found a deprecated code:

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in vendor/imdbphp/imdbphp/src/Imdb/Title.php on line 1645

On line 1645 in Title.php, changing $dir['role'] = str_replace(' / ...', '', $dir['role']); by $dir['role'] = str_replace(' / ...', '', $dir['role'] ?? ''); did the trick.

duck7000 commented 1 year ago

well it might be better to check if $dir['role'] == null up front?

jcvignoli commented 1 year ago

Yes, much cleaner 😃

jreklund commented 1 year ago

Solved in v7.4.0+

duck7000 commented 1 year ago

To me it sounds strange to cast null to string? If you up front check if $dir['role'] == null the whole string_replace could be bypassed.

It is a matter of opinion i guess

jreklund commented 1 year ago

Converting/casting null to scalar types is what PHP did internally until they deprecated it in PHP 8.1 so it's a backport of how it used to behave. Some projects use them but it's better to rewrite your application to correct types as PHP are becoming a more strict language. Which we can't use as this project supports PHP 5.6+.

PHP RFC: Deprecate passing null to non-nullable arguments of internal functions

duck7000 commented 1 year ago

@jreklund thanks for explaining, i didn't know this!