Skip to content

PHP 8.5: TypeError in CustomerDataTrait — strict_types return type mismatch #8126

@theMosaad

Description

@theMosaad

Bug Description

CustomerDataTrait::get_customer_key() and CustomerDataTrait::get_customer_email() in inc/Engine/License/API/CustomerDataTrait.php declare string return types. The file uses declare(strict_types=1).

However, both methods return values from Options_Data::get() and rocket_get_constant(), which return mixed. On PHP 8.5, when the stored value is not strictly a string (e.g., an integer from the database), this causes a fatal TypeError:

TypeError: CustomerDataTrait::get_customer_key(): Return value must be of type string, int returned

Steps to Reproduce

  1. Install WP Rocket 3.20.5 on PHP 8.5
  2. Activate WP Rocket with a valid license
  3. Visit any page — the TypeError is triggered during license validation

Expected Behavior

Methods should return a string without throwing a TypeError.

Proposed Fix

Add explicit (string) casts to the return values in both methods. This is backwards-compatible — casting a string to string is a no-op on older PHP versions.

 protected function get_customer_key(): string {
     return ! empty( $this->options->get( 'consumer_key', '' ) )
-        ? $this->options->get( 'consumer_key', '' )
-        : rocket_get_constant( 'WP_ROCKET_KEY', '' );
+        ? (string) $this->options->get( 'consumer_key', '' )
+        : (string) rocket_get_constant( 'WP_ROCKET_KEY', '' );
 }

 protected function get_customer_email(): string {
     return ! empty( $this->options->get( 'consumer_email', '' ) )
-        ? $this->options->get( 'consumer_email', '' )
-        : rocket_get_constant( 'WP_ROCKET_EMAIL', '' );
+        ? (string) $this->options->get( 'consumer_email', '' )
+        : (string) rocket_get_constant( 'WP_ROCKET_EMAIL', '' );
 }

I'll submit a PR with this fix shortly.

Environment

  • WP Rocket version: 3.20.5
  • PHP version: 8.5
  • WordPress version: 6.9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions