Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/v2/PageProfiler/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function is_in_all_viewports( int $image_id ): bool {
*/
public function is_lcp_image_in_all_viewports( int $image_id ): bool {
foreach ( self::get_active_devices() as $device ) {
if ( ( ( self::$current_profile_data[ $device ]['lcp']['type'] ?? '' ) === 'img' ) && ( self::$current_profile_data[ $device ]['lcp']['imageId'] === $image_id ) ) {
if ( ( ( self::$current_profile_data[ $device ]['lcp']['type'] ?? '' ) === 'img' ) && ( ( self::$current_profile_data[ $device ]['lcp']['imageId'] ?? null ) === $image_id ) ) {
return true;
}
}
Expand Down
34 changes: 33 additions & 1 deletion inc/v2/PageProfiler/Storage/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,42 @@ abstract public function store( string $key, array $data );
* Retrieve data by key.
*
* @param string $key The unique identifier for the data to retrieve.
* @return array|false The stored data or null if not found.
* @return array<string, mixed>|false The stored data or false if not found.
*/
abstract public function get( string $key );

/**
* Coerce a stored profiler payload to an array.
*
* Object-cache backends that JSON-decode without associative arrays return stdClass.
* Nested objects (e.g. `af`, `bg`, `lcp`) are converted recursively.
*
* @param mixed $value Raw storage value.
* @return array<string|int, mixed>|false
*/
public static function normalize_value( $value ) {
if ( false === $value || null === $value ) {
return false;
}

if ( is_object( $value ) ) {
$value = get_object_vars( $value );
}

if ( ! is_array( $value ) ) {
return false;
}

foreach ( $value as $key => $item ) {
if ( is_object( $item ) || is_array( $item ) ) {
$normalized_item = self::normalize_value( $item );
$value[ $key ] = ( false !== $normalized_item ) ? $normalized_item : [];
}
}

return $value;
}

/**
* Delete data by key.
*
Expand Down
4 changes: 2 additions & 2 deletions inc/v2/PageProfiler/Storage/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public function store( string $key, array $data ) {
* Retrieve data from the object cache.
*
* @param string $key The unique identifier for the data to retrieve.
* @return array|false The stored data or false if not found.
* @return array<string, mixed>|false The stored data or false if not found.
*/
public function get( string $key ) {
return wp_cache_get( $key, self::GROUP );
return self::normalize_value( wp_cache_get( $key, self::GROUP ) );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions inc/v2/PageProfiler/Storage/Transients.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public function store( string $key, array $data ) {
* Retrieves data from a transient.
*
* @param string $key The key to retrieve data for.
* @return mixed The stored data or false if the transient doesn't exist or has expired.
* @return array<string, mixed>|false The stored data or false if the transient doesn't exist or has expired.
*/
public function get( string $key ) {
return get_transient( $this->get_key( $key ) );
return self::normalize_value( get_transient( $this->get_key( $key ) ) );
}

/**
Expand Down
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3072,12 +3072,6 @@ parameters:
count: 1
path: inc/v2/PageProfiler/Storage/Base.php

-
message: '#^Method OptimoleWP\\PageProfiler\\Storage\\Base\:\:get\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: inc/v2/PageProfiler/Storage/Base.php

-
message: '#^Method OptimoleWP\\PageProfiler\\Storage\\Base\:\:store\(\) has no return type specified\.$#'
identifier: missingType.return
Expand All @@ -3090,12 +3084,6 @@ parameters:
count: 1
path: inc/v2/PageProfiler/Storage/Base.php

-
message: '#^Method OptimoleWP\\PageProfiler\\Storage\\ObjectCache\:\:get\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: inc/v2/PageProfiler/Storage/ObjectCache.php

-
message: '#^Method OptimoleWP\\PageProfiler\\Storage\\ObjectCache\:\:store\(\) has parameter \$data with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
Expand Down
Loading
Loading