Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix Nullable Deprecated in PHP 8.4 #4086

Closed
BrookeDot opened this issue Feb 13, 2025 · 4 comments · Fixed by #4087
Closed

BUG: Fix Nullable Deprecated in PHP 8.4 #4086

BrookeDot opened this issue Feb 13, 2025 · 4 comments · Fixed by #4087
Labels
bug Something isn't working

Comments

@BrookeDot
Copy link
Contributor

Describe the bug

PHP 8.4 deprecated marking a parameter as nullable. ElasticPress now has the following PHP warning appear in the PHP logs:

ElasticPress\Feature\WooCommerce\OrdersAutosuggest::__construct(): Implicitly marking parameter $woocommerce as nullable is deprecated, the explicit nullable type must be used instead

Here is the stack trace from Query Monitor

    wp-content/plugins/elasticpress/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php:52
    {closure:/srv/htdocs/wp-content/plugins/elasticpress/elasticpress.php:75}()
    wp-content/plugins/elasticpress/includes/classes/Feature/WooCommerce/WooCommerce.php:75
    ElasticPress\F\W\WooCommerce->__construct()
    wp-content/plugins/elasticpress/elasticpress.php:170
    ElasticPress\register_indexable_posts()
    wp-includes/class-wp-hook.php:324
    do_action('plugins_loaded')
    wp-settings.php:559

https://github.com/10up/ElasticPress/blob/develop/includes/classes/Feature/WooCommerce/OrdersAutosuggest.php#L52

Side note, I don't have WooCommerce installed.

Steps to Reproduce

  1. Install PHP 8.4, ElasticPress and optionally Query Monitor but ensure that logging is enabled
  2. Notice the warning in the logs

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress and ElasticPress information

Elasticsearch

Third Party Elasticserch index, healthy.

ElasticPress

Version 5.1.4

PHP

Version	8.4.3
SAPI	fpm-fcgi
max_execution_time	850
memory_limit	512M
upload_max_filesize	2047M
post_max_size	2047M
display_errors	
log_errors	1
Error Reporting	4983
Extensions	55

Database

Server Version	10.6.20-MariaDB-log
Extension	mysqli
Client Version	80403 (8.4.3)
User        ***
Host	127.0.0.1
Database	***
innodb_buffer_pool_size	34359738368 (~32 GB)
key_buffer_size	134217728 (~128 MB)
max_allowed_packet	104857600 (~100 MB)
max_connections	500

WordPress

Version	6.7.2
Environment Type ([Help](https://make.wordpress.org/core/2020/07/24/new-wp_get_environment_type-function-in-wordpress-5-5/))	production
Development Mode ([Help](https://core.trac.wordpress.org/changeset/56042))	empty string
WP_DEBUG	false
WP_DEBUG_DISPLAY	false
WP_DEBUG_LOG	false
SCRIPT_DEBUG	false
WP_CACHE	true
CONCATENATE_SCRIPTS	undefined
COMPRESS_SCRIPTS	undefined
COMPRESS_CSS	undefined
WP_ENVIRONMENT_TYPE	undefined
WP_DEVELOPMENT_MODE	empty string

Server

Software	nginx
Version	Unknown
IP Address	****
OS	Linux 4.19.0-27-amd64
Architecture	x86_64
Basic Auth	false```

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct
@BrookeDot BrookeDot added the bug Something isn't working label Feb 13, 2025
@BrookeDot BrookeDot changed the title BUG: Fix Nullable Deprication in PHP 8.4 BUG: Fix Nullable Deprecated in PHP 8.4 Feb 13, 2025
@felipeelia
Copy link
Member

As we still need to be compatible with PHP 7.4, we can't use WooCommerce|null as a type, so we'll likely need to change

public function __construct( WooCommerce $woocommerce = null ) {

with

public function __construct( $woocommerce = null ) {

@BrookeDot would you be interested in opening a PR with that change? Thanks in advance!

@BrookeDot
Copy link
Contributor Author

Uggg... PHP 7.4 😄

Confirmed the suggestion fixes the warning in PHP 8.4 and updated the PR.

@felipeelia
Copy link
Member

K, this made me curious and actually, we do have a better way to work around this PHP 7.4 limitation. Although it seems we can't use WooCommerce|null, we could use the ? notation of nullable types (available since PHP 7.1). @BrookeDot sorry for the back and forth, but do you mind making that

public function __construct( ?WooCommerce $woocommerce = null ) {

Thank you very much!

@BrookeDot
Copy link
Contributor Author

oh nice, I saw the ? in the docs, but didn't think to look at when it was supported, in part because it is less readable. But indeed a better solution.

PR Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants